Java Inheritance

Inheritance is one of the base of object-oriented programming because it allows the creation of hierarchical classifications.

With inheritance, you can create a general class that defines traits common to the set of related items. This class can then be inherited by the other, more specific classes, each adding those things that are unique to it.

In nomenclature of Java, a class which is inherited is known as a superlcass. The class that does the inheriting is called a subclass. Thus, a subclass is a specialized version of a superclass. It inherits all of the members defined by the superclass and then adds its own, unique elements.

Java Inheritance Example

To inherit a class, you incorporate the definition of one class into another by using the keyword extends. To see how, let's take a look at the following short example. Here this program creates a superclass named A and a subclass named B. Notice here that how the extends keyword is used to create a subclass of A.

/* Java Program Example - Java Inheritance
 * This is a simple example program of inheritance.
 */


/* create a superclass */
class A
{
    int i, j;
    
    void showij()
    {
        System.out.println("i and j : " + i + "  " + j);
    }
}

/* create a subclass by extending the class A */
class B extends A
{
    int k;
    
    void showk()
    {
        System.out.println("k : " + k);
    }
    
    void sum()
    {
        System.out.println("i+j+k = " + (i+j+k));
    }
}

class JavaProgram
{
    public static void main(String args[])
    {
        
        A superObj = new A();
        B subObj = new B();
        
        /* the superclass may be used by itself */
        superObj.i = 100;
        superObj.j = 200;
        System.out.println("Contents of superObj : ");
        superObj.showij();
        System.out.println();
        
        /* the subclass has access to all the public
         * members of its superclass.         */
         subObj.i = 70;
         subObj.j = 80;
         subObj.k = 90;
         System.out.println("Contents of subObj : ");
         subObj.showij();
         subObj.showk();
         System.out.println();
         
         System.out.println("Sum of i, j, and k in subObj : ");
         subObj.sum();

    }
}

When the above Java program is compile and executed, it will produce the following output:

java inheritance

As you can see that the subclass B includes all of the members of its superclass, A. This is why subObj can access i and j and call the showij() method. Also, inside sum(), i and j can be referred to directly, as if they were part of B.

Even though A is a superclass for B, it is also completely independent, stand-alone class. Being a superclass for a subclass doesn't mean that the superclass can't be used by itself. Further, a subclass can be a superclass for another subclass.

Here is the general form of class declaration which inherits a superclass :

class subclass-name extends superclass-name {
   // body of the class
}

You can only specify one superclass for any subclass which you create. Java does not support the inheritance of multiple superclasses into a single subclass. You can create a hierarchy of inheritance in which a subclass becomes a superclass of another subclass. However, no class can be a superclass of itself.

Java Online Test


« Previous Tutorial Next Tutorial »