Inheritance is the act of deriving a new class from an existing class. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods and variables of the original class. The new derived class is called a child class, or subclass. The original class is called parent class, or superclass, or base class. Since the derived class is always more specific, the relation between these classes is called is-a relation. Class extension can be used for a number of purposes. The most common use is specialization - where the extended class defines new behaviors and thus becomes a specialized version of its superclass. Consider two classes Book and Dictionary. Which one is more general? Book, since a dictionary is a specific kind of a book. Therefore, we say that Dictionary extends Book.
public class Dictionary extends Book { }
All public methods declared in Book available in Dictionary, but not vice versa: inheritance is a one-way street. Inheritance allows the following three changes in derived class:
The following two changes are NOT allowed:
There are two forms of inheritance:
These two forms always occur together.
The following picture demonstrates the stack implementation by inheritance
See Stack.java for a complete implementation of the Stack class.
public Stack() { super(); }
By keyword super
Java provides a mechanism to call parent's constructor. The above super()
is actually a call for ArrayList's constructor. The super must be used in the first line (literally) of a constructor.
If the child class constructor does not call super
, the parent's constructor with no
arguments will be implicitly called. If parent class implements a constructor with arguments and has no a constructor with no arguments, then the child constructors must explicitly call a parents constructor.
The super reference can also be used to invoke other methods in parent's class. How would you call the ArrayList's toString method from inside the Stack's toString?. Since both methods have the same signature, we say that Stacks's toString overrides Book's toString(). We will distinguish between them using this
and super
keywords. Consider the following code example and trace new B().m1()
call:
public class Demo { public static void main(String[] args) { System.out.println( new B().m1() ); } } class A { int m1() {return m3();} int m2() {return this.m3();} int m3() {return 1;} } class B extends A { int m1() {return super.m1();} int m3() {return 4;} int m4() {return m2();} }
Here is a trace: B.m1() -> A.m1() -> B.m3() -> 4
.
A child class can override any public parent's method that is not defined with the final
modifier. Also, classes with final
modifier cannot be extended. The Java's String and StringBuffer classes are examples of such classes.
The derived class inherits all variables and methods, which are public. It does not inherit those
variables and methods, which are private. However, we do not want to declare all variables and methods to be public, since they will be visible for everybody. Therefore, Java offers another modifier called protected
. Protected variables are not accessible for other classes, but only for derived classes.
Multiple classes can be derived from a single parent. There is no limit to the number of children a class can have (but a child can have only one parent). Two children of the same parent are called siblings. Siblings are NOT related to each other by inheritance. Inheritance is transitive. All methods and fields are passed from a parent to the children and then from children to their children and further. There is no single best hierarchy, the decision is made when you design your classes.
The term polymorphism can be defined as having many forms. A polymorphic reference is one that refer to different types of the objects. Polymorphism allows programs to be written more abstractly and therefore less redundantly. Polymorphism is implemented in Java using a technique called dynamic binding - where exact types are determined during the program execution.
Consider a collection of greeting cards: the base class is Card which has a subclass Holiday which has a subclass ApprilFool:
public class Card { String toWhom; public Card(String name) { toWhom = name; System.out.println("\nDear " + toWhom + "!"); } } public class Holiday extends Card { public Holiday(String name) { super(name); System.out.println("Season's Greetings!"); } } public class AprilFool extends Holiday { public AprilFool(String name) { super(name); System.out.println("Just Kidding!"); } }
Next we declare a reference of the type Card.
Card myCard;
myCard is a polymorphic reference that can have any of three types: Card, Holiday or AprilFool.:
myCard = new Card("Charlie"); myCard = new Card("Caitlin"); myCard = new Card("Alex");
Dear Charlie! Dear Caitlin! Season's Greetings! Dear Alex! Season's Greetings! Just Kidding!
It's important to note that the declaration
Holiday hol = new Card("fill the blank");