Professor Abdul-Quader
Polymorphism
Finish demos today
Recap:
// Library class:
public void addBook(String title, String author, int year) {
// Is this object-oriented?
}Book.
Book object.Book object?What’s wrong with this?
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getAuthor().equals(author)) {
System.out.println(i + ". " + bookList.get(i));
} else {
System.out.println("There are no books written by " + author);
}
}Solution?
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);
}final String pig = "length: 10";
final String dog = "length: " + pig.length();
System.out.println(pig);
System.out.println(dog);
System.out.println("Animals are equal: " + pig == dog);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]
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;