How to use Java extends keyword (Inheritance)

As we discussed earlier in our previous post, a class can be a superclass or a subclass of another class. A subclass is a class that inherits the functionality of its superclass. The java extends keyword is used to define that a class is a subclass. Consider the example below.

Java extends Example

ClassA.java

package com.javapointers.javacore;

public class ClassA {

    public ClassA() {
        //this is the constructor of ClassA.
    }
    public void printMethod(){
        System.out.println("printMethod in class ClassA");
    }
}

ClassB.java

package com.javapointers.javacore;

public class ClassB extends ClassA {

    public ClassB() {
        //this is the constructor of ClassA.
    }

    public static void main(String args[]){
         ClassB myClass = new ClassB();
         myClass.printMethod(); //this is possible and will not cause error
    }

In our example, we have created two java classes in the same package. First is ClassA and the second is ClassB which has been extended from ClassA using the java extends keyword. By adding the extends keyword, we have defined that ClassB is now a subclass of ClassA, and the methods and fields of ClassA will be available to ClassB based on its modifier.

Looking at ClassB, it has the main method and it will be called when we run the program. Inside our main method, we have created a new instance of ClassB. After that, we have called the method printMethod that is written in ClassA. Is this possible? Yes it is since the printMethod was declared as public and thus, can be inherited. Here’s the access level table for your reference:

ModifiersClassPackageSubclassWorld
PublicYesYesYesYes
ProtectedYesYesYesNo
No ModifierYesYesNoNo
PrivateYesNoNoNo

ClassA has a method printMethod which has a public modifier. Based on the table above, methods or fields that has a public modifier is available in its own class, package, subclass, or in the world. It said it is available to subclass, then, therefore calling the method printMethod from its subclass is really possible.

What if printMethod is like this?

     
private method printMethod() {
    System.out.println("printMethod in class ClassA");
}

Calling this method from ClassB will cause an error. From the table above, a private modifier can only be accessed inside its own class. It can never be accessed even if it’s inside the same package, a subclass, or to the world.

Is subclass important?

If you are creating a class which has the same functionality like the other class, it may be better to just make it a subclass rather than creating a new class and re-write all the functionalities that can be found on the other class.

The superclass

A Java Superclass is the parent class. In our example, ClassB is a subclass of ClassA and therefore, ClassA is the superclass of ClassB. A class can only have one superclass:

public class ClassC extends ClassB, ClassA {
     //this is not possible and will give you an error.
     //a class can have only one super class.
}

But a class can have multiple subclasses:

public class ClassA {
    //this is the superclass of ClassB and ClassC
}

public class ClassB extends ClassA {
    //this is a subclass of ClassA
}

public class ClassC extends ClassA {
    //this is also a subclass of ClassA
}


Most of the projects in Java uses inheritance so practice yourself in creating inheritance by using java extends keyword. Next on our tutorial is the Nested Class in Java.

Share this tutorial!