Inheritance in Java

Inheritance is one of the most important concepts in object-oriented programming (OOP). It allows us to create new classes that are based on existing classes. This can save us time and effort, as we can reuse the code and functionality of the existing class.

What is inheritance?

inheritance in java

In Java, inheritance is the mechanism by which one class can inherit the features (fields and methods) of another class. The class that inherits the features is called the subclass, and the class that is being inherited from is called the superclass.

Why is inheritance important?

There are several reasons why inheritance is important in Java.

First, it allows us to reuse code. This can save us a lot of time and effort, as we don’t have to rewrite code that already exists.

Second, inheritance can help us to organize our code in a logical way. By grouping related classes together, we can make our code easier to understand and maintain.

Third, inheritance can help us to create more complex and powerful classes. By inheriting from existing classes, we can add new features and functionality to our own classes.

Types of inheritance

There are four types of inheritance:

  • Single-level inheritance: This is the simplest type of inheritance. A subclass inherits from a single superclass.
  • Multi-level inheritance: This is when a subclass inherits from another subclass.
  • Hierarchical inheritance: This is when there is a hierarchy of classes, with each class inheriting from a single superclass.
  • Multiple inheritance: This is when a subclass inherits from multiple superclasses.

Single-level inheritance

In single-level inheritance, a subclass inherits from a single superclass. This is the simplest type of inheritance, and it is the most common type of inheritance in Java.

Single Level inheritance

Example of single-level inheritance

The following code shows an example of single-level inheritance in Java:

class Animal {
  public void eat() {
    System.out.println("Animal is eating");
  }
}

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

public class Main {
  public static void main(String[] args) {
    Dog dog = new Dog();
    dog.eat();
    dog.bark();
  }
}

In this code, the Dog class inherits from the Animal class. This means that the Dog class inherits all of the methods and fields of the Animal class. In the main() method, we create a Dog object and call the eat() and bark() methods. The eat() method is inherited from the Animal class, and the bark() method is a method that is specific to the Dog class.

Advantages of single-level inheritance

The advantages of single-level inheritance include:

  • It can save us time and effort, as we can reuse the code and functionality of the existing class.
  • It can help us to organize our code in a logical way.
  • It can help us to create more complex and powerful classes.

Disadvantages of single-level inheritance

The disadvantages of single-level inheritance include:

  • It can be difficult to understand the relationships between classes.
  • It can be difficult to modify the code in the superclass, as this may affect the code in the subclass.

Multi-level inheritance

In multi-level inheritance, a subclass inherits from another subclass. This is a more complex type of inheritance than single-level inheritance, but it can be very useful for organizing our code.

Multi Level Inheritance

Example of multi-level inheritance

The following code shows an example of multi-level inheritance in Java:

class Animal {
  public void eat() {
    System.out.println("Animal is eating");
  }
}

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

class GermanShepherd extends Dog {
  public void herd() {
    System.out.println("German Shepherd is herding");
  }
}

public class Main {
  public static void main(String[] args) {
    GermanShepherd shepherd = new GermanShepherd();
    shepherd.eat();
    shepherd.bark();
    shepherd.herd();
  }
}

Hierarchical inheritance

In hierarchical inheritance, there is a hierarchy of classes, with each class inheriting from a single superclass. This is a more complex type of inheritance than single-level or multi-level inheritance, but it can be very useful for organizing our code.

Hierarchical Inheritance

Example of hierarchical inheritance

The following code shows an example of hierarchical inheritance in Java:

class Animal {
  public void eat() {
    System.out.println("Animal is eating");
  }
}

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

class Cat extends Animal {
  public void meow() {
    System.out.println("Cat is meowing");
  }
}

public class Main {
  public static void main(String[] args) {
    Dog dog = new Dog();
    dog.eat();
    dog.bark();

    Cat cat = new Cat();
    cat.eat();
    cat.meow();
  }
}

In this code, we have three classes: Animal, Dog, and Cat. The Animal class is the superclass, and the Dog and Cat classes are subclasses. The Dog class inherits from the Animal class, and the Cat class inherits from the Animal class. This means that both the Dog and Cat classes can eat, as they inherit the eat() method from the Animal class. However, the Dog class can also bark, and the Cat class can also meow, as these are methods that are specific to each subclass.

Multiple inheritance

In multiple inheritance, a subclass inherits from multiple superclasses. This is not supported in Java, as it can lead to ambiguity.

There is a separate post on why multiple inheritance is not supported in Java.

Conclusion

Inheritance is a powerful tool that can be used to improve the code organization and reusability in Java. It is important to understand the different types of inheritance and their advantages and disadvantages before using them in our code.

FAQs

What is the main benefit of using inheritance in Java?

The main benefit of inheritance is code reuse. It allows us to create new classes that inherit attributes and behaviors from existing classes, reducing redundancy and promoting modularity in our code.

What is the “is-a” relationship in inheritance?

The “is-a” relationship is a key concept in inheritance, indicating that a subclass is a specialized version of its superclass. For example, if we have a Vehicle superclass and a Car subclass, we can say that “a car is a vehicle.”

What is the “super” keyword used for in Java inheritance?

The super keyword is used to access members (fields, methods, and constructors) of the superclass from within the subclass. It is particularly useful for calling superclass constructors and accessing overridden methods or fields.

Are all members of the superclass accessible in the subclass?

No, not all members of the superclass are accessible in the subclass. Members with private access modifiers are not accessible in the subclass. Members with default, protected, or public access modifiers are accessible, depending on their visibility.

When should I use inheritance in Java?

Inheritance should be used when you want to create a new class that shares attributes and behaviors with an existing class. It’s useful for modeling relationships such as “is-a” and for promoting code reuse.