Inheritance in Java
Inheritance is one of the four pillars of Object-Oriented Programming. It allows a class to inherit fields and methods from another class, promoting code reuse and establishing a natural hierarchy between classes.
What is Inheritance?
Inheritance is a mechanism where a new class (called a subclass or child class) is derived from an existing class (called a superclass or parent class). The subclass inherits all non-private fields and methods from the superclass.
- Superclass (Parent): The class being inherited from
- Subclass (Child): The class that inherits
- extends: The keyword used to inherit from a class
The extends Keyword
In Java, you use the extends keyword to inherit from a class.
// Superclass (Parent)
class Animal {
String name;
void eat() {
System.out.println(name + " is eating.");
}
void sleep() {
System.out.println(name + " is sleeping.");
}
}
// Subclass (Child) - inherits from Animal
class Dog extends Animal {
void bark() {
System.out.println(name + " says: Woof!");
}
}
In this example, Dog inherits all non-private members from Animal. A Dog object can use eat(), sleep(), AND its own bark() method.
The IS-A Relationship
Inheritance establishes an IS-A relationship. If Dog extends Animal, then we can say "a Dog IS-A Animal". This relationship is fundamental to polymorphism.
Dog myDog = new Dog();
myDog.name = "Buddy";
// Dog IS-A Animal, so Dog can do everything Animal can do
myDog.eat(); // Inherited from Animal
myDog.sleep(); // Inherited from Animal
myDog.bark(); // Defined in Dog
What Gets Inherited?
A subclass inherits:
publicmembers - accessible everywhereprotectedmembers - accessible in subclass and same package- Default (package-private) members - only if subclass is in same package
privatemembers - never accessible directly in subclass- Constructors - not inherited, but can be called using
super()
Single Inheritance in Java
Java supports single inheritance only - a class can extend only ONE other class. This avoids the complexity of the "diamond problem" found in languages with multiple inheritance.
// Valid - single inheritance
class Dog extends Animal { }
// INVALID - multiple inheritance not allowed
// class Dog extends Animal, Pet { } // Compile error!
Inheritance Hierarchy
Classes can form a chain of inheritance. Each subclass inherits from its direct parent AND all ancestors above it.
class Animal {
void breathe() {
System.out.println("Breathing...");
}
}
class Mammal extends Animal {
void warmBlooded() {
System.out.println("I'm warm-blooded!");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("Woof!");
}
}
// Dog inherits from Mammal AND Animal
Dog dog = new Dog();
dog.breathe(); // From Animal
dog.warmBlooded(); // From Mammal
dog.bark(); // From Dog
Click Run to execute your code
Benefits of Inheritance
- Code Reuse: Write common code once in the parent class
- Extensibility: Add new features without modifying existing code
- Maintainability: Fix bugs in one place (parent class)
- Polymorphism: Treat objects of different classes uniformly
Common Mistakes
- Trying to access private members: Private fields/methods are not accessible in subclasses. Use getters/setters or
protectedaccess. - Forgetting that constructors aren't inherited: You must call the parent constructor explicitly using
super()if needed. - Overusing inheritance: Use inheritance for IS-A relationships. For HAS-A relationships, use composition instead.
Summary
- Inheritance allows a class to inherit fields and methods from another class using
extends. - The child class (subclass) inherits from the parent class (superclass).
- Inheritance establishes an IS-A relationship.
- Java supports single inheritance - one class can only extend one other class.
- Private members are not inherited; constructors are not inherited but can be called with
super().
Enjoying these tutorials?