A6: Inheritance

Learning Goals

  • To use inheritance to form type hierarchies
    • These allow us to record 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

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

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() {...}
}

And Part 3, 4, 5

protected
protected int age;
private < default < protected < public

= private + 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

Keywords

super
protected
method override
supertype
subtype
declared type
actual type
method dispatch
method overload

= apparent type

= instantiated type

Lecture Lab

A6: Inheritance

The End - Thank You!

Made with Slides.com