Simple Java inheritance example

0
330

Inheritance is one of the most powerful feature of OOP. It provides us code reusability and polymorphism. The concept is very simple and similar to real life.

It allows new classes to absorb all non-private features and functionality of the existing classes, moreover; inheritance enhance new classes by adding their own new features and functionality.

When inheritance is in progress, we define a parent class (Super class). A super class has general features and functionalities. Finally we make another child class (sub class) with specific features.

Inheritance defines “is-a” relationship between super class and its sub classes. We use “extends” and “implements” keywords to apply inheritance concept in Java i.e.

class X // parent class
{
   public void methodX()
   {
     System.out.println("Class X method");
   }
}
class Y extends X // child class
{
	public void methodY()
{
System.out.println("class Y method");
}
}
class Z extends Y // child class
{
   public void methodZ()
   {
     System.out.println("class Z method");
   }
}


In simple words, Inheritance is an ability to obtain the same functionality with the existing classes.

But remember java allows single inheritance i.e. all sub classes can have only one super class. We use different technique to obtain multiple inheritance functionality.

Leave a Reply