Java Inheritance is a property of Object-Oriented Programming Concepts by which we can access some methods of a class in another class. The class whose methods is inherited know as Parent class/ Base class/ Superclass, and the class which is derived from Parent class is known as Child class/Subclass.
Java Inheritance Example
The concept behind inheritance in Java is that you can create the new classes that are built upon existing classes. When you inherit methods and properties from an existing class, you can reuse the methods and fields of a parent class. Also, you can add new methods and fields in your current class also.
Syntax
Some important points about inheritance:
- Any sub can inherit all the members (all fields, member functions, and nested classes) from its superclass.
- Constructors are not considered as member functions; that’s why a subclass can not inherit it, but we can invoke a constructor of the superclass.
- A subclass inherits one superclass one at a time, but a class can have multiple subclasses.
Why use Inheritance in Java
Java is an Object-Oriented Programming Language. Inheritance is one of the most popular concepts in Object-Oriented Programming.
We use Inheritance in Java for the following reasons:
- Encapsulation: If we have the common attributes, then we encapsulate these in the parent class and give specific attributes to the child classes.
- Polymorphism: We can also use the Polymorphism with an Inheritance in Java. So that based on a type of child class, the same class behaves differently. Runtime polymorphism cannot be achieved without using the inheritance.
- Code Reusability: Using the Inheritance, we can create a reusable code.
- Efficiency: It is efficient to use Inheritance while writing code. This can increase the speed of the project.
- We can simulate an inheritance of classes with real-time objects, which makes the OOPs more realistic.
- Inheritance provides data hiding. The base class can hide some data from a derived class by making it private.
Types of Inheritance in Java
There are five types of inheritance.
- Single or Simple Inheritance
- Multiple Inheritance (Through Interface)
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance (Through Interface)
Now we are going to discuss those four different types of inheritance, one by one as Java doesn’t support multiple inheritances. We’ll discuss this later on in this topic.
Single or Simple Inheritance
In this inheritance, the subclass is derived from its superclass. Generally, in single inheritance, there are only one superclass and one subclass. It’s also known as Single level inheritance.
In the above diagram, Class A is extending by Class B. Here, Class A is parent/superclass of Class B, which is child/subclass.
Let’s understand Simple Inheritance with the help of an example.
// Simple.java class Person { int age,id; String name; void naming(String name) { System.out.println("Name:"+name); } } class Student extends Person { void ageN(int age) { System.out.println("Age of student is:"+age); } } class Simple { public static void main(String [] er) { Student s= new Student(); s.naming("Robert"); s.ageN(14); } }
See the below output.
Multilevel Inheritance
When a subclass is derived from a derived class, then this type of inheritance is known as Multilevel inheritance. In the Multilevel Inheritance, the derived class will be inheriting the parent class and as well as the derived class act as a parent class to another class.
As per the above diagram Class A extending by Class B and Class B is extending by class C.
Now we will discuss it with the help of an example.
// Multilevel.java //creating a class class Student { void printIden() { System.out.print("Student's "); } } //extending Student class class BTech extends Student { void Degree() { System.out.println("Degree is Bachelor of Technology"); } } //extending BTech class class CSE extends BTech { void Stream() { System.out.println("Stream is Computer Sciencec and Engineering"); } } //extending CSE class class CyberSecurity extends CSE { void Domain() { System.out.println("Domain is Cyber Security"); } } //driver class class Multilevel { public static void main(String []er) { BTech bt=new BTech(); bt.printIden(); bt.Degree(); CSE cse =new CSE(); cse.printIden(); cse.Stream(); CyberSecurity cs =new CyberSecurity(); cs.printIden(); cs.Domain(); } }
See the below output.
In this example, we have seen that the Student class is inherited by BTech class further BTech class is extended by CSE class, and the CyberSecurity class inherits CSE class.
Multiple Inheritance
When one class inherits more than one class is known as Multiple inheritance. But as we mentioned before that java does not support multiple inheritance because of ambiguity.
Suppose, Class C inherits Class A and Class B. If there is the same method in both of these classes, it will create ambiguity whenever we call the standard method using child class object. Still, we try to create these types of situations then it will show us compile-time error rather than run time error (because a compile-time error is better than run time error).
In the above diagram, Class A and Class B are extending by Class C.
We can achieve the Multiple Inheritance in Java through Interfaces.
See the below program.
// Multiple.java // declaring interface Pet interface Pet { public void Show(); } //declaring interface Animal interface Animal { public void Type(); } //Multiple inheritance using interface class Dog implements Pet, Animal { //decalring methods public void Show() { System.out.println("It is a Pet"); } //declaring methods public void Type () { System.out.println("It is an Animal"); } } //driver class class Multiple { public static void main(String []er) { Dog d=new Dog(); d.Show(); d.Type(); } }
The output is below.
Why multiple inheritance is not supported in java
Consider the scenario where A, B, and C are three classes. Class C inherits class A and B. If A and B classes have the same method and you call it from the child class object, they will create an ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit two classes. So whether you have the same method or different, there will be a compile-time error.
Hierarchical Inheritance
In this Inheritance, one Class is extending by many other subclasses. In Hierarchical inheritance, one parent class will be inherited by any subclasses.
In the above diagram, Class B, Class C, and Class D inherit the superclass A. See the following code.
// Hierarchical.java //creating a class class Person { int id; //creating a common method void printId(int id) { System.out.println("ID:"+id); } } //extending person class class Student extends Person { void iden() { System.out.print("Student's"); } } //extending person class class Teacher extends Person { void iden() { System.out.print("Teacher's "); } } //extending person class class Clark extends Person { void iden() { System.out.print("Clark's"); } } // driver class class Hierarchical { public static void main(String [] er) { Student s=new Student(); s.iden(); s.printId(11701200); Clark c=new Clark(); c.iden(); c.printId(1203); Teacher t= new Teacher(); t.iden(); t.printId(11020); } }
See the output.
In this above example, we have a class name Person who has a standard method printId() which is being used by Student, Teacher, and Clark class.
Hybrid Inheritance
If there is class A and it is extending by class B and class C. Further, if class D inherits both class B and class C, then this type of inheritance is known as Hybrid Inheritance. It will also cause an error as class D inherits multiple classes, i.e. class B and class C.
Hybrid Inheritance is a combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through the interface we can achieve this.
Finally, Java Inheritance Example | Inheritance in Java article is over.