Inheritance in Java

Inheritance in java is a feature where child object acquires all the properties and behaviors of parent object.

In java child acquires all properties of parent unlike in real life child usually acquires few properties of parent (sometime nothing).

The idea behind inheritance in java is the Reusability.

When we have a class which has some attributes and methods, and if another class also needs similar attributes and methods along with its own methods, then why should we write a class by repeating already existing attributes and methods?

Will it not be good if we reuse those from the existing class and define extra things in the new class?

Yes that’s where the Inheritance comes into the picture.
Syntax to achieve inheritance in Java is as below

1
2
3
4
class Subclass-name extends Superclass-name  
{  
   // fields  and  methods 
} 
class Subclass-name extends Superclass-name  
{  
   // fields  and  methods 
} 

Now we will see the example below

1
2
3
4
5
6
7
package com.kb.inheritance;
 
class Employee{
    
     String empId="abc123";
     String name="kb";
}
package com.kb.inheritance;

class Employee{
	
	 String empId="abc123";
	 String name="kb";
}


1
2
3
4
5
6
7
8
9
10
11
12
package com.kb.inheritance;
public class Developer extends Employee {
private float bonus=9000;
 
public static void main(String[] args) {
    Developer dev = new Developer();
    System.out.println("emp id is "+ dev.empId);
    System.out.println("name is "+dev.name);
    System.out.println("bonus is "+dev.bonus);
}
 
}
package com.kb.inheritance;
public class Developer extends Employee {
private float bonus=9000;

public static void main(String[] args) {
	Developer dev = new Developer();
	System.out.println("emp id is "+ dev.empId);
	System.out.println("name is "+dev.name);
	System.out.println("bonus is "+dev.bonus);
}

}

extends is a keyword in java to achieve inheritance.
Here Developer class extends Employee class and hence Employee is super class, Developer is subclass.

Inheritance represents the IS-A relationship, also known as Parent-Child relationship.
So we can say Developer IS-A Employee.

Types Of Inheritance

1) Single Inheritance

2)Hierarchical Inheritance

3)Multilevel Inheritance

4)Multiple Inheritance

5)Hybrid(Diamond Shape) Inheritance

Multiple Inheritance and Hybrid Inheritance are not supported in Java using class.

But it can be achieved using Interfaces.

Why Multiple Inheritance is not supported in Java?

Dr James Gosling words on this

“JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions.”

He has clearly explained the reason behind not supporting because of

1)To Retain Simplicity rather than making it complex and confusing

2)Very rare situation to get the need of it.

3) to avoid confusion of using which method from multiple parent classes.

Let’s see 3rd point with example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.kb.inheritance;
 
 
class Parent1{
    
    public void show(){
        System.out.println("Parent1 - show");
    }
}
 
class Parent2{
    
    public void show(){
        System.out.println("Parent2 - show");
    }
}
package com.kb.inheritance;


class Parent1{
	
	public void show(){
		System.out.println("Parent1 - show");
	}
}

class Parent2{
	
	public void show(){
		System.out.println("Parent2 - show");
	}
}


1
2
3
4
5
6
7
8
public class Child extends Parent1,Parent2 { // if its allowed
 
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
    }
 
}
public class Child extends Parent1,Parent2 { // if its allowed

	public static void main(String[] args) {
		Child c = new Child();
		c.show();
	}

}

So which show() method it has to call ? Parent1 or Parent2 show() method, it gets ambiguity hence it’s not supported in Java. So compile time error.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on