Inheritence and its types in Java

Nov 23 • Notes • 1136 Views • Comments Off on Inheritence and its types in Java

Java is a programming language which is mainly based on OOPs concept.

Now what is OOPs?

Well, OOPs stands for Object Oriented Programming. It is concept which links our programs to real world models. This OOP concept has four main features – 

1.     Abstraction
2.     Encapsulation
3.     Inheritence
4.     Polymorphism

Among the above four, Inheritence is one of the important feature of OOP. And as Java is based on OOP, so it is also an important feature of Java.

The main significance of Inheritence is that it makes the programming language more and more flexible and easy to code, understand and debug.

Now, what actually is Inheritence!

Well, the ability to inherit the features and attributes is known as Inheritence. If we look at the real life example, a boy / girl inherits features from their parents. Like wise here also one class can inherit feature from another existing class as Java is fully class based. It increases the reusability feature. So we have named it in the same way, i.e., the class which is inheriting features from another class is named as Child Class (Sub Class). 

Now the inheritence feature is brought in action by the keyword “extends” while programming. Now in case the Parent Class is fully empty, means if the Parent Class is having functions in it but none of the functions are defined, then that Parent Class is generally called an “Interface“. So in such case the keyword used is “implements“.

Syntax:

class X                               // Parent Class or Super Class

{

}

class Y extends (or implements) X      // Child Class or Sub Class

{

}

Now let us discuss about the types of Inhertence in java which are as follows:

  1. Single level interface:-  In this type ,the child class inherits the parent class attributes  by using extend keyword.
  2. Heirarchial interface:- In this type , a heirarchy is followed to inherit the features.
  3. Multiple Interface:- In java there is no muliple inheritence between classes i.e., no class can extend two classes.But, Multiple inheritence is possible with the help of interfaces.The keyword implement is used to inherit features of an interface.
  4. Multilevel inheritence:- In this type ,the no. of levels is more than one i.e., multiple levels     are used.

This example may clear all your remaining doubts.

Example:

class A

{

public static void main(String args[])

{

int add(int a,int b)

{

int c=a+b;

return c;

}

}

}

class B extends A

{

public static void main(String args[])

{

add(10,20);                         //method of Parent Class is used.

}

}

I think I was able to clear this important topic of Java. Then too if you have any query or suggestion, you may share it with us in the comment box below.

Comments are closed.

« »