Which keyword is used to call the base parent class functions from the child functions?
IntroductionInheritance 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. Implementation by InheritanceThe following picture demonstrates the stack implementation by inheritance See Stack.java for a complete implementation of the Stack class. superConstructors are not inherited in a derived class. Therefore, a child's constructor is responsible for calling the parent's constructor:public Stack() { super(); } By keyword If the child class constructor does not call 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 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: A child class can override any public parent's method that is not defined with the protectedThe 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 HierarchiesMultiple 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. PolymorphismThe 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");This will print 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");is incorrect - a variable can hold a reference to an object who's class is a descendant of the class of the variable. |