Table of Contents
ToggleIntroduction to Types of Inheritance in Java
There are different types of inheritance in Java, it means deriving or inheriting the superclass properties by the subclass. In general, the term Inheritance means deriving features from parents or ancestors by the children. Inheritance in Java also performs similar operations.
Now, the question arises why do we need inheritance? Suppose, You want to use a method or variable, multiple times in a program; you need to code it repeatedly.
This increases the time and memory consumption. Inheritance, with its beautiful property, helps in code reusability. That means you can use the same code again and again in just a single line using inheritance.
Inheritance is one of the OOPs concepts, and it is an Is-A property. There are two primary classes in the inheritance Base Class and Derived Class. There are 5 types of inheritance in Java.
- The base class is also called the Parent class or Super class.
- The derived class is also called the Child class or Subclass.
To use the types of inheritance in Java, you need to use the “extends” keyword in the code, which helps the compiler to know that the class before the “extends” keyword is the derived class, and the class after the “extends” keyword is the base class.
Important points to remember for Inheritance in Java:
- A constructor cannot be inherited.
- Private members are accessed only to the defined class.
- There is no possibility of inheriting the properties of the child class from the parent class. That means no reverse inheritance.
Syntax of inheritance in Java:
Class Parent
{
// body
}
Class Child extends Parent
{
// body
}
There are 5 Types of Inheritance in Java:
- Single Inheritance
- Multiple Inheritance
- MultiLevel Inheritance
- Hybrid Inheritance
- Hierarchical Inheritance
Each type of inheritance in Java has its own unique property and application. In this blog, I will elaborate on the types of inheritance in Java along with syntax and their examples. Stay with me to understand the beauty of inheritance with its properties.
Brief Overview of Java
Java is the IT industry’s most popular and highly used programming language. It is a complex language, but it is a more secure and robust language that helps make it the king of programming languages.
Properties of Java:
- Developed by Sun Microsystems in 1995
- Java is platform independent language, which means you can run its code on any platform like mac or OS.
- It is Object Oriented Language.
- It is a High-level language.
- It is used in the development of mobile and desktop applications, games, and many more.
- It is a compiler language.
- It is a case-sensitive language and structured language.
Java is a OOP (Object-Oriented Programming) language with the following OOPs properties:
- Polymorphism
- Inheritance
- Abstraction
- Encapsulation
- Association
- Composition
Terminologies of Types of Inheritance in Java
Before going deeper into the types of inheritance in Java, I am elaborating on the terminologies used in inheritance in Java.
1. Class
It is a user-defined blueprint to create object or objects. It contains variables and methods of the code. The variable can be local, instance, or class variable. You need to create a class before defining an object.
It is created with a keyword “class” followed by a class name.
Syntax:-
class <class name>
{
//class body
}
For example:-
class A
{
int i = 1;
System.out.println(i);
}
2. Object
It is a member or instance of the class. They are created at the runtime.
For example for class A, object is define as:
A obj1 = new A() // obj1 is the object for class A
Objects have 3 main points:
- State
- Behavior
- Name/ declaration/ Initialization
3. Parent Class/ Base Class/ SubClass
The parent class contains various features and properties that the base class can inherit. It can also have private, public, or protected variables and methods that can be inherited or not, depending on the type of access modifier.
It is defined by the “class” keyword with the class name.
Syntax : class <class name>
{
// body
}
4. Child Class/ Derived Class/ Super Class
It is the class that inherits the properties of the superclass. It contains all the methods of the superclass along with its own methods.
Its syntax is as follows:-
class <subclass name> extends <superclass name>
{
Class body
}
For example:-
class child1 extends class parent1
{
// child class body
}
5. Variables
These are the containers in Java that hold values. The data type defines the type of variable. There are the following types of variables in Java:
- string: stores words or strings like “thanks”; it is always quoted in double-quotes.
- int: It stores integer values without decimals. For example 1,2,77
- float: it contains floating numbers like 22.9
- boolean: holds boolean values, that is: true or false
- char: it stores characters, like a, c.
6. Methods
It is a block of code that contains statements and objects to create specific tasks. Java also contains predefined methods in its library.
Method in Java is defined by method name followed by parentheses ().
For example: books()
Types of Inheritance in Java
There are 5 types of inheritance in Java. This section covers all the types of inheritance in java with their syntax and example.
Single Inheritance
It is the most basic inheritance. It has a child class that inherits features of only one parent class.
Here in the above diagram, class B is child or subclass that inherits the properties of superclass A
1. Syntax of Single Inheritance
class <parent class name>
{
// body of parent class
}
class <child class name> extends <parent class name>
{
// body of child class
}
2. Example of Single Inheritance
class A
{
void method1()
{
System.out.println(“I am method of parent class”);
}
class B extends A //inheritance
{
void method2()
{
System.out.println(“”I am method of base class”);
}
public static void main(String[] args)
{
B obj1 = new B();
obj1.method1(); // super class method
obj2.method2(); // base class method
}
}
Output
I am method of parent class
I am method from base class
Multiple Inheritance
In multiple inheritance, child class derived properties of more than one parent class. Having several parent classes increases code complexity and lots of dependencies, by which programmers rarely use Multiple inheritance.
Multi-Level Inheritance
In multilevel inheritance there are several child classes and parent classes. At a time, one child class becomes the parent class for another child class. Consider flow diagram to understand it:
In above diagram:
- B is child class of A
- C is child class for B
- C inherits features of both A and B
- A is grandparent of C
1. Syntax of MultiLevel Inheritance
class <parent class name>
{
// body of class
}
class <child class name> extends <parent class name>
{
// body of child class
}
class <child class name> extends <parent class name>
{
// body of child class
}
2. Example of MultiLevel Inheritance
class A
{
void method1()
{
System.out.println(“I am grandparent”);
}
class B extends A // intermediate base class C inherit features of A
{
void method2()
{
System.out.println(“I am parent”);
}
class C extends B // base class inherit features of C
{
void method3()
{
System.out.println(“I am child”);
}
public static void main(String[] arg)
{
C obj1 = new C(); //object created in class C
obj1.method1(); // method of grandparent class A
obj2.method2(); // method of parent class B
obj3.method3(); // method of child class C
}
}
Output
I am grandparent
I am parent
I am child
Hierarchical Inheritance
In hierarchical inheritance, there is one paren t class which is derived by multiple child classes. You can understand it with the example of a father with 3 children.
There are 3 child classes with one parent class in the above hierarchical inheritance. Hierarchical inheritance does not support a combination of multiple and single inheritance.
1. Syntax of Hierarchical inheritance
class <parent class name>
{
// body of parent class
}
class <base class1 name> extends <parent class name>
{
// body of base class
}
class <child class2 name> extends <parent class name>
{
// body child class>
}
2. Example of Hierarchical inheritance
class Father
{
public method1()
{
System.out.println(“I am father”);
}
}
class Ram extends Father
{
public method2()
{
System.out.println(“I am Son”);
}
}
class Sita extends Father
{
void method3()
{
System.out.println(“I am daughter”);
}
public static void main(String[] args)
{
Sita s = new Sita();
Ram r = new Rahul();
s.method1(); // inherit method of parent class Father by class Sita
r.method1(); // inherit method of parent class Father by class Ram
}
}
Output
I am Father
I am Father
Hybrid Inheritance
It is a combination of two types of inheritance in Java. It can be a combination of single and multilevel inheritance or single and hierarchical inheritance. It is implemented by using an interface in Java.
In the above diagram, there is a combination of the following.
- Single inheritance between A and B. B inherits the properties of A
- Hierarchical inheritance between B and X,Y. X and Y are the child classes and B is their parent class.
1. Syntax of Hybrid Inheritance
class <parent class name>
{
//body of parent class
}
class <child class name> extends <parent class name>
{
//body of child class
}
class <child class name> extends <parent class name>
{
// body of child class
}
Class <base class name> extends <parent class name>
{
// body of child class
}
2. Example of Hybrid Inheritance
class A
{
void method1();
{
System.out.println(“I am super boss”);
}
}
class B extends A // single Inheritance
{
void method2();
{
System.out.println(“I am boss and working under superboss”);
}
}
class X extends B //Hierarchical Inheritance
{
void method3();
{
System.out.println(“I am junior 1”);
}
}
class Y extends B
{
void method4();
{
System.out.println(“I am Junior 2”);
}
public static void main(String[] args);
{
Y y = new Y();
y.method1(); // method of class A
}
}
Output
I am super boss
Advantages of Inheritance in Java
Inheritance helps programmers in many ways, and it is one of the essential properties of OOPs. The various advantages of types of inheritance in Java are as follows:
Code Reuse
Inheritance helps in code reusability, and Programmers can use the same code in different parts of the program. This reduces time and memory.
Method Overriding
With inheritance, you can easily override the same method at different places in a program. Method overriding means derived/child class can add or change the method’s behavior derived from the base/parent class.
Method overriding helps in run-time polymorphism. Polymorphism has more than one form. By inheritance, a single method can be modified in various forms.
Data Hiding
The base class can hide data that the child class cannot derive. Methods and variables can be made private to the parent class only by using the “private” keyword followed by the method and variable name.
Dis-Advantages of Inheritance in Java
Decrease Code Execution Speed
Inheritance increases code complexity and dependencies. It takes more execution time as child and parent classes are tightly coupled. Types of inheritance in Java decrease the execution speed due to more dependencies.
Code Modification
Subclass depends on the superclass; it becomes difficult to modify code. The dependency increases by which modification in one place affects all the dependent classes.
Also Read: 25+ Career Options After Engineering.
Conclusion
Inheritance is one of the concept of OOPs and it helps in code reusability and runtime polymorphism. You can define members of classes as public, private, or protected to define the boundaries of their use.
It is the end of this blog on types of inheritance in Java. I hope this blog will help you to understand the inheritance concepts and types of inheritance in Java. If you are looking to learn some programming language to be a software developer or for some other purpose, Java is unbeatable.
Frequently Asked Question's
Inheritance means inheriting ancestors’ properties in programming languages like Java, C++, PHP, and Python. It is one of the OOPs concepts. In Java, Inheritance is implemented in the code using a superclass and Subclass There are multiple types of inheritance in java. The Subclass inherits the properties like methods and variables of the superclass.
- Another name for the superclass is base class or parent class. Other names for the Subclass are child class or derived class.
- There are several types of Inheritance in Java. The Inheritance helps in code reuse and supports run-time polymorphism.
- Along with the beautiful advantages of Inheritance, a few disadvantages are associated with Inheritance in Java.
Inheritance is one of the concepts of OOPs languages. The process of inheriting the features of the base class by the derived class is called Inheritance.
The types of Inheritance in Java is implemented using the “protected” keyword after the derived class name. There are 5 different types of Inheritance in Java. each has its use and properties.
Syntax of Inheritance:
<derived class name> extends <base class name>
{
// body of the derived class
}
For example
Class father
{
void method1()
{
System.out.println(“I am father”);
}
class daughter extends father
{
void method2()
{
System.out.println(“I am daughter”);
}
Daughter obj1 = new daughter();
obj1.method1();
}
Inheritance is a property of Java and other object-oriented languages that helps inherit the parent class’s properties by the base class.
Inheritance is one of the essential parts of OOPs among the other 3 properties like Abstraction, Polymorphism, and Data Abstraction.
There are basically 5 different types of Inheritance in Java, and they all are very important in their sense. In Java, Inheritance is implemented by using the “protected” keyword. There are 2 primary classes in Inheritance in Java.
- Parent Class
It is the class whose properties are used by the child class. The child class inherits all variables and member functions with defined access modifiers. It is also known as base class or superclass class.
- Child Class
This class inherits the properties of the Parent class. It is also known as a derived class or Subclass.
There can be more than one parent class and multiple child classes per the types of Inheritance in Java.
There are 5 different types of Inheritance in Java, and they are as follows:
- Simple Inheritance
In this Inheritance, there is one parent class and one child class. It is the most basic type of Inheritance in Java.
- Multiple Inheritance
This type of Inheritance in Java is very complex and rarely used by programmers. In this type of Inheritance in Java, there is more than one parent class and only one child class.
- MultiLevel Inheritance
In this, one parent class is inherited by the child class; this child class becomes the parent class to another child class, and so on.
- Hybrid Inheritance
It combines two or more different types of Inheritance in Java. Like simple Inheritance and multilevel Inheritance.
- Hierarchical Inheritance
In this types of Inheritance in Java, there is one parent class and several child classes which inherit the parent class’s properties.
There are not 6 types of Inheritance in OOPs. But there are 5 types of Inheritance in OOPs, and some of them are not supported by Java, like Multiple Inheritance, due to its complexity and more dependencies.
The 5 types of Inheritance in OOPs are as follows:
- Simple Inheritance
It is the simplest Inheritance; in this, one superclass and one Subclass inherit the properties of the superclass.
- Multiple Inheritance
In multiple Inheritance, there is various Inheritance in the same program. There is more than one parent class for the single-child class.
- Multilevel Inheritance
In Multilevel Inheritance, one child class becomes a parent class to the other child class, and so on. One child class inherits the properties of various parent classes.
- Hybrid Inheritance
It is a pairing of two or more Inheritance in OOPs. It includes the features of at least two types of Inheritance.
- Hierarchical Inheritance
In hierarchical Inheritance, there is one superclass class with multiple subclasses. Each derived class inherits the properties of the same base class.