by Dinesh Thakur Category: Introduction to Java

Defination : The objects, are the building blocks of the object oriented programming language. In some situations, a programer may use multiple objects belonging to different classes which may have some kind of relationship among themselves as they share some common features. Let us consider an example of a program containing objects of two different classes Car and Motorcycle for maintaining car and motor cycle information respectively. Both Car and Motorcycle classes exhibit some kind of relationship as both are a kind of vehicle and share some common features like speed, engine specification etc. To represent relationship between classes, we use the concept of Inheritance.

Inheritance is one of the compile-time mechanism in the Object Oriented Programming language like Java, that enables you to organize classes in a hierarchical form. Just like a child inherits the characteristics of his parents and add certain new characteristics of his own, in the similar way in Java, a programmer can write a new class that can inherit the accessible fields and methods of the existing class and may also add new fields and methods. The inclusion of members of the existing class in a new class so that they are accessible in the new class is known as class inheritance. The existing class that is being inherited is called the super class and the new class that inherits the functionality is called the subclass. In some other programming languages, a superclass can also be referred as a parent, base class or an ancestor and the subclass is referred as a child, derived class or a descendent.

The main advantage of inheritance is the reusability of the code. Inheritance allows well tested code to be reused and enable changes to be made once and have effect in all relevant places. Once the super class has been written, tested and debugged properly, its functionality can be reused by the subclasses rather than being rewritten from the scratch. The subclass inherits the members of the super class and can add members of its own. Thus, it reduces the amount of new code that must be designed, written and tested each time a program is developed. It also saves time and increases the reliability of the program. The benefit of reusability is more visible in developing complex projects involving many programmers. Without inheritance, programmers may spend most of their time in rewriting code they have written many times before. But using the concept of inheritance, same code can be written only once in the super class and can be reused by different programmers. In general, Inheritance provides the ability to extend an existing class to meet new requirements, without having to effect the original class in any way.

Implementing Inheritance

Inheriting a superclass gives the ability to a subclass to define only those aspects that differ from or extend the functionality of the superclass. The syntax for creating a subclass is simple. At the beginning of your class definition, use the extends keyword after the class name followed by the name class being extended (i.e.superclass ). A subclass can only inherit directly from one superclass.
Unlike C++, Java does not support multiple inheritance.

class SubClassName extends SuperClassName {
// Additional member declarations
}

Here, SubClassName is the name of the subclass and SuperClassName is the name of the superclass whose members are inherited by subclass. The extends keyword identifies that SuperClassName is the superclass for class SubClassName. This keyword establishes an inheritance relationship between the two classes. The body of the subclass contains the members declaration for all the members that are not common to that of the superclass.

The member that is inherited in the subclass is a full member of that class and is freely accessible to any method in the class. When object(s) of the subclass are instantiated, it will not only contain the members of its class but also contain all the inherited members of the superclass from which it is derived.

In order to understand how a subclass is derived from the superclass, let us consider a program having a superclass named Base that consists of a data field num1 and a method baseShow(). Suppose you want to extend the functionality of Base by adding new members. For
this, we create subclass named Derived which is derived from the Base. So it inherits all the accessible members of the Base. It also extends the functionality of the Base by adding its own field num2 and methods product() and derivedShow(). These members are exclusive to the subclass and cannot be used by the object of the superclass.

// show the use of inheritance

// super class

class Base {

  int num1;

  void baseShow() {

  System.out.println("num1 = "+num1);

}

//subclass of Base class

class Derived extends Base {

    int num2;

    void product() {

    System.out.println("Product = " + (num1 * num2));

}

  void derivedShow() {

      System.out.println("num2 = " +num2);

  }

}

public class inheritanceD {

   public static void main(String[] args) {

   Derived d = new Derived();

   d.num1 = 20;

   d.baseShow();

   d.num2 = 10;

   d.derivedShow();

   d.product();

 }

}

Here, class Base is the superclass and class Derived is its subclass. In the class header, 'Derived extends Base' means that the class Derived implicitly inherits the data fields and methods of class Base. In other words, we can say that class Derived inherits the field numl and the method baseShow () from the class Base. So these members are freely accessible to any method of the class Derived as if they are members of that class. This can be seen in the method product () of the class 'Derived' where numl field of class Base is referred directly.

In the main() method, when the Derived object is created, it can access both num1 and num2 fields as well as can call methods baseShow() , derivedShow() and product().

Inheritance and Access Specifiers

Inheritance is a mechanism of creating a new class from an existing class by inheriting the features of existing class and adding additional features of its own. When a class is derived from an existing class, all the members of the superclass are automatically inherited in the subclass. However, it is also possible to restrict access to fields and method of the superclass in the subclass. This is possible by applying the access specifiers to the member of the superclass. If you do not want a subclass to access a superclass member, give that member private access. The private members of the superclass remain private (accessible within the superclass only) in the superclass and hence are not accessible directly to the members of the subclass. However, the subclass can access them indirectly through the inherited accessible methods of the superclass.

The following kinds of inheritance are there in java.

1. Simple Inheritance

2. 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();
  }
  }





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.