Java Inheritance

Java Inheritance is a core concept of Object-Oriented Programming (OOP) that allows a class (child) to acquire the properties and behaviors (methods) of another class (parent). This makes the code more reusable, organized, and easy to maintain. When you define a new class in Java, you can make it inherit from an existing class to utilize its methods and variables without rewriting the code.

How It Works

In inheritance, the parent class is called the superclass, and the child class is called the subclass. The subclass inherits all the attributes and methods of the superclass.

Example:

// Parent Class
class Animal {
void eat() {
System.out.println("This animal eats food");
}
}
// Child Class
class Dog extends Animal {
void bark() {
System.out.println(“The dog barks”);
}
}

public class TestInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // inherited method from Animal class
dog.bark(); // method from Dog class
}
}

Output:

This animal eats food
The dog barks

In the above example, the Dog class is a subclass that inherits the eat method from the Animal class, while adding its own bark method.

Types of Inheritance in Java

  1. Single Inheritance: A class inherits from one superclass. (e.g., Dog inherits from Animal)
  2. Multilevel Inheritance: A class inherits from a superclass, and another class inherits from it. (e.g., Dog -> Bulldog)
  3. Hierarchical Inheritance: Multiple classes inherit from the same superclass. (e.g., Dog and Cat inherit from Animal)
  4. Multiple Inheritance: Java doesn’t support multiple inheritance directly (a class cannot inherit from more than one class). However, it can be achieved using interfaces.

Advantages of Using Inheritance

  1. Code Reusability: You can write common functionalities once in the superclass and reuse them in subclasses, reducing code duplication.
  2. Easy Maintenance: Changes made in the superclass will be reflected in all subclasses, making the code easier to maintain.
  3. Extensibility: Subclasses can add or override methods to extend or modify the behavior of the superclass, making the system more flexible.

Overriding and Inheritance

One of the most powerful features of inheritance is the ability to override methods. When a subclass needs to provide a specific implementation of a method that is already defined in its superclass, it can override that method.

Example:

class Animal {
void makeSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println(“The dog barks”);
}
}
Here, the Dog class overrides the makeSound method of the Animal class to provide a specific implementation.

Real-World Scenario

Imagine developing software for different types of vehicles. You can create a base Vehicle class with general features like speed, color, and fuel consumption methods. Each specific vehicle (car, truck, motorcycle) can inherit from Vehicle, extending or overriding the general properties and behaviors to include their specific features.

class Vehicle {
int maxSpeed = 120;
void drive() {
System.out.println(“Driving at “ + maxSpeed + ” km/h”);
}
}class Car extends Vehicle {
int maxSpeed = 180;

@Override
void drive() {
System.out.println(“Car drives at “ + maxSpeed + ” km/h”);
}
}

Java Inheritance is essential for writing clean, reusable, and maintainable code. It allows the creation of a natural hierarchy for classes, promoting code reuse and simplifying changes across related classes. Whether building small programs or large systems, understanding how inheritance works can significantly improve your coding skills.

Scroll to Top