by Dinesh Thakur

Inheritance is a compile-time mechanism in Java that allows you to extend a class (called the base class or superclass) with another class (called the derived class or subclass). In Java,inheritance is used for two purposes:

 

1. class inheritance - create a new class as an extension of another class, primarily for the purpose of code reuse. That is, the derived class inherits the public methods and public data of the base class. Java only allows a class to have one immediate base class, i.e., single class inheritance.

 

2. interface inheritance - create a new class to implement the methods defined as part of an interface for the purpose of subtyping. That is a class that implements an interface “conforms to” (or is constrained by the type of) the interface. Java supports multiple interface inheritance. In Java, these two kinds of inheritance are made distinct by using different language syntax. For class inheritance, Java uses the keyword extends and for interface inheritance Java uses the keyword implements.

 

public class derived-class-name extends base-class-name {

}

public class class-name implements interface-name {

 

What is not possible using java class Inheritance?

 

1.                  Private members of the superclass are not inherited by the subclass and can only be indirectly accessed.

 

2. Members that have default accessibility in the superclass are also not inherited by subclasses in other packages, as these members are only accessible by their simple names in subclasses within the same package as the superclass.

 

3. Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.

 

4. A subclass can extend only one superclass

 

The following kinds of inheritance are there in java.

 

      Simple Inheritance

  •   Multilevel Inheritance
  •  

Simple Inheritance

 

When a  subclass is derived simply from it's parent class then this mechanism is known as simple inheritance. In case of simple inheritance there is only a sub class and it's parent class. It is also called single inheritance or one level inheritance.   

 

class A {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}

class extends A{
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
  void display(){
  System.out.println("B");
  }
}

Multilevel Inheritance

It is the enhancement of the concept of inheritance. When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritance. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above ( parent ) class.  Multilevel inheritance can go up to any number of level.

 

class A {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}
class extends A{
  void Showb(){
  System.out.println("B");
  }
}

class extends B{
  void display(){
  System.out.println("C");
  }
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
}

 

Multiple Inheritance

 

The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface.

 

In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.

 

super keyword

 

The super is java keyword. As the name suggest super is used to access the members of the super class.It is used for two purposes in java.

 

 The first use of keyword super is to access the hidden data variables of the super class hidden by the sub class.

 

e.g. Suppose class A is the super class that has two instance variables as  int a and float b. class B is the subclass that also contains its own data members named a and b. then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command.

 

super.member;

 

Here member can either be an instance variable or a method. This form of super most useful to handle situations where the local members of a subclass hides the members of asuper class having the same name. The following example clarify all the confusions. 

 

class A{
  int a;
  float b;
  void Show(){
  System.out.println("b in super class:  " + b);
  }

}

class extends A{
  int a; 
  float b;
  B( int p, float q){
  a = p;
  super.b = q;
  }
  void Show(){
  super.Show();
  System.out.println("b in super class:  " + super.b);
  System.out.println("a in sub class:  " + a);
  }

  public static void main(String[] args){
  B subobj = new B(1, 5);
  subobj.Show();
  }
}

 

Use of super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass. This functionality can be achieved just by using the following command.

 

super(param-list);

 

Here parameter list is the list of the parameter requires by the constructor in the super class. super must be the first statement executed inside a super class constructor. If we want to call the default constructor then we pass the empty parameter list. The following program illustrates the use of the super keyword to call a super class constructor. 

 

c=r;
  }
}
  
  class extends A{
  int d;
  B(int l, int m, int n, int o){
  super(l,m,n);
  d=o;
  }
  void Show(){
  System.out.println("a = " + a);
  System.out.println("b = " + b);
  System.out.println("c = " + c);
  System.out.println("d = " + d);
  }

  public static void main(String args[]){
  B b = new B(4,3,8,7);
  b.Show();
  }
  }