Object Review
> Click or hit Control-Enter to run Example.main above
Review: Polymorphism
Polymorphism: the provision of a single interface to entities of different types.
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
Pet
is also anObject
-
Each
Dog
is also aPet
and also anObject
Review: Object 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
Review: Object Downcasting
public class Pet { }
public class Dog extends Pet {
public String toString() {
return "Still a Dog";
}
}
public class Example {
public static void main(String[] unused) {
Object chuchu = new Dog();
Example.printAnything(chuchu);
Pet chuchuAsPet = (Pet) chuchu; // chuchu is a Pet, so this works
Example.printAnything(chuchuAsPet);
}
}
We can also cast instances down but only if the instance is actually the appropriate subtype.
Java checks the cast at runtime to make sure that it is appropriate.
> 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.).
Substitutability in Practice
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());
}
}
Since everything is an Object
, any Java object inherits all of the desirable
properties of Object
: like toString
.
Subtype Polymorphism
We can always use toString
, but every class can implement it
differently.
Same Names, Different Behavior
Where else have we seen this before?
> Click or hit Control-Enter to run the code above
Polymorphism
Polymorphism: the provision of a single interface to entities of different types.
-
Subtype polymorphism: a single method can act on all descendants of a given class
-
Method overloading: a method can behave differently depending on its arguments
-
Generics (discussed later)
Review: Flip
Define a public class
named Flip
with a single public instance method
called flop
that takes no parameters and returns a boolean
.
It should also provide a single constructor that takes a boolean
argument that
sets the initial state of the Flip
instance.
Review: LastTen
Complete the implementation of a class called LastTen
.
Your class should implement two public methods as described below:
-
void add(int newValue)
: add a new integer to the values that we are remembering -
int[] getLastTen()
: return the last ten values that were added using add, in any order. If fewer than ten values were added, you should return zeros in their place.
Your class should also provide a constructor that takes no arguments. It should not expose any of its state publicly.
> Click or hit Control-Enter to run Example.main above
> Click or hit Control-Enter to run Example.main above
> Click or hit Control-Enter to run Example.main above
Questions About Objects?
Announcements
-
MP2 is out and due in less than two weeks. Get started!
-
This week’s lab exercise is open and available over the weekend. Have fun!
-
I have office hours from 1–3PM today in Siebel 2227. Please stop by!
-
We will not have a lecture next Wednesday.
-
We have a anonymous feedback form to the course website. Use it to give us feedback!