Last Updated on June 9, 2023 by Mayank Dham
In Java, inheritance is a fundamental concept that allows classes to inherit properties and behaviours from other classes. Through inheritance, a subclass can reuse and extend the features of a superclass, creating a hierarchical relationship between classes. This promotes code reuse, modularity, and the ability to create specialized classes based on existing ones. Inheritance is achieved using the extends keyword, where a subclass inherits its superclass’s fields, methods, and constructors and can add its unique characteristics or override inherited methods to customise its behaviour.
What is Inheritance?
One of the key components of object-oriented programming (OOP) is inheritance. This feature makes it easier for the child class to inherit all of the parent class’s properties. Using inheritance, new classes may be built on top of existing ones.
Why to use inheritance?
- Code Reusability:- We can use parent class properties in the child class so we don’t need to write that property again in the child class.
- Method Overriding:- We can achieve method overriding (Runtime Polymorphism) using inheritance.
Important terms used in inheritance:
-
Parent Class: A parent class is a class from which another class (Child Class) acquires all the properties of the parent class. It is also called a Super class or Base class.
-
Child Class: A child class is a class that acquires all the properties from its parent class. It is also called as Sub class or extended class.
Syntax of inheritance is in java.
Class Parent_class
{
// parent class methods and fields
}
Class Child_class extends Parent_class
{
// child class methods and fields
}
In the above syntax, we created a parent class and a child class. We have inherited the parent class in the child class using extends keyword which means the child class derives all the properties of the parent class.
Let’s understand inheritance in java with an example.
In the above image, we can see that we have one parent class named College with a property name and a child class named Branch with property total seats. Let’s write an inheritance program in java using the above example.
class College{ String name="PrepBytes"; } class Branch extends College{ int total_seats=100; public static void main(String[] args) { Branch computer_science=new Branch(); System.out.println("College name: "+computer_science.name); System.out.println("Total seats: "+computer_science.total_seats); } }
Output:
College name: PrepBytes
Total seats: 100
In the above program, we have created the parent class College and child class Branch which extends the College class. In the output, we can see that we can use parent class variables as well as child class variables in the child class Branch.
Types of inheritance in java:
There are mainly five types of inheritance:
- Single
- Multilevel
- Hierarchical
- Multiple
- Hybrid
Out of these five java supports only three inheritance (Single, Multilevel, Multiple). We will see all three types of inheritance program in java.
Single Inheritance Program in java:
When only a single child class extends the single parent class it is called Single inheritance. In the above image of single inheritance, “class2” is a child class that extends the parent class “class1”. Let’s see how to write a single inheritance program in java.
class Vehicle{ public void ride(){ System.out.println("riding vehicle"); } } class Type extends Vehicle{ public void car(){ System.out.println("vehicle type is car"); } } class PrepBytes{ public static void main(String[] args) { Type type=new Type(); type.ride(); type.car(); } }
Output:
riding vehicle
vehicle type is car
In the above program, we have created a parent class Vehicle and a child class Type. We can see that the child class Type extends the parent class Vehicle so when we create an object of the child class Type in class PrepBytes we do not need to create another object to fetch the method of the parent class Vehicle. We can use all the properties of the parent class using the child class objects. In the output, we can see that we called both methods using a single child class.
Multilevel Inheritance program in java:
When a sub-child class extends the child class and the child class extends the parent class it is called Multilevel Inheritance. In the above image of multilevel inheritance, “class3” is a sub-child class that extends the child class “class2” and that child class “class2” extends the parent class “class1”. Let’s see how to write a multilevel inheritance program in java.
class Vehicle{ public void ride(){ System.out.println("riding vehicle"); } } class Type extends Vehicle{ public void car(){ System.out.println("vehicle type is car"); } } class BMW extends Type{ public void speed(){ System.out.println("testing speed of BMW"); } } class PrepBytes{ public static void main(String[] args) { BMW bmw=new BMW(); bmw.ride(); bmw.car(); bmw.speed(); } }
Output:
riding vehicle
vehicle type is car
testing speed of BMW
In the above program, we created a parent class Vehicle, a child class Type, and a sub-child class BMW. We can see that a sub-child class BMW extends a child class Type and that child class extends parent class Vehicle. Now when we create an object of a sub-child class BMW we don’t need to write methods of parent class in child class. In the output, we can see that we can access all the methods of parent class Vehicle and child class Type in the main method of class PrepBytes.
Hierarchical Inheritance program in java:
When two child class extends the same single parent class it is called Hierarchical Inheritance. In the above image of Hierarchical inheritance, “class2” and “class3” are the two child class which extends the same parent class “class1”. Let’s see how to write a hierarchical inheritance program in java.
class Vehicle{ public void ride(){ System.out.println("riding a vehicle"); } } class Car extends Vehicle{ public void test(){ System.out.println("Testing a car"); } } class Bike extends Vehicle{ public void test(){ System.out.println("Testing a bike"); } } class PrepBytes{ public static void main(String[] args) { Car car=new Car(); car.ride(); car.test(); Bike bike=new Bike(); bike.ride(); bike.test(); } }
Output:
riding a vehicle
Testing a car
riding a vehicle
Testing a bike
In the above program, we created two child classes Car and Bike and we also created a parent class Vehicle. We can see that both the child class Car and Bike extend the same parent class Vehicle. Now when we create an object of the child class Car or Bike we can use the method of parent class in both the child class. In the output, we can see that using the object of both the child class we can access the parent class method in both the child class.
Why Multiple and Hybrid Inheritance program in java is not supported?
Multiple and Hybrid inheritance program in java is not possible to reduce the complexity and make the language more simple. Let’s understand why this type of inheritance program in java is not supported using one example.
In the above image, we can see that we have two parent classes “Class A” and “Class B” and both have the same method test now if we extend both the parent class in the same child class then the question arises that which test method to call in child class. Let’s understand further why this inheritance program in java is not possible.
class Car{ public void test(){ System.out.println("Testing a car"); } } class Bike{ public void test(){ System.out.println("Testing a bike"); } } class PrepBytes extends Car,Bike{ public static void main(String[] args) { PrepBytes pb= new PrepBytes(); pb.test(); } }
In the above program, we have created two parent classes Car and Bike and both have the same method test. When we extend both the classes in a single child class Prepbytes problem rise when we create an object of the child class PrepBytes and we call the method test compiler to get confused to call the test method of which parent class.
Conclusion
Inheritance is a powerful feature in Java that enables classes to inherit and extend the properties and behaviours of other classes. It promotes code reuse, modularity, and the creation of specialized classes. By using the extends keyword, a subclass can inherit the fields, methods, and constructors of its superclass and customise their behaviour as needed. Inheritance plays a crucial role in object-oriented programming, allowing for the creation of hierarchies of related classes and facilitating the development of flexible and maintainable code.
Frequently Asked Questions
Q1. What is the difference between superclass and subclass?
In Java, the superclass is the class that is being inherited from, while the subclass is the class that inherits properties from the superclass. The superclass provides the base set of fields and methods, which can be extended or overridden in the subclass.
Q2. Can a subclass have multiple superclasses in Java?
No, Java does not support multiple inheritance of classes. A subclass can only inherit from a single superclass. However, Java supports multiple inheritance of interfaces, where a class can implement multiple interfaces.
Q3. How does method overriding work in inheritance?
Method overriding allows a subclass to provide a different implementation for a method that is already defined in its superclass. The subclass declares a method with the same name, return type, and parameters as the superclass method, and the subclass implementation is used instead of the superclass implementation when the method is called on an object of the subclass.
Q4. Can a subclass access private members of its superclass?
No, a subclass cannot directly access the private members (fields or methods) of its superclass. Private members are only accessible within the class where they are declared. However, subclasses can access the protected and public members of their superclass.
Q5. What is the "super" keyword used for in Java?
The "super" keyword in Java is used to refer to the superclass of a subclass. It can be used to call the superclass constructor, access superclass methods and fields, and differentiate between superclass and subclass members with the same name.
Q6. Can a subclass add new fields and methods in addition to inheriting from its superclass?
Yes, a subclass can add its own fields, methods, and constructors in addition to inheriting from its superclass. This allows for customization and extension of the superclass functionality.
Q7. Is it possible to prevent a class from being inherited in Java?
Yes, in Java, you can prevent a class from being inherited by declaring it as final. A final class cannot be extended by any subclass.