A6: Inheritance
Learning Goals
-
To use inheritance to form type hierarchies
- These allow us to record the similarity between types
-
To use inheritance to reuse code
- Between two different implementations
- To use inheritance to refine or override code
Inheritance
- A class can be declared to extend exactly one other class
public class Poodle extends Dog {
}
Becomes a Subtype
Becomes a Supertype
-
Supertype can be substituted with any of its subtypes
- Just like with interfaces
Pod Activity (Do in Groups of 3-5)
- What is the difference between implements and extends ?
- Why do we need both?
- In Java, can you implements multiple times?
- In Java, can you extends multiple times?
Why Inheritance?
- Remember: object-orientation models the real world!
...
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");
}
}
Implementing
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(); // ???
Examples
Keywords
super
protected
method override
supertype
subtype
declared type
actual type
method dispatch
method overload
= apparent type
= instantiated type
Lecture Ticket Review (1)
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() {...}
}
Lecture Ticket Review (2)
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() {...}
}
Lecture Ticket Review (3)
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() {...}
}
Lecture Ticket Review (4)
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() {...}
}
Lecture Ticket Review (5)
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
Lecture Lab
A6: Inheritance
The End - Thank You!
Copy of CPSC210 - A6 Inheritance
By firas_moosvi
Copy of CPSC210 - A6 Inheritance
- 110