Felix Grund
Instructor @ UBC
public class Poodle extends Dog {
}
Becomes a Subtype
Becomes a Supertype
...
Standard Poodle
Miniature Poodle
Toy Poodle
...
...
...
...
...
German
Shepherd
Border
Collie
Poodle
Dog
public class Dog {
public void bark() {
System.out.println("Wow wow");
}
}
public class Poodle extends Dog {
public void bark() {
System.out.println("Wiiif Wiiif");
}
}
public class BorderCollie extends Dog {
public void bark() {
System.out.println("Wuuuf Wuuuf");
}
}
public class GermanShepherd extends Dog {
public void bark() {
System.out.println("Waaaf Waaaf");
}
}
Dog
public class Dog {
public void bark() {
System.out.println("Wow wow");
}
}
public class Poodle extends Dog {
public void bark() {
System.out.println("Wiiif Wiiif");
}
}
public class BorderCollie extends Dog {
public void bark() {
System.out.println("Wuuuf Wuuuf");
}
}
public class GermanShepherd extends Dog {
public void bark() {
System.out.println("Waaaf Waaaf");
}
}
Dog dog = new Dog();
Dog poodle = new Poodle();
Dog borderCollie = new BorderCollie();
Dog germanShepherd = new GermanShepherd();
dog.bark(); // ???
poodle.bark(); // ???
borderCollie.bark(); // ???
germanShepherd.bark(); // ???
public class UnclassifiedDog extends Dog {
}
Dog unclassified = new UnclassifiedDog();
unclassified.bark(); // ???
super
protected
method override
supertype
subtype
declared type
actual type
method dispatch
method overload
= apparent type
= instantiated type
public class TransitPass {
// REQUIRES: amount > 0
// MODIFIES: this
// EFFECTS: adds amount to the balance on this pass
void reload(int amount) {...}
// EFFECTS: returns balance on this pass
int getBalance() {...}
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip
void tapIn() {...}
}
public class TransitPassTripTracker
extends TransitPass {
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip and
// adds one to number of trips
// paid for with this pass
void tapIn() {...}
// EFFECTS: returns number of trips paid for using
// this transit pass
int getNumTrips() {...}
}
public class TransitPass {
// REQUIRES: amount > 0
// MODIFIES: this
// EFFECTS: adds amount to the balance on this pass
void reload(int amount) {...}
// EFFECTS: returns balance on this pass
int getBalance() {...}
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip
void tapIn() {...}
}
public class TransitPassTripTracker
extends TransitPass {
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip and
// adds one to number of trips
// paid for with this pass
void tapIn() {...}
// EFFECTS: returns number of trips paid for using
// this transit pass
int getNumTrips() {...}
}
public class TransitPass {
// REQUIRES: amount > 0
// MODIFIES: this
// EFFECTS: adds amount to the balance on this pass
void reload(int amount) {...}
// EFFECTS: returns balance on this pass
int getBalance() {...}
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip
void tapIn() {...}
}
public class TransitPassTripTracker
extends TransitPass {
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip and
// adds one to number of trips
// paid for with this pass
void tapIn() {...}
// EFFECTS: returns number of trips paid for using
// this transit pass
int getNumTrips() {...}
}
public class TransitPass {
// REQUIRES: amount > 0
// MODIFIES: this
// EFFECTS: adds amount to the balance on this pass
void reload(int amount) {...}
// EFFECTS: returns balance on this pass
int getBalance() {...}
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip
void tapIn() {...}
}
public class TransitPassTripTracker
extends TransitPass {
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip and
// adds one to number of trips
// paid for with this pass
void tapIn() {...}
// EFFECTS: returns number of trips paid for using
// this transit pass
int getNumTrips() {...}
}
public class TransitPass {
// REQUIRES: amount > 0
// MODIFIES: this
// EFFECTS: adds amount to the balance on this pass
void reload(int amount) {...}
// EFFECTS: returns balance on this pass
int getBalance() {...}
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip
void tapIn() {...}
}
public class TransitPassTripTracker
extends TransitPass {
// REQUIRES: pass has sufficient balance to pay fare
// MODIFIES: this
// EFFECTS: taps in to pay fare for transit trip and
// adds one to number of trips
// paid for with this pass
void tapIn() {...}
// EFFECTS: returns number of trips paid for using
// this transit pass
int getNumTrips() {...}
}
protected
protected int age;
private < default < protected < public
= default + subclasses
Visibility
super
Used in Two Contexts
public Poodle() {
super(); // First statement!
//...
}
public int getAge() {
return super.age;
}
public int getAge() {
return super.getAge();
}
Call constructor of immediate superclass
Access fields/methods of a superclass
By Felix Grund