Inheritance
Inheritance
An OOP principle where one class (the subclass) can reuse and extend the attributes and methods of another class (the superclass).
The Parent-Child Relationship
- Parent Class (Superclass):
- Contains common data and actions that can be shared with multiple child classes.
- Serves as a blueprint for its subclasses.
- Child Class (Subclass):
- Inherits data and actions from the parent class.
- Can add new features or override existing ones to provide specialised behaviour.
- Think of inheritance like a family tree.
- The parent class is the ancestor, and child classes are descendants that inherit traits but can also have unique characteristics.
- Consider a Person class with attributes like name and age, and a method walk().
- A Student class can inherit from Person, gaining these attributes and methods while adding its own, such as study().
In Java, inheritance is defined via the keyword extends.
public class Dog extends Animal {
...
}Here, Dog is a subclass and Animal is a superclass.
How Inheritance Works
- Data inheritance:
- Child classes inherit attributes from the parent class.
- These attributes can be used or overridden as needed.
- Action inheritance:
- Child classes inherit methods from the parent class.
- Methods can be overridden to provide specialised behaviour.
- When designing a class hierarchy, start by identifying common features that can be placed in a parent class.
- This promotes code reuse and simplifies maintenance.
- A Bird class might have a fly() method.
- A Penguin class can override this method to reflect that penguins cannot fly, while still inheriting other characteristics like eat() or swim().
public class Bird { // parent class
public void fly() {
System.out.println("This bird can fly.");
}
public void eat() {
System.out.println("The bird is eating.");
}
public void swim() {
System.out.println("Some birds can swim.");
}
}
public class Penguin extends Bird { // child class
@Override
public void fly() { // overriding the fly() method
System.out.println("Penguins cannot fly.");
}
public static void main(String[] args) {
Bird genericBird = new Bird();
genericBird.fly(); // output: This bird can fly.
genericBird.eat(); // output: The bird is eating.
Penguin penguin = new Penguin();
penguin.fly(); // output: Penguins cannot fly.
penguin.eat(); // output: The bird is eating.
penguin.swim(); // output: Some birds can swim.
}
}Tip
@Override in Java:
- @Override is an annotation that tells the compiler that a method in a subclass is intended to replace (override) a method in its parent class.
- It ensures compile-time checking, so if you accidentally mismatch the method name or parameters, the compiler will give an error.
- While it is not necessary, using @Override improves code readability and makes it clear that the subclass is providing a specialised version of a parent method.
Advantages of Inheritance
Code reusability:
- Inheritance allows child classes to reuse the data and actions of a parent class.
- This eliminates the need to rewrite code, reducing duplication and errors.
- Consider a Vehicle class with methods like start() and stop().
- A Car class can inherit these methods without rewriting them, focusing only on car-specific features.
public class Vehicle { // parent class
public void start() {
System.out.println("Vehicle is starting...");
}
public void stop() {
System.out.println("Vehicle is stopping...");
}
}
public class Car extends Vehicle { // child class
public void honk() { // car-specific feature
System.out.println("Car horn: Beep Beep!");
}
public static void main(String[] args) {
Car myCar = new Car();
// testing inherited methods from Vehicle
myCar.start();
myCar.stop();
// testing car-specific method
myCar.honk();
}
}
Extensibility: