Java Inheritance

Java Inheritance is a process to acquire or inherit one class (base or parent) of attributes or behavior in to another class (child or derived).

Inheritance can be achieved by using extends or implements keyword.

In the below example, SuperClass know as parent class or base class and SubClass is known as the derived or the child class.

Inheritance uses mostly for method overriding(also called as runtime or dynamic polymorphism).

Syntax:

public class SuperClass {

}
public class SubClass extends SuperClass {

}

Example:

In the below code Maths class is behaving as parent or base class and Calculation class is behaving as child or derived class as it is extending Maths class.

The Calculation class is inheriting or acquiring Maths class addition() and subtraction() function.

Note: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors so they are not inherited by subclasses as they are not methods, but the constructor of the superclass can be invoked from the subclass using super keyword.

Types of Inheritance:

Single inheritance:

As mentioned in below diagram, B class is subclass of A super class. 

The relation in between Class A and Class B is “Class B IS-A class A”, this means Class B is type of Class A.

Class B inheriting everything from Class A.

Syntax:

Example:

In below example where company class in extends Student class and showing a IS-A relationship. This means Company sub (or child) class will be able to access data members, methods and constructors form Student super (or parent) class.

OUTPUT:

Multilevel inheritance:

Below is the chain of inheritance which is called as multilevel inheritance. class c inherits class B and class B inherits class A.

Syntax:

Example:

In the below example, ClassC extends ClassB and ClassB extends ClassA.

ClassB has access to all the data members and methods of ClassA.

Similarly, ClassC also has access to all the data members and methods of ClassB.

This means, ClassC has access to all data members and methods of ClassA and ClassB as show in below example.

OUTPUT:

Hierarchical inheritance:

When two or more class inherits the same class called as Hierarchical inheritance.

Note: There is not relation in between Class B and Class C. Accessing of data members and methods will give compile time error.

Syntax:

Example:

In below example we created an object of ClassC extends ClassA. As discussed above, accessing function of ClassB using ClassC object will given compile time error. 

OUTPUT:

Multiple Inheritance

Multiple inheritance not supported in Java. 

In below diagram, ClassA is inheriting ClassB and ClassC which is not allowed in Java will give compile time error.

Syntax:

Example:

In below example, on creation of ClassC object is giving compile time error as multiple inheritance not allowed in Java.

OUTPUT:

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan