If a subclass has the same method as declared in the parent class it is known as

Introduction

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:

  1. add new fields (variables)
  2. add new methods
  3. override existing (in base class) methods

The following two changes are NOT allowed:

  1. derived class cannot remove fields (variables)
  2. derived class cannot remove methods.

There are two forms of inheritance:

  1. inheritance of type: whereby the subclass acquires the type of the base class;
  2. inheritance of implementation: whereby the subclass acquires the implementation of the base class;

These two forms always occur together.

Implementation by Inheritance

The following picture demonstrates the stack implementation by inheritance

If a subclass has the same method as declared in the parent class it is known as

See Stack.java for a complete implementation of the Stack class.

super

Constructors 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 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.

protected

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.

Hierarchies

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.

Polymorphism

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");
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.

Can a subclass have a method with the same name?

If your subclass defines a method with the same name and signature as a method in its superclass, the method in the subclass overrides the one in the superclass. Thus, the subclass does not inherit the method from its superclass.

What is it called when two methods have the same name but different parameters?

Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.

Is also known as a parent class?

A base class is also called parent class or superclass.

When a method in a subclass overrides a method in superclass?

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.