Inheritance provided mechanism that allowed a class to inherit property of another class. When a Class extends another class it inherits all non-private members including fields and methods. When subclass object is created, a separate object of super class object will not be created. Only a subclass object is created that has super class variables. Inheritance defines is-a relationship between a Super class and its Subclass. extends and implements keywords are used to describe inheritance in Java.

super keyword is used to refer to immediate parent class of a child class.

class Parent
{
    String name;
}
public class Child extends Parent {
    String name;
    public void details()
    {
        super.name = "Parent";	    //refers to parent class member
        name = "Child";
        System.out.println(super.name+" and "+name);
    }
}

A sub-classes can inherit members as is, replace them, hide them, or supplement them with new members.

  • The inherited fields and methods can be used directly, just like any other fields.
  • We can declare new fields and methods in the subclass that are not in the superclass.
  • We can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
  • We can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding (Shadowing) it.
  • We can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Important Points for Inheritance

  1. Superclass constructors are not inherited by subclass.
  2. Superclass members with default access is accessible to subclass ONLY if they are in same package.
  3. We can create an instance of subclass and then assign it to superclass variable, this is called upcasting.
  4. We can call the superclass methods and access superclass variables using super keyword. It comes handy when we have a same name variable/method in the subclass but we want to access the superclass variable/method. This is also used when constructors are defined in the superclass and subclass and we have to explicitly call the superclass constructor.

Shadowing of static functions
In Java, if name of a derived class static function is same as base class static function then the derived class static function shadows (or conceals) the base class static function. For example, the following Java code prints “A fun”

class A {  
    static void fun() {  System.out.println("A fun");  } 
} 

class B extends A { 
   static void fun() {   System.out.println("B fun");  } 
} 

public class Main {  
  public static void main(String args[]) {  
        A a = new B();  
       a.fun(); // prints A fun, upcasting  
  } 
}

Overriding in Java
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Rules for method overriding

  1. The access modifier for an overriding method can allow more, but not less access than the overridden method. For example, a protected instance method in the super-class can be made public, but not private, in the subclass.
  2. If we don’t want a method to be overridden, we declare it as final.
  3. We can call parent class method in overriding method using super keyword.
  4. Abstract methods in an interface or abstract class are meant to be overridden in derived concrete classes otherwise compile-time error will be thrown.