Professor Abdul-Quader
Polymorphism
while loop
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter in a number: ");
int x = sc.nextInt();
System.out.println("Enter in a name: ");
String name = sc.nextLine();
System.out.println("You entered: " + x + ", " + name);
}What’s wrong with this?
try-catch-finally, try-with-resources, re-throwing.An interface is an abstract type. That is, it just describes behaviors, but it is not actually implemented. You cannot instantiate an interface. This is useful when we are defining methods:
ArrayList and LinkedList are two different implementations of the same List interface.
add, get, and size.ArrayList: keeps an array, resizes that array when needed.LinkedList: keeps each item in a “node”, node has a “link” to the next node.
add: create a new node, link from the last node.ArrayList and LinkedList are internal.list.get(10); or list.add("hello"); on either one.When we write a method, if we don’t care how list is implemented, we can just ask for a List as a parameter.
ComparableThe Collections class has a static method sort, which will sort a list for us. It can sort lists of any type – as long as the type implements the Comparable interface. The Comparable interface specifies just one method: compareTo.
ComparableIn order to implement the interface in your class:
Starter code. Implement the “compareTo” method in the Location class. Implement it so that:
Run the main method after you have implemented compareTo. It should output:
[10 St and 7 Ave, 15 St and 2 Ave, 15 St and 5 Ave, 25 St and 3 Ave]
Collections.sort method able to sort a list of Locations when it had no idea what a Location was when it was written?The ability for an object to take multiple forms. Example: if we have a method that can calculate the area of a Rectangle, it should work on Squares too!
Two primary ways of introducing polymorphism in Java:
When an object is instantiated, it has a “compile-time” (declared) type and a “run-time type” (the type of the actual object that is instantiated).
int width = 5; int height = 5;
Rectangle r = new Rectangle(width, height);
Square s = new Square(width);
Rectangle r2 = new Square(width);What are the declared and run-time types of \(r\), \(s\), and \(r2\)?
LinkedList<String> linkedList = new LinkedList<>();
ArrayList<String> arrayList = new ArrayList<>();
List<String> abstractList;Which of the following assignments are legal?
abstractList = linkedList;abstractList = arrayList;arrayList = abstractList;arrayList = linkedList;linkedList = arrayList;linkedList = abstractList;List<Integer> tells you that list.add(5) compiles.Inheritance: to make one class inherit from another, we use the extends keyword.
Square extends Rectangle means every Square is a Rectangle.Square!Square must invoke the constructor for its “superclass” (Rectangle).superMain, the print statement inside Rectangle happens twice:
Rectangle class, implement the getArea method, and then uncomment the printAllAreas(rectangles); line inside Main.Main. How does this correctly compute the areas even for the Square object?System.out.println statement inside main. What error do you get? Why?printAllShapes(rectangles); line inside Main. Run it once and take a look at the output. Then implement the toString method inside the Square class. When a Square with side length \(x\) is output, it should print out “Square with side length \(x\)”. Run the Main again and see if the output changes. (It should!) Why does it change?Object classEvery class inherits from the Object class. This provides default implementations of a few commonly used methods:
System.out.printlnprintln is defined on all Objectsprintln calls a method (which calls another, which, eventually) which calls the toString method on the Object.super.Object has a default (no-argument) constructor, which is automatically invoked (unless otherwise specified).Interfaces:
Extending classes:
public or protected members!Two main types of class relationships:
Employee and Person are two different classes, and Employee inherits from Person, we say that every Employee is a Person.Composition is referred to as a “has-a” relationship. That is: a Driver is not a Vehicle, but a Driver does have a Vehicle. Usually implemented by making the Vehicle be an instance variable of the Driver class.