Inheritance is one of the core concepts of object-oriented programming (OOP). It allows one class to inherit the fields and methods of another class, promoting code reusability and establishing a natural hierarchy between classes.
Single inheritance occurs when a class (the subclass) inherits from one and only one superclass (the parent class). This means that a subclass can access the properties and methods of only one superclass, creating a straightforward and manageable class hierarchy.
In Java, the extends
keyword is used to implement single inheritance. Here’s a basic syntax structure:
class Superclass {
// Superclass members (fields, methods)
}
class Subclass extends Superclass {
// Subclass members (fields, methods)
Let’s consider a simple example that illustrates single inheritance through a scenario involving animals. We will create a superclass called Animal
and a subclass called Dog
.
// Superclass
class Animal {
// Field
String name;
// Constructor
public Animal(String name) {
this.name = name;
}
// Method
void eat() {
System.out.println(name + " eats food.");
}
void sleep() {
System.out.println(name + " sleeps.");
}
}
Explanation:
name
field holds the name of the animal.name
field when an object of Animal
is created.eat()
method prints what the animal does, and the sleep()
method indicates the animal is sleeping.
// Subclass
class Dog extends Animal {
// Additional field specific to Dog
String breed;
// Constructor
public Dog(String name, String breed) {
// Call to the superclass constructor
super(name);
this.breed = breed;
}
// Method specific to Dog
void bark() {
System.out.println(name + " barks.");
}
// Overriding the sleep method
@Override
void sleep() {
System.out.println(name + " sleeps peacefully.");
}
}
Explanation:
Dog
class extends the Animal
class, inheriting its properties and methods.breed
field is specific to the Dog
class.super(name)
to call the constructor of the Animal
class, ensuring that the name
field is initialized.bark()
method is specific to the Dog
class.sleep()
method is overridden to provide a specific implementation for dogs.
public class Main {
public static void main(String[] args) {
// Create an Animal object
Animal animal = new Animal("Generic Animal");
animal.eat();
animal.sleep();
// Create a Dog object
Dog dog = new Dog("Buddy", "Golden Retriever");
dog.eat(); // Inherited method
dog.bark(); // Dog's own method
dog.sleep(); // Overridden method
}
}
When you run the Main
class, you will see the following output
Generic Animal eats food.
Generic Animal sleeps.
Buddy eats food.
Buddy barks.
Buddy sleeps peacefully.
Animal Class: This is the superclass. It defines general properties and behaviors for all animals, such as eating and sleeping. The name
field and methods are designed to be reusable for any subclass that extends Animal
.
Dog Class: This subclass represents a specific type of animal (a dog). By extending Animal
, it inherits the eat()
and sleep()
methods, allowing for code reuse. The bark()
method introduces behavior specific to dogs.
Method Overriding: The Dog
class overrides the sleep()
method to provide a customized message, demonstrating how subclasses can modify inherited behavior to fit their needs.
Main Class: The Main
class contains the main
method, where instances of Animal
and Dog
are created. This class demonstrates how the subclass can access both its own methods and those inherited from the superclass.
In real-world applications, single inheritance is often used to create a clear and understandable class hierarchy. For instance, in a banking application, you might have a superclass Account
with subclasses like SavingsAccount
and CheckingAccount
. Each subclass can inherit common methods like deposit()
and withdraw()
, while adding specific functionalities pertinent to their type.
In game development, single inheritance is frequently used to model entities in the game world. For example, a superclass Character
could have subclasses like Player
and Enemy
. Each subclass can inherit shared properties (like health and position) while implementing unique behaviors such as attacking or defending.
In graphical user interface (GUI) applications, single inheritance can simplify the management of components. A superclass Component
can have subclasses like Button
, TextField
, and Label
. Each subclass can inherit common methods like draw()
and setPosition()
, while also defining behaviors specific to user interaction