Seeing the workflow of Class Inheritance in Java


When it comes to Inheritances, that means we try using code reuse. In Java, Python, C++, Inheritance is a common thing since we want to reduce duplicate code and manage them easily. In Java, we can create a subclass by inheriting from parent using “extends”. Here is some trivial example:

InheritanceImplementation.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.lang.String;

class Bank {
    private int id;
    private String type;
   
    public Bank() {
        System.out.println("Creating Bank… ");
    }
}

public class InheritanceImplementation {
    public static void main(String[] args) {
        System.out.println("Execute main programm… ");
        Bank bank = new Bank();
        System.out.println("Bank object created… ");

    }
}

We create a new “parent” class called “Bank” with constructor that showing information when this class instantiated. Then inside the main(), we put information about when this main executed and when the object created. The results as we predicted is :

1
2
3
Execute main programm…
Creating Bank…
Bank object created…

Yes, now we know how to inherit class or made subclass. The example we use here is single-level inheritance. We can use multi-level inheritance in Java. The question is, how Java handle this inheritance from multiple level ? Is that from child go to the parent? or Java lookup into the parent then start drill down to the last child ? We can create multi-level inheritance like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.lang.String;

class Bank {
    private int id;
    private String type;
   
    public Bank() {
        System.out.println("Creating Bank… ");
    }
}

class Employee extends Bank {
    private String name;
    private String address;
    private int rank;
   
    public Employee() {
        System.out.println("Creating Employee … ");
    }
}

class Task extends Employee {
    public Task() {
        System.out.println("Creating Task …");
    }
}


public class InheritanceImplementation {
    public static void main(String[] args) {
        System.out.println("Main executed ….");
        Task task = new Task();
        System.out.println("Taks object created ….");    
    }
}

What’s is the result? As we predicted again:

1
2
3
4
Creating Bank…
Creating Employee …
Creating Task …
Taks object created ….

Yes, Java runtime first caling the Bank constructor, followed by Employee and Task in last step. The next question is how Java runtime climb into parent class then start to execute constructor then followed by object created on the last step?

The secret is there is a little magic inside Java run-time. Once you using inheritance, that’s mean Java will looking at the constructor and execute super() at the first-line in Class constructor. Like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.lang.String;

class Employee extends Bank {
    ….
    public Employee() {
                super();
        System.out.println("Creating Employee … ");
    }
}

class Task extends Employee {
    public Task() {
                super();
        System.out.println("Creating Task …");
    }
}

What this super() for? this “super” is have function to call another constructor which in this case is calling the constructor’s parent. No wonder the Bank’s constructor called first by Java run-time, because each constructor calling super() which mean going up to the top parent class.

Now, what if we want to protect some class and preventing it can be subclassed? Then, “final” is come and save us. At this example, we only allow Bank -> Employee inheritance, meanwhile it just stop there which that mean Task() should be disallowed from inheriting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.lang.String;

class Bank {
    private int id;
    private String type;
   
    public Bank() {
        System.out.println("Creating Bank… ");
    }
}

final class Employee extends Bank {
    private String name;
    private String address;
    private int rank;
   
    public Employee() {
        System.out.println("Creating Employee … ");
    }
}

class Task extends Employee {
    public Task() {
        System.out.println("Creating Task …");
    }
}


public class InheritanceImplementation {
    public static void main(String[] args) {
        System.out.println("Main executed ….");
        Task task = new Task();
        System.out.println("Taks object created ….");    
    }
}

An this is the Java-runtime results:

1
2
3
4
5
6
Main executed ….
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The type Task cannot subclass the final class Employee

    at Task.<init>(InheritanceImplementation.java:22)
    at InheritanceImplementation.main(InheritanceImplementation.java:32)

Is obviously clear that Task() not allowed to subclassing Employee. Then we can protect and manage base and child class inheritance level. Playing inheritance in Java is fun! 😀


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.