Understanding Inheritance in Java
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit fields and methods from another class. It helps promote code reusability and establishes a relationship between different classes. In Java, inheritance enables a class to acquire properties and behaviors (methods) of another class, thus allowing you to create a hierarchy of classes. The class that is inherited from is called the superclass, while the class that inherits is called the subclass.
Inheritance allows us to create new classes based on existing ones, extending or modifying their functionality. This not only saves time but also helps in maintaining a more organized and efficient codebase. Java supports single inheritance, meaning a subclass can inherit from only one superclass.
Sample Program: Demonstrating Inheritance in Java
Here’s a simple Java program that demonstrates inheritance. In this example, we have a Superclass called Animal
, and a Subclass called Dog
. The Dog
class inherits from Animal
, allowing it to reuse the properties and behaviors defined in Animal
.
javaCopy// Superclass (Parent class)
class Animal {
// Fields of the class
String name;
// Constructor
public Animal(String name) {
this.name = name;
}
// Method to describe the animal
public void sound() {
System.out.println(name + " makes a sound.");
}
}
// Subclass (Child class)
class Dog extends Animal {
// Constructor of Dog class
public Dog(String name) {
// Calling the constructor of the superclass (Animal)
super(name);
}
// Overriding the sound method to provide a specific implementation for Dog
@Override
public void sound() {
System.out.println(name + " barks.");
}
// Additional method specific to Dog class
public void wagTail() {
System.out.println(name + " is wagging its tail.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of the Dog class
Dog dog = new Dog("Buddy");
// Calling methods from the Dog class and its superclass
dog.sound(); // Calls overridden method in Dog class
dog.wagTail(); // Calls method specific to Dog class
}
}
Explanation:
- Superclass
Animal
: Contains a fieldname
and a methodsound()
, which prints a general message. - Subclass
Dog
: Inherits from theAnimal
class using theextends
keyword. TheDog
class overrides thesound()
method to provide its own specific implementation (barking) and also adds a new methodwagTail()
which is unique to theDog
class. - Constructor and
super()
: In theDog
class, thesuper(name)
constructor is used to call the constructor of theAnimal
class to initialize thename
field.
Output:
bashCopyBuddy barks.
Buddy is wagging its tail.
Key Points:
- Inheritance: The
Dog
class inherits thename
field and thesound()
method from theAnimal
class. - Method Overriding: The
Dog
class overrides thesound()
method to provide a specific behavior for dogs. - Code Reusability: By using inheritance, we reuse the
name
field and the base methodsound()
without rewriting the code, making the program more efficient and maintainable.