• The extends keyword (Inheritance)

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

    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 class in the same package. First is ClassA and the second is ClassB which has been extended from ClassA using the extends keyword. By adding the extends keyword, we have define that ClassB is now a subclass of ClassA, and the methods and fields of ClassA will be available to ClassB based on its modifier.

    Lets look at ClassB. Class B has the main method and it will be called when running the program. In the main method, we have created a new instance of ClassB. After creating a new instance, we have called the method printMethod that is written in ClassA. Is this possible? Yes it is. Remember our table in Java Access Levels Chapter? Well here is again the table:

    Modifiers Class Package Subclass World
    Public Yes Yes Yes Yes
    Protected Yes Yes Yes No
    No Modifier Yes Yes No No
    Private Yes No No No

    Let look now at ClassA. 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 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. Again, from the table above, a private modifier can only be access inside its own class. It can never be accessed in package, subclass or to the world.

    Is subclass important?

    Well, if you are creating a class which has the same functionality like the other class, it maybe 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

    The superclass is the class that has been subclass. In our example, ClassB is a subclass of ClassA and therefore, ClassA is the superclass of ClassB. A class can only have one superclass. But a class can have multiple subclass. Like these:

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

    A class that has been subclasses multiple times.

    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
    }

    Related Post