Inheritance in Java
Inheritance in Java
In this tutorial you learn about the Inheritance in Java, Definition of Inheritance, what is Inheritance? all types of Inheritance in Java with programming examples and output also, lets start the learning.
Definition of Inheritance
The mechanism of deriving a new class form an old one is called Inheritance.
Inheritance is an important part of OPP (Object Oriented Programming). It is the mechanism in java by which one class is allowed to inherit the features of another class.
We can say that, in java inheritance means creating new classes based on existing one. A class that inherits form another class reuse the methods and fields of that class.
You can also say, Inheritance is used to borrow properties and methods form an existing class.
Declaring Inheritance in Java
Subclass extend Superclass
Inheritance in Java is declaring using extends Superclass.
Why use inheritance in Java
- For method Overloading
- For Code Reusability
- Abstraction
Terms used in Inheritance
Class: A class is a user defined blueprint or prototype from which objects are created. It a group of objects which have common properties.
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
Types of Inheritance in Java
On the basis of class. There are three types of inheritance in Java, that are given below.
- Single Inheritance (Only one Super class)
- Multilevel Inheritance (Several super classes)
- Hierarchical Inheritance (One super class, many subclasses)
Single Inheritance
In single inheritance, subclasses inherit the features of only one superclass. It is the simplest type of inheritance in java, a class inherits the properties from a single class is known as the single inheritance.
Programming Example of Single Inheritance;
package com.company; |
Output;
Reading
Writing
Multilevel Inheritance
The Multilevel Inheritance includes the involvements of at least two or more than two classes, are known as Multilevel Inheritance.
A derived class will be inheriting a base class as well as the derived class also acts as the base class for other class.
Programming Examples of Multilevel Inheritance
package com.company; |
Output;
Reading
Writing
Experiment
Hierarchical Inheritance
When two or more classes inherits a single class is known as Hierarchical Inheritance. Where multiple child classes inherit the methods and properties of the same parent class.
Programming Example of Hierarchical Inheritance
package com.company; |
Output;
Reading
Writing
So, in this tutorial you learned about the Inheritance of java, such as what is Inheritance in Java, it’s definition, types of Inheritance in java with programming examples. I hope you guys, you read this tutorial better and learn something new. If you have any doubt, you can ask in the comment section.
Read Also –
1 Comment
Chery Vohs · January 16, 2023 at 7:44 am
Perfect work you have done, this website is really cool with superb info .