Inheritance in Java: Concepts and Examples

Inheritance is one of the four fundamental principles of Object-Oriented Programming (OOP) in Java, allowing a class to inherit properties and behaviours from another class. It helps in reusing existing code, simplifying code maintenance, and establishing a hierarchy among classes. This blog will guide you through the concepts of inheritance in Java, its types, benefits, and practical examples.

Inheritance in Java

Table of Contents

  1. What is Inheritance?
  2. Benefits of Inheritance
  3. Types of Inheritance in Java
  4. How to Implement Inheritance in Java
  5. Method Overriding
  6. The super Keyword
  7. Practical Example: Implementing Inheritance
  8. Conclusion

1. What is Inheritance?

Inheritance is a mechanism in Java that allows one class (known as the subclass or derived class) to inherit fields and methods from another class (known as the superclass or parent class). The subclass can use and extend the functionality of the superclass. This feature promotes code reusability, as common features can be implemented in the superclass and then inherited by subclasses.

Example of Basic Inheritance Syntax

class Superclass {
// Fields and methods
}

class Subclass extends Superclass {
// Additional fields and methods
}

In this syntax, the Subclass inherits all non-private fields and methods from Superclass.

2. Benefits of Inheritance

Inheritance provides several advantages:

3. Types of Inheritance in Java

Java supports various types of inheritance, each serving different programming needs:

  1. Single Inheritance: A class inherits from one superclass. This is the most common form of inheritance in Java.
  2. Multilevel Inheritance: A class inherits from a superclass, which is also a subclass of another class, creating a hierarchy.
  3. Hierarchical Inheritance: Multiple classes inherit from the same superclass.

Note: Java does not support multiple inheritance (a subclass inheriting from multiple superclasses) directly due to complexity issues. However, it can be achieved using interfaces.

4. How to Implement Inheritance in Java

To implement inheritance in Java, use the extends keyword. This keyword indicates that the subclass will inherit fields and methods from the specified superclass.

Example of Single Inheritance

class Animal {
void eat() {
System.out.println("Eating...");
}
}

class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}

In this example, Dog is a subclass of Animal. This means that Dog inherits the eat() method from Animal, and it also has its own unique bark() method.

Using the Subclass

public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal class
myDog.bark(); // Defined in Dog class
}
}

Output:

Eating...
Barking...

5. Method Overriding

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The overridden method in the subclass has the same signature as the method in the superclass.

Example of Method Overriding

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow");
}
}

In this example, sound() is overridden in the Cat class to provide a unique implementation.

Using the Overridden Method

public class Main {
public static void main(String[] args) {
Animal myAnimal = new Cat();
myAnimal.sound(); // Calls the overridden method in Cat class
}
}

Output:

Meow

6. The super Keyword

The super keyword is used in a subclass to refer to its immediate superclass. It is commonly used to:

Example of Using super

class Animal {
void sound() {
System.out.println("Animal sound");
}
}

class Dog extends Animal {
@Override
void sound() {
super.sound(); // Calls the superclass method
System.out.println("Woof");
}
}

Here, super.sound() calls the sound() method from the Animal class before executing the sound() method in the Dog class.

Calling the Method

public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
}
}

Output:

Animal sound
Woof

7. Practical Example: Implementing Inheritance

Let’s take a practical example of how inheritance can be used to create a class hierarchy for different types of vehicles.

// Superclass
class Vehicle {
int wheels;
int speed;

void displayInfo() {
System.out.println("Wheels: " + wheels + ", Speed: " + speed + " km/h");
}
}

// Subclass
class Car extends Vehicle {
String model;

void showModel() {
System.out.println("Car Model: " + model);
}
}

// Subclass
class Motorcycle extends Vehicle {
boolean hasSidecar;

void checkSidecar() {
if (hasSidecar) {
System.out.println("Motorcycle has a sidecar.");
} else {
System.out.println("Motorcycle does not have a sidecar.");
}
}
}

In this example:

Using the Classes

public class Main {
public static void main(String[] args) {
// Creating a Car object
Car myCar = new Car();
myCar.wheels = 4;
myCar.speed = 180;
myCar.model = "Sedan";
myCar.displayInfo();
myCar.showModel();

// Creating a Motorcycle object
Motorcycle myMotorcycle = new Motorcycle();
myMotorcycle.wheels = 2;
myMotorcycle.speed = 120;
myMotorcycle.hasSidecar = false;
myMotorcycle.displayInfo();
myMotorcycle.checkSidecar();
}
}

Output:

Wheels: 4, Speed: 180 km/h
Car Model: Sedan
Wheels: 2, Speed: 120 km/h
Motorcycle does not have a sidecar.

8. Conclusion

Inheritance is a powerful concept in Java that promotes code reusability, allows polymorphism, and helps organise code into a clear hierarchy. By understanding how to create subclasses that inherit from superclasses, you can simplify code maintenance and build applications that are easier to manage and extend. Whether you’re building a simple application or a complex system, leveraging inheritance will help you create efficient, modular, and scalable Java programs. Experiment with different inheritance structures in your projects to deepen your understanding and experience with Java’s OOP capabilities.

Other Java Topics

Leave a Reply

Your email address will not be published. Required fields are marked *