Inheritance in Java is the way of code reusability this provides the facility to establish a relation between two classes as parent-child or base-derived. In this relation base will provides its non-private data and functions to the derived class.
- Subclass /Child – This class that inherits from another class.
- Superclass /Parent – This class being inherited from.
Extends Keywords
This keyword is use for making a single inheritance between two classes. Here, one class will be base class and other will be derived class.
Syntax of Inheritance
class derived-class extends base-class { //methods and fields }
Types of Inheritance in Java
- Single Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Notes: Multiple inheritance and Hybrid inheritance is not support in Java.
Single Inheritance
A subclasses inherit of one superclass, it is know as Single Inheritance.
Example of Single Inheritance
Input Value
Example of Single Inheritanceclass inhertance{ void study(){System.out.println("basic");} } class key extends inhertance{ void tus(){System.out.println("engineer");} } class TestInheritance{ public static void main(String args[]){ key d=new key(); d.study(); d.tus(); }}
Let’s run the code
Output Value
Multi-level Inheritance
A derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes.
Example of Multi-level Inheritance
Input Value
Example of Multi-level Inheritanceclass inhertancem{ void study(){System.out.println("basic");} } class tur extends inhertancem{ void key(){System.out.println("engineer");} } class Babytur extends tur{ void menu(){System.out.println("Hello Student");} } class inhertancem2{ public static void main(String args[]){ Babytur d=new Babytur(); d.menu(); d.key(); d.study(); }}
Let’s run the code
Output Value
Hierarchical Inheritance
A one class serves as a superclass for more than one subclass, It is know as Hierarchical Inheritance.
Example of Hierarchical Inheritance
Input Value
Example of Hierarchical Inheritanceclass inhertainh{ void study(){System.out.println("Basic");} } class rose extends inhertainh{ void key(){System.out.println("Engineer");} } class lily extends inhertainh{ void tur(){System.out.println("Hello Student");} } class TestInheritance3{ public static void main(String args[]){ lily c=new lily(); c.tur(); c.study(); }}
Let’s run the code
Output Value
Constructor With Inheritance
If the base and derived both classes having constructor then constructor never be inherited means base class constructor never be inherited with derived class but derived class constructor can call the base class constructor automatically.
Example Constructor With Inheritance
Input Value
Example Constructor With Inheritanceclass staticm { public staticm () { System.out.println("Basic Engineer"); } } class Con_inher extends staticm { public Con_inher() { System.out.println("Basic Engineer"); } public static void main(String[] args) { Con_inher m=new Con_inher(); } }
Let’s run the code
Output Value
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about JAVA Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments