In Object-Oriented Programming (OOP) the fundamental idea of Inheritance enables derivative classes to receive the features of base classes. Inheritable code and logical organization emerge through this mechanism which establishes relationships between different classes.
The extends keyword in Java code enables inheritance through which child classes automatically obtain attributes and behaviors from parent classes.
In this guide, we will learn Java inheritance mechanisms as well as different inheritance types and practical implementation examples.
Key Concepts of Inheritance in Java
Before diving into code, let’s define the key terms in inheritance:
- Superclass (Parent Class): A class whose properties and methods are inherited by another class.
- Subclass (Child Class): A class that inherits the properties and methods from a superclass.
- Extends Keyword: Used by the subclass to inherit from the superclass.
Inheritance makes it easy to reuse existing code and extend functionality.
Syntax of Inheritance in Java
The general syntax for creating inheritance in Java is:
class Parent {
// Parent class code
}
class Child extends Parent {
// Child class inherits from Parent class
}
The Child class inherits all non-private members of the Parent class, including fields and methods.
Simple Example of Inheritance in Java
Here’s a simple example to demonstrate inheritance:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class TestInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal class
dog.bark(); // Method defined in Dog class
}
}
Output:
This animal eats food.
The dog barks.
Types of Inheritance in Java
Java supports different types of inheritance, each used to model relationships in code. The main types of inheritance are:
1. Single Inheritance
In single inheritance, a subclass inherits from only one superclass.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class TestSingleInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal class
dog.bark(); // Defined in Dog class
}
}
Output:
This animal eats food.
The dog barks.
2. Multilevel Inheritance
In multilevel inheritance, a class is derived from another class, which is also a subclass of another class.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Puppy extends Dog {
void play() {
System.out.println("The puppy plays.");
}
}
public class TestMultilevelInheritance {
public static void main(String[] args) {
Puppy puppy = new Puppy();
puppy.eat(); // Inherited from Animal
puppy.bark(); // Inherited from Dog
puppy.play(); // Defined in Puppy
}
}
Output:
This animal eats food.
The dog barks.
The puppy plays.
3. Hierarchical Inheritance:
In hierarchical inheritance, multiple subclasses inherit from a single superclass.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("The cat meows.");
}
}
public class TestHierarchicalInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.bark(); // Defined in Dog
Cat cat = new Cat();
cat.eat(); // Inherited from Animal
cat.meow(); // Defined in Cat
}
}
Output:
This animal eats food.
The dog barks.
This animal eats food.
The cat meows.
4. Multiple Inheritance (Using Interfaces):
Java does not allow multiple inheritance of classes, but it supports multiple inheritance through interfaces. A class can implement multiple interfaces.
Example:
interface Animal {
void eat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
public void eat() {
System.out.println("The dog eats.");
}
public void play() {
System.out.println("The dog plays.");
}
}
public class TestMultipleInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Implemented from Animal interface
dog.play(); // Implemented from Pet interface
}
}
Output:
The dog eats.
The dog plays.
While Java does not support multiple inheritance in object-oriented programming, C++ does.
Also Read: Interface in Java
5. Constructor Inheritance in Java
In Java, constructors are not inherited by subclasses, but they can be invoked using super(). The constructor of the superclass is called before the subclass’s constructor.
Also Read: Constructors in Java
Benefits of Using Inheritance in Java
- Code Reusability: Inheritance allows subclasses to reuse code from the parent class.
- Modular Code: Inheritance allows for better organization and structure of code.
- Maintainability: Changes in the superclass reflect automatically in the subclasses, making maintenance easier.
- Polymorphism: Allows the method of a subclass to be invoked, even when using the superclass reference.
Common Inheritance Pitfalls in Java
- Tight Coupling: Subclasses can become tightly coupled to their superclasses, making changes difficult.
- Overridden Method Confusion: If methods are overridden incorrectly, it may lead to unexpected results.
- Lack of Flexibility in Inheriting Multiple Classes: Java doesn’t allow multiple inheritance directly, which can lead to limitations when modeling complex relationships.
Conclusion
Inheritance is a powerful feature of Java that supports code reuse and hierarchical classification. You can write more efficient and maintainable code by mastering single, multilevel, hierarchical, and multiple inheritance (through interfaces).
Understanding the super keyword, method overriding, and constructor inheritance will also empower you to use inheritance more effectively.
This brings us to the end of the guide on Inheritance in Java. Hope this helps you to up-skill your Java skills. Also, if you are preparing for Interviews, check out these OOPS Interview questions to ace them like a pro.
Frequently Asked Questions
1. Why is multiple inheritance (of classes) not supported in Java?
Java does not support multiple inheritance with classes to avoid the diamond problem, where ambiguity arises if two parent classes have the same method. This ensures cleaner, less error-prone code.
2. How can we achieve multiple inheritance in Java?
In Java, multiple inheritance is achieved through interfaces. A class can implement multiple interfaces, inheriting methods from each without ambiguity.
Also Read: Difference between abstract classes and interfaces in Java
3. Can constructors be inherited in Java?
Constructors are not inherited in Java, but a subclass can call the constructor of its superclass using the super() keyword to initialize the parent class.
Related Free Courses: