Core Java - SPLessons

Inheritance in Java

Chapter 15

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Inheritance in Java

Inheritance in Java

shape Description

Inheritance in Java is the concept to hire the properties from base class, inheritance is the Java oops concept and one of the excellent feature of Java, but Inheritance in Java does not support multiple inheritance concept why because it leads to the confusion. Inheritance follows the IS-A relationship. Following is the syntax for the Inheritance in Java.

Achieving multiple inheritance by using interface.

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

Where extends is the keyword to extend the base class.


Single Inheritence

shape Description

Inheritance in Java concept, Single inheritance is very simple to understand when one class is extending another class is known as Single inheritance. Following is an example which describes single inheritance.Following is an example to understand single inheritance.
Class A
{
   public void methodA()//super class method.
   {
     System.out.println("Base class method");
   }
}

Class B extends A //here B is extending the class A so all the properties of class A will be hired by class B.
{
   public void methodB()//local method.
   {
     System.out.println("Child class method");
   }
   public static void main(String args[])
   {
     B obj = new B();
     obj.methodA(); //calling super class method
     obj.methodB(); //calling local method
  }
}

Output

Base class method
Child class method

Multilevel Inheritance

shape Description

In Inheritance in Java, multilevel inheritance will follow the Object Oriented technology where users can inherit properties from derived class and thereby making this derived class the base class of the new class. Following figure will explain the overall concept.
Where class C is the child class of class B and class B is the child class of class A, here class C has all the rights to hire the properties of class A and class B, this is called as multilevel inheritance. Following is an example.
Maruti800.java


	class Car{
		public Car()
		{
			System.out.println("Class Car");
		}
		public void vehicleType()
		{
			System.out.println("Vehicle Type: Car");
		}
	}
	class Maruti extends Car{
		public Maruti()
		{
			System.out.println("Class Maruti");
		}
		public void brand()
		{
			System.out.println("Brand: Maruti");
		}
		public void speed()
		{
			System.out.println("Max: 90Kmph");
		}
	}
	public class Maruti800 extends Maruti{

		 public Maruti800()
		 {
			 System.out.println("Maruti Model: 800");
		 }
		 public void speed()
			{
				System.out.println("Max: 80Kmph");
			}
		 public static void main(String args[])
		 {
			 Maruti800 obj=new Maruti800();
			 obj.vehicleType();
			 obj.brand();
			 obj.speed();
		 }
	}

Here Maruti800 can hire the properties from above classes and object is also created in this class only.
Output
When compile the code following is the output will be generated.

Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph

Hierarchical Inheritance

shape Description

In Inheritance in Java, If more child classes hire the properties of one super class is known as hierarchical inheritance. Following is the conceptual figure which explains the concept of hierarchical inheritance.

As explained in the above figure Class B and Class C are hiring the properties of Class A. Following is an example which describes more about the concept.
MainClass.java

package corejava;

class HierarchicalInheritance { 
	void DisplayA() { 
		System.out.println("This is a content of parent class"); 
	} 
} 

//B.java 
class A extends HierarchicalInheritance { 
	void DisplayB() { 
		System.out.println("This is a content of child class 1"); 
	} 
} 

//c.java 
class B extends HierarchicalInheritance { 
	void DisplayC() { 
		System.out.println("This is a content of child class 2"); 
	} 
} 

//MainClass.java 
class MainClass { 
	public static void main(String args[]) { 
		System.out.println("Calling for child class C"); 
		B b = new B(); 
		b.DisplayA(); 
		b.DisplayC(); 
		System.out.println("Calling for child class B"); 
		A a = new A(); 
		a.DisplayA();
		a.DisplayB();
	} 
}

As explained in the above program Class A and Class B are taking the ownership from HierarchicalInheritance.
Output
When compile the code following is the output will be generated.

Calling for child class C
This is a content of parent class
This is a content of child class 2
Calling for child class B
This is a content of parent class
This is a content of child class 1

Multiple Inheritance

shape Description

Inheritance in Java, The interesting point in Java is that it does not support multiple inheritance concept, why because it leads to confusion, it will be overcome with the interface concept. Following is an example which describes more about this concept.
As explained in the above image Class A is hiring the properties from Class B and Class C, while executing a method from both the classes dependency problem will be occurred, this is the reason Java does not support multiple inheritance. The interface concept is there in java to achieve the multiple inheritance concept as follows.

package com.splessons;

interface Printable{  
void print();  
}  
interface Showable{  
void show();  
}  
class Splesson implements Printable,Showable{  
public void print(){System.out.println("Hello");}  
public void show(){System.out.println("Welcome To Splessons");}  
  
public static void main(String args[]){  
Splesson obj = new Splesson();  
obj.print();  
obj.show();  
 }  
}  

In the above example, Printable and Showable are the two interfaces that have their own methods and where Splesson is the class name that implements two interfaces(there is no ambiguity).
Output:

Hello
Welcome To Splessons

Hybrid Inheritance

shape Description

Inheritance in Java concept, Hybrid inheritance is a combination of single inheritance and multiple inheritance. Java does not support hybrid inheritance with classes, it supports while using interfaces.Following is the conceptual figure.

Summary

shape Key Points

  • Inheritance in Java is the oops concept.
  • Java does not support multiple inheritance.
  • Hybrid inheritance and multiple inheritance problems will be overcome by using interfaces.