Inheritance in Java

1.5K views 7 minutes read
A+A-
Reset

Inheritance in Java:

Inheritance is the most used buzzword in java programming language. In previous posts we have understood about “what is class and how to use class and objects?”. You of class must have good concept about class and objects before proceeding about Inheritance. If you are beginner and don’t’have good concept but you have been through previous topic, don’t worry after reading this post you will have more clear idea about all these stuff. Let’s start Inheritance in Java topic hereby.

What is Inheritance?

Inheritance is concept of object oriented programming in which we can inherit the properties of one object to another object. Inheritance is a mechanism by which all the fields and methods of class can be accessed inside the particular class.  In this concept if a class B inherit class A then object of B can possess all the properties of class B as well as class A. In this case class A is called super class and class B is called sub class.

In this way we can create hierarchy of classes in which super class are above sub-classes. When we deal with lot of classes in a project then creation of class hierarchy using inheritance is crucial. Inheritance in Java

 

What is sub Class?

A class which inherits any other class is sub class. In Sub class we do not define properties which are defined in super class since all super class properties are imported by inheritance. Let’s understand by real world example. Let’s suppose we have a class named Animal to represent any animal. Then a class Dog can inherit from class Animal. Here Dog is the sub class. Sub-classes lies in lower levels in class hierarchy.

What is super class?

A class which is inherited by some other class is super class. Super class and its object are not affect by inheritance. Super classes are in higher levels in the class hierarchy.

Inheritance in Java

How to write code for inheritance?

To see by code how Inheritance in Java works, we must have at least two classes. First of all we will create a super class. Any class can be super class. There is no specification for super class but keep in mind class defined with keyword final cannot be used as super class. Final class cannot be inherited in java. For example String is final class so we cannot inherit this class by any other class.

Then we have to define sub class. The only code you have to write for inheritance is keyword extends.

Remember Keyword extends is used to inherit properties from super class to sub class.

Lets have a look at following code.

Consider a class Employee.

package myPackage;
class Employee{
   Employee(){
      System.out.println("Employee constructor"); 
   }
   String name;
   int id;
   void getId(){
      System.out.println("Employee id is "+id);
   }
}

Now inherit Employee class in another class Manager.

package myPackage;
class Manager extends Employee{
   Manager(){ 
      System.out.println("Manager constructor"); 
   }
   String department;
   void getDetails(){
      System.out.println("Manager "+name +" is in "+department +" department.");
   }
}

Look at main class

package myPackage;
class MainClass{
   public static void main(String args[]){
      Manager m=new Manager();
      m.id=1234;         /**************************/
      m.name="Sanam";    ** All the fields and     ** 
      m.getId();         ** methods from Employee  **
      m.getDetails();    ** and Manager are used   **
}                        ** by Manger Object       **
}                        /**************************/

What output are you expecting? I let you imagine the output. Scroll down the bottom of post for output.

Order of execution of constructors in inheritance class hierarchy

As we have discussed in last post while creating object constructor of corresponding class is invoked automatically. Similarly if your class extends any other class and you are creating object of your class, then constructor of the super-class will be executed first then only constructor of sub class is invoked. While creating objects jvm will look for corresponding class and find out its super class. If the super class again have any super class it will go to that super class and call constructor and move down in the class hierarchy step by step. This seems complicated in theory but it is very simple as we see in above example.

The main fact behind this is compiler automatically add super(); statement as the first statement of the constructor where super(); cause the super class constructor invocation.

Advantage of Inheritance

If you have already created a class that contains some of the feature that you want to include in the class, Then you need not to write code in this class. You can just extend this class to previously defined class. Inheritance in Java lead to code re-usability.

This feature of OOPs also helps in better mapping of real world scenario in computer programming.

If you add a class as subclass of any existing class then there is no need of compiling existing code again. Only new class needs compilation.

What is multiple Inheritance?          Multiple Inheritance

The case when a single class is inherited from two or more super class then it is called Multiple Inheritance. Multiple inheritance allows many super class of a single sub class. In java Multiple Inheritance is not allowed. Java allows only one single super class for any class.

Multiple Inheritance is not there in java good or bad?

Java lovers say it is very much good to not having Multiple Inheritance in Java. This avoids extra complexity of class and hence makes java a simple programming language.

Consequently it also quit the ability of making a class with all imaginable behavior.  Creation of very much versatile class seems bound by the absence of multiple inheritance. So you can assume this as bad aspect of language depending on your view.

But I personally find it good to not having that complex feature.

 

Output:

Employee constructor
Manager constructor
Employee id is 1234
Manager Sanam is in Accounts department.

If this output is as your expectation, that means you getting right things in right way.

I think that’s all about inheritance. There are many concepts remained like how abstract classes are inherited. Next post will cover much remaining concepts.

Type comment if you get any bug or if you have any query.

Related Posts

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.