Why Inheritance?

Thanoshan MV
4 min readApr 29, 2020
Photo by Michiel Leunens on Unsplash

We’re are using inheritance in our code frequently. Have you ever thought why are we using inheritance and the full reasons for it?

Through inheritance we can achieve three main goals:

  1. Keep common code (common states & behavior) in one class.
  2. Split different states & behavior into separate classes.
  3. Keep all of the objects in a single data structure.

Let’s say in a university, there are student and faculty members and they have some states and behaviors.

Figure 1: Class diagram shows how we implement two classes

It’s okay to have these two classes but this solution will not work indefinitely. It leads to inconsistency and we can’t store all objects in one data structure.

a. inconsistency

Let’s assume that storing name is not enough, we have to store last name and first name also. According to figure 1, we have to change all instances where we used name in first place to first and last names. This is tedious, time consuming and hard to keep code consistent.

b. No clean way of storing objects

We can store both students and professors in separate data structure.

Student students[];
Professor professors[];

When someone says to sort the order by the date of students and professors has registered to the university, we have to focus on two data structures which is not efficient.

The easy way is storing all of them inside Person persons[];

Solution is obviously the inheritance!

Figure2: class diagram shows the inheritance relationship

In Person class we have put common code and in Student, Professor class we have put diverging code.

We can keep all of the objects in one data structure.

Person[] p = new Person[3];
p[0] = new Person();
p[1] = new Student();
p[3] = new Professor();

What are inherited?

Visibility/access modifiers impact what gets inherited from one class to another.

In Java, as a rule of thumb we make instance variables as private and instance methods as public .

In this case, we can safely say that what are inherited:

  1. public instance methods.
  2. private instance variables (private instance variables can be accessed only through public methods- getter, setter methods).

NOTE: Use private access modifier only to helper methods other than that always use public .

In other occasions, for example, if you use other access modifiers such as protected or default, as I have already said all instance variables and instance methods are inherited but you should look after for their visibility.

Again this is to note, we should always apply Java rule of thumb: always use private and public

You need not to worry about inheritance in non-access modifiers such as abstract , final and so on. They all will be inherited.

NOTE: static methods and static variables are not inherited as they are associated with class.

For example:

class Animal {
/**
* static members
*/
public static String publicstaticName = "publicstaticName";
public static void run(){
System.out.println("Running static method");
}
}
public class Dog extends Animal{
public static void main(String[]args){
Dog d1 = new Dog();
/**
*
Accessing static variable and
*
static method via class instance
*/
System.out.println(d1.publicstaticName);
d1.run();
}
}

Output:

publicstaticName
Running static method

The above code executes just fine but with warnings.

warning message: Shows references to static methods and fields via class instance rather than a class itself

The compiler puts the class name instead of the object name for you.

Here’s the correct way of accessing static members:

class Animal {
/**
* static members
*/
public static String publicstaticName = "publicstaticName";
public static void run(){
System.out.println("Running static method");
}
}
public class Dog {
public static void main(String[]args){
/**
*
Accessing static variable and
*
static method via class
*/
System.out.println(Animal.publicstaticName);
Animal.run();
}
}

Output:

publicstaticName
Running static method

This time the code gets executed with no errors. As you see above code, we don’t have to use inheritance to access static members.

So static members are not inherited. We access them by their class names.

Wrap up

Using inheritance we can achieve:

  1. Keep common code (common states & behavior) in one class.
  2. Split different states & behavior into separate classes.
  3. Keep all of the objects in a single data structure.

Please feel free to let me know if you have any doubts.

Thank you for reading!

--

--