For the reusability of existing classes, inheritance came into use, and java supports only two types of inheritance, in which a class extends another class.
- 1.Single Inheritance
- 2.Multilevel Inheritance
In Java, a class can be derived from another class using the general form:
access_specifier class Subclass extends Superclass
{
//Derived class data and methods
}
Various types of inheritance are discussed in the section below.
Single Inheritance
In this type of inheritance, a single sub class is derived from a single base class.
Multiple Inheritances
In multiple inheritances, there is more than one base class from which the child class is derived. This results in the duplication of data with the derived class. This is a major problem with OOP and a mechanism called Virtual Base Class is provided for this, in which, one of the base classes is made virtual to avoid the duplicate inheritance of data.
Java does not directly support multiple inheritances. This is because different classes may have different variables with same name that may be contradicted and can cause confusions in JVM, thus resulting in errors. Java only supports multiple inheritances when used with Interfaces. We can extend one class only to avoid ambiguity problem with JVM. In interface we have to define the functions. So there is no ambiguity. In C++, it is big problem but in JAVA this issue has been improved by introducing Interfaces.
Multilevel Inheritance
In multilevel inheritance, a class derives another class, and from the derived class, another sub class is derived.
Hybrid Inheritance
There may occur situations where we need to apply two or more types of inheritance in the design of a program. For such cases, hybrid inheritance is used.
Multiple inheritances cannot be possible by extending more than one class, it can be possible with one another and important features of the Java, called interface.
To inherit a class into another class extends keyword is used.
class subclassName extends superclassName
{
//body of subclass
}
The most important point in inheritance is that, the subclass object will be created and all the accessible members of superclass and its own are accessed by subclass object.
Example:
// Create a superclass
class SuperClass
{
int a,b;
public void storeab()
{
a = 41;
b = 41;
}
public void showab()
{
System.out.println(“SuperClass contents :: a = “+a+” b = “+b);
}
}
//Create a sub class by extending class SuperClass
class SubClass extends SuperClass
{
int s;
public void sumab ()
{
s=a+b;
}
public void shows()
{
System.out.println(“SubClass contents:: s = “+s) ;
}
}
public class SingleInheritance
{
public static void main(String arg[])
{
SubClass ob = new SubClass();
ob.storeab ();
ob.sumab();
ob.showab();
ob.shows();
}
}
Output:
In the give example, the object of the class SubClass, ob, is used to call the public member of it’s superclass as well as it’s own. So that we can observe, that the public member of SuperClass are inherited in the SubClass, i.e. become the part of it, so that using ob, we’re able to call them or access them. In the method sumab() of SubClass, we are directly using the a,b of SuperClass without using any instance of SuperClass, because these are inherited in the Subclass. The fields are without any access modifier, count under the category of default access modifier, and methods are defined as public.