Java : Inheritance
Understanding Java Inheritance: A Comprehensive Guide
Java, one of the most popular and widely used programming languages, is known for its object-oriented principles. One of the most important concepts in object-oriented programming (OOP) is inheritance. In this blog, we will dive deep into what inheritance is, how it works in Java, and how you can use it effectively in your projects.
What is Inheritance in Java?
In simple terms, inheritance is the mechanism by which one class can inherit the properties (fields) and behaviors (methods) of another class. The class that is inherited from is called the parent class (or superclass), and the class that inherits from the parent is called the child class (or subclass).
In Java, inheritance enables code reusability, as the child class can reuse the code written in the parent class without needing to rewrite it. This leads to cleaner, more efficient, and maintainable code.
Key Concepts in Java Inheritance
1. Single Inheritance
In Java, a class can inherit from only one class at a time. This is known as single inheritance. While some other languages (like C++) support multiple inheritance (where a class can inherit from more than one class), Java does not allow it directly to avoid complexity and ambiguity in the code.
2. The extends Keyword
To create a subclass in Java, you use the extends keyword. When a class extends another, it inherits all non-private fields and methods from the parent class.
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 Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal class
myDog.bark(); // Defined in Dog class
}
}3. Constructors in Inheritance
While constructors are not inherited, a subclass can call the constructor of its parent class using the super keyword. This allows the child class to initialize the fields of the parent class.
class Animal {
Animal() {
System.out.println("Animal is created.");
}
}
class Dog extends Animal {
Dog() {
super(); // Calling the parent class constructor
System.out.println("Dog is created.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
}
}Output:
Animal is created.
Dog is created.4. Method Overriding
In inheritance, the subclass can override a method of the parent class to provide its own specific implementation. To override a method, the subclass method must have the same name, return type, and parameters as the parent class method.
class Animal {
void sound() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound(); // Calls the overridden method in Dog class
}
}Output:
Dog barks.5. The super Keyword
The super keyword is used to refer to the immediate parent class. It can be used to access the parent class’s methods and constructors. It’s also helpful when you want to call an overridden method in the parent class.
class Animal {
void sound() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
super.sound(); // Calling the parent class method
System.out.println("Dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
}
}Output:
Animal makes a sound.
Dog barks.Advantages of Inheritance in Java
Code Reusability: Inheritance allows a child class to inherit the properties and methods of the parent class, leading to significant code reuse.
Get Rupam Thakre’s stories in your inbox
Join Medium for free to get updates from this writer.
Extensibility: It’s easy to extend an existing class without changing its source code. You can create new classes with additional features or functionality.
Method Overriding: Inheritance allows you to change or extend the behavior of inherited methods in subclasses.
Hierarchical Classification: Inheritance helps to organize and structure code hierarchically, which can make complex systems easier to manage and understand.
Types of Inheritance in Java
1. Single Inheritance
This is the most common form where a class inherits from one superclass. For example:
class Animal { }
class Dog extends Animal { }2. Multilevel Inheritance
In this form, a class inherits from another class, and that class inherits from another class, forming a chain. For example:
class Animal { }
class Mammal extends Animal { }
class Dog extends Mammal { }3. Hierarchical Inheritance
In hierarchical inheritance, multiple classes inherit from a single parent class. For example:
class Animal { }
class Dog extends Animal { }
class Cat extends Animal { }4. Hybrid Inheritance
Hybrid inheritance combines multiple inheritance and multilevel inheritance. However, Java does not directly support hybrid inheritance through classes, but it can be achieved through interfaces (which we’ll discuss next).
Interfaces and Multiple Inheritance in Java
Although Java does not support multiple inheritance through classes, it supports multiple inheritance using interfaces. A class can implement multiple interfaces, allowing it to inherit behavior from more than one source.
interface Animal {
void eat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
public void eat() {
System.out.println("Dog is eating.");
}
public void play() {
System.out.println("Dog is playing.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat();
myDog.play();
}
}Output:
Dog is eating.
Dog is playing.Conclusion
Java’s inheritance mechanism is one of its most powerful features, promoting code reuse, modularity, and a clear hierarchical structure. By understanding the basics of inheritance and its various features, you can create more efficient and maintainable Java programs. Remember to carefully design your class hierarchies and leverage inheritance wisely to avoid complex and tightly coupled code.
Keep exploring Java’s features, and don’t forget to practice to strengthen your understanding of object-oriented principles like inheritance!






