Polymorphism
> Click or hit Control-Enter to run Example.main above
Next Few Classes
-
Today: polymorphism and inheritance review
-
Friday: no class! Enjoy a long weekend and good luck finishing up MP1!
-
Next Monday: objects review session, including examples from this week’s homework problems
-
Next Wednesday: more about polymorphism
Hierarchical Thinking
Why organize objects into a hierarchy?
-
It can be a natural expression of real-world taxonomies
-
It allows us to organize and reuse code between multiple classes
The Tree Of (Java) Life
In Java, each class has a single parent, meaning that classes are organized into a tree.
If we follow each node to its parent, we eventually get to the top, or root…
The Root Object: Object
public class Dog { }
// is equivalent to
public class Dog extends Object { }
If a Java class does not explicitly extend another class, it implicitly
extends Object.
Inherited from Object
public class Dog {
private String name;
Dog(String setName) {
name = setName;
}
}
public class Example {
public static void main(String[] unused) {
Dog chuchu = new Dog("Chuchu");
System.out.println(chuchu.toString());
}
}
All Java objects inherit a small number of important methods from Object.
As a result, all Java objects implement these methods!
Methods Inherited from Object
For our purposes, the following methods inherited from Object are important:
-
String toString(): return aStringrepresenting the instance. Frequently used for debugging. -
boolean equals(Object other): return abooleanindicating whether this object is the same as another object -
int hashCode(): return anintuniquely representing an object’s contents. We’ll talk more about hashing later—it’s incredibly important and useful.
Method Overriding
public class Dog {
private String name;
Dog(String setName) {
name = setName;
}
public String toString() {
return name;
}
}
public class Example {
public static void main(String[] unused) {
Dog chuchu = new Dog("Chuchu");
System.out.println(chuchu.toString());
}
}
The default Object methods are rarely useful.
So classes usually override them and provide their own.
Hierarchical Name and Method Resolution
The Java type hierarchy is used when resolving the names of variables and methods:
-
Does the class have a variable or method with the given name? If so, use it.
-
If not, search the parent class—but limited by
publicandprotected -
Continue up the tree until the name is found or the search fails
> Click or hit Control-Enter to run Example.main above
super Constructor
public class Pet {
protected String type;
Pet(String setType) {
type = setType;
}
}
public class Dog extends Pet {
private String breed;
Dog(String setBreed) {
super("Dog");
breed = setBreed;
}
}
Java classes can access their parent’s constructor using the super keyword.
This must be the first thing done in a child constructor.
> Click or hit Control-Enter to run Example.main above
Polymorphism
Polymorphism: the provision of a single interface to entities of different types.
We’ll discuss interfaces in more detail when we talk about about packages. For now, let’s identify two kinds of Java polymorphism using examples.
Subtype Polymorphism
public class Pet {
public void printMe() {
System.out.println("I'm a pet");
}
}
public class Dog extends Pet {
public void printMe() {
System.out.println("I'm a dog");
}
}
In Java, every object is really an instance of at least two types:
-
Each
Petis also anObject -
Each
Dogis also aPetand also anObject
Object Conversion: Upcasting
public class Pet { }
public class Dog extends Pet {
public String toString() {
return "Dog";
}
}
public class Example {
public static void main(String[] unused) {
Dog chuchu = new Dog();
Pet xyz = new Pet();
Example.printAnything(chuchu);
Example.printAnything(xyz);
}
public static void printAnything(Object toPrint) {
System.out.println(toPrint.toString());
}
}
Java will upcast object types automatically.
> Click or hit Control-Enter to run Example.main above
But Instances Retain Their Types
public class Pet { }
public class Dog extends Pet {
public String toString() {
return "Still a Dog";
}
}
public class Example {
public static void main(String[] unused) {
Dog chuchu = new Dog();
Object chuchuAsObject = chuchu;
System.out.println(chuchuAsObject);
Pet chuchuAsPet = chuchu;
System.out.println(chuchuAsPet);
}
}
> Click or hit Control-Enter to run Example.main above
Liskov Substitution Principle
Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e. an object of type T may be substituted with any object of a subtype S) without altering any of the desirable properties of T (correctness, task performed, etc.).
Barbara Liskov, Turing Award Winner
Barbara Liskov was one of the first women in the US to earn a doctorate in computer science. She won the Turing Award, the highest honor in computer science, in 2008.
She’s given a bunch of great talks that you can find on YouTube, like this one.
Obligatory Cheating Reminder
We take cheating in CS 125 very seriously. Everything you submit for the MP will be carefully checked against all other submissions.
Our full cheating policy is available here. But to summarize:
-
Discussing the MP in English 1 is OK.
-
Exchanging source code in any way is cheating.
Announcements
-
This week’s lab homework has been reopened. Have at it!
-
I have office hours from 1–3PM today in Siebel 2227. Please stop by!
-
We will not have class Friday.
-
Good luck finishing up MP1!
-
We have a anonymous feedback form to the course website. Use it to give us feedback!
