Inheritance in Java
0% completed
Inheritance in java
Car Class
Multilevel Inheritance
Types of Inheritance
Method Overriding
Method Overriding II
Method Overriding III
Basics Of Java
Inheritance in java

 

Inheritance in Java

footer line

Inheritance

 

Inheritance is a crucial part of object-oriented programming. The process of inheriting the properties and behavior of an existing class into a new class is known as inheritance. When we inherit the class, we will reuse the original class’s methods and fields in a new class. Inheritance may also be described as the Is-A relationship, also known as the parent-child relationship.

 

Let’s understand the inheritance with the help of a real-world example.

Assume Human is a class with properties such as height, weight, age, and so on, as well as functionalities (or methods) such as eating(), sleeping(), dreaming(), working(), and so on.

We now want to create Male and Female classes. Males and Females are both humans, and since they share certain common properties (such as height, weight, age, and so on) and behaviors (such as eating(), sleeping(), and so on), they will inherit these properties and functionalities from the Human class. Male and Female each have their own set of characteristics (like men have short hair and females have long hair). Such properties may be added separately to the Male and Female classes.

 

This technique requires writing fewer codes since both classes inherited some properties and behaviors from the superclass and thus did not need to be rewritten. This also makes the code easier to understand.

 

 

Why we use inheritance?

  • The primary advantage of inheritance is code reuse. When we inherit the methods and fields of an existing class into a new class, we can reuse the code.
  • Only inheritance can achieve runtime polymorphism (method overriding).

 

 

Important Terminology in Inheritance

 

Super class:

The super class is the class whose features are inherited by a subclass. Super class is also known as the parent class or base class. 

 

Sub class:

Sub class is the class that inherits the other class. Sub class is also known as the child class or derived class.

 

 

Java Inheritance Syntax:

 

class SuperClass {
   // Methods and Fields
}

class SubClass extends SuperClass {
      // Methods and Fields
}


The extends is a keyword used to inherit the class.

 

 

Types of Inheritance in Java

 

Java only supports three types of inheritance: single inheritance, multilevel inheritance, and hierarchical inheritance. Multiple inheritance and hybrid inheritance are only possible in Java via interfaces. 

 

1. Single Inheritance

 

In single inheritance, one class can extend the functionality of another class. In single inheritance, there is only one parent class and one child class.

 

Syntax:

 

class SuperClass{
   //methods and fields
}
class SubClass extends SuperClass{
      //methods and fields
}

 

Example:

 

// Parent class
class Animal {

	public void eat() {
		System.out.println("eating");
	}
	
}

// Child class
class Dog extends Animal {

	public void bark() {
		System.out.println("barking");
	}
	
}

public class Test {

	public static void main(String args[]) {

	// Creating an object of the child class
	Dog obj = new Dog();

	// Calling methods
	obj.eat();
	obj.bark();
	}

}

 

Output: 

 

eating
barking

 

 

2. Multilevel Inheritance

 

When a class inherits from a derived class, and the derived class becomes the base class of the new class, it is called multilevel inheritance in Java. In multilevel inheritance, there is more than one level.

 

Syntax:

 

class A
{
    // Methods and Fields
}

class B extends A
{
   // Methods and Fields
}

class C extends B
{
   // Methods and Fields
}

 

Example:

 

class Animal {

	public void eat() {
		System.out.println("eating");
	}
	
}

class Dog extends Animal {

	public void bark() {
		System.out.println("barking");
	}
	
}

class BabyDog extends Dog {

	public void weep() {
		System.out.println("weeping");
	}
	
}

public class Test {

	public static void main(String args[]) {
		BabyDog obj = new BabyDog();

		// Calling methods
		obj.eat();
		obj.bark();
		obj.weep();
	}
	
}


Output: 

 

eating
barking
weeping

 


3. Hierarchical Inheritance

 

In hierarchical inheritance, one class serves as a base class for more than one derived class. 

 

Syntax:

 

class A
{
    // Methods and Fields
}

class B extends A
{
   // Methods and Fields
}

class C extends A
{
   // Methods and Fields
}

 

Example:

 

class Animal {

	public void eat() {
		System.out.println("eating");
	}
	
}

class Dog extends Animal {

	public void bark() {
		System.out.println("barking");
	}
	
}

class Cat extends Animal {

	public void meow() {
		System.out.println("meowing");
	}
	
}

public class Solution {

	public static void main(String args[]) {
		Cat obj = new Cat();

		// Calling Methods
		obj.eat();
		obj.meow();
		// obj.bark(); // compile time error

	}
	
}


Output: 

 

eating
meowing

 

 

The Diamond Problem in Java

 

Because of the diamond problem, Java does not support multiple inheritance. Let’s break down the diamond problem with the help of an example.

 

Consider the following scenario: A, B, and C are the three classes. Both the A and B classes are inherited by the C class. If two classes, A and B, have the same method, and we call it from a child class object (class C object). In this case, it would be unclear whether to call the methods of class A or class B. As a result, if we inherit two classes, the compiler will generate a compile-time error. Let’s look at the example below to understand better.

 

Example:

 

class A {

	public void display() {
		System.out.println("In Class A");
	}
	
}

class B extends A {

	public void display() {
		System.out.println("In Class B");
	}
	
}

public class C extends A, B {

	public static void main(String args[]) {

		C obj = new C();

		// Creates ambiguity which display() method to call 
		obj.display();
	}
	
}


Output: 

 

C.java:14: error: '{' expected
class C extends A, B {
                        ^
1 error

 

The solution to the Diamond problem is to use default methods and interfaces. Using interfaces, we can achieve multiple inheritance in Java. We will cover interfaces in the latter section of the module.