1 of 24

CISC 181

Inheritance

Prof. Christopher Rasmussen

March 20, 2015

2 of 24

Review: What's a Class?

  • Frogs and fish in both Animals and Food are examples of how classes can overlap
    • A thing may "belong" to multiple classes at once
  • Fish are a subset of Animals, Flowers a subset of Plants, Hats a subset of Clothing:
    • One class may completely be "contained" in another

3 of 24

What's a Class?

  • Hierarchy of specificity/generality

Fish

4 of 24

What's a Class?

  • Hierarchy of specificity/generality

Animal

Fish

5 of 24

The Idea of Related Classes

  • We know how to define a Fish class and an Animal class which are independent
  • But imagine all of the methods and fields which are common to them…
    • weight, length, bodyColor, etc.
    • move(), grow(), reproduce(), die(), etc.

6 of 24

The Idea of Related Classes

  • We know how to define a Fish class and an Animal class which are independent
  • But imagine all of the methods and fields which are common to them…
    • weight, length, bodyColor, etc.
    • move(), grow(), reproduce(), die(), etc.
  • Fish can just give different values to fields, but some methods & fields should be added to specialize certain characteristics/behaviors:

7 of 24

The Idea of Related Classes

  • We know how to define a Fish class and an Animal class which are independent
  • But imagine all of the methods and fields which are common to them…
    • weight, length, bodyColor, etc.
    • move(), grow(), reproduce(), die(), etc.
  • Fish can just give different values to fields, but some methods & fields should be added to specialize certain characteristics/behaviors:
    • move() swim()
    • reproduce() layEggs()
    • Add tailColor, numFins, etc.

8 of 24

Derived Classes

  • Object-oriented languages such as Java allow classes to be derived from each other
  • So we say the more specific Fish class is based on the more general Animal class, but it "extends" or "modifies" it in some way

public class Animal { … }

public class Fish extends Animal { … }

9 of 24

Derived Classes

  • Object-oriented languages such as Java allow classes to be derived from each other
  • So we say the more specific Fish class is based on the more general Animal class, but it "extends" or "modifies" it in some way

public class Animal { … }

public class Fish extends Animal { … }

public class Bird extends Animal { … }

10 of 24

Derived Classes: Terminology

  • It is possible to derive a class from a derived class:

public class Salmon extends Fish { … }

11 of 24

Derived Classes: Terminology

  • It is possible to derive a class from a derived class:

public class Salmon extends Fish { … }

  • Besides saying Fish is derived from Animal, we also can say:
    • Fish is a subclass of the Animal class
    • Animal is a superclass of Fish
    • Animal is the parent class of Fish
    • Animal is the base class for Fish, Bird, Salmon, etc.

12 of 24

Derived Class Facts

  • Every field and method in the base class is inherited by the derived class

public class Animal {

Color bodyColor;

}

public class Fish extends Animal {

Color tailColor;

}

myFish.bodyColor = Color.SILVER;

myFish.tailColor = Color.RED;

13 of 24

Derived Class Facts

  • Every field and method in the base class is inherited by the derived class

public class Animal {

Color bodyColor;

}

public class Fish extends Animal {

Color tailColor;

}

myFish.bodyColor = Color.SILVER;

myFish.tailColor = Color.RED;

myAnimal.tailColor = Color.BLUE;

14 of 24

Inheritance: Access

  • private methods and fields in the superclass are inaccessible to subclasses
  • The protected access specifier makes superclass members behave like they are...
    • public to their subclasses
    • private to everything else

15 of 24

Derived Classes: Overriding

  • Inherited methods may be overridden in order to specialize them
  • Example: Animal can define print() function...and so can derived class Fish
  • The derived version is called for Fish objects

16 of 24

The Object class

  • Object is the "ancestor" or ultimate base classes of all Java classes

17 of 24

The Object class

  • Object is the "ancestor" or ultimate base classes of all Java classes
  • It defines toString() and equals() in the most generic terms, which is why...
    • They exist at all
    • But they should be overridden by subclasses

18 of 24

Polymorphism

  • We know how to make an ArrayList of Animal objects or Fish objects

19 of 24

Polymorphism

  • We know how to make an ArrayList of Animal objects or Fish objects
  • But we can put Fish objects into an ArrayList of Animal objects and Java "knows" which is which

20 of 24

super

  • If this refers to the current object, super refers to the object's superclass

21 of 24

super

  • If this refers to the current object, super refers to the object's superclass
  • Like this, super can be used as an object name
    • This allows explicit calls to overridden superclass methods -- e.g., super.print() from Fish

22 of 24

Overriding with super

  • Suppose Animal's grow() method just adds weight. Salmon can override it like so:

void grow(double dw) {

super.grow(dw); // superclass's grow()

}

23 of 24

Overriding with super

  • Suppose Animal's grow() method just adds weight. Salmon can override it like so:

void grow(double dw) {

super.grow(dw); // superclass's grow()

if (weight >= WEIGHT_THRESH) {

bodyColor = Color.DARK_GRAY;

}

}

24 of 24

super

  • If this refers to the current object, super refers to the object's superclass
  • Like this, super can be used as an object name
    • This allows explicit calls to overridden superclass methods -- e.g., super.print() from Fish
  • It can also be used to call the superclass constructor
    • In this case, super() (or with arguments, depending on which superclass constructor) must be the first line of a constructor