Reading:
Java Inheritance Tutorial: Learn with Example Programs

Java Inheritance Tutorial: Learn with Example Programs

Metamug
Java Inheritance Tutorial: Learn with Example Programs

Inheritance is a key concept in Java programming that allows you to create new classes based on existing ones. By inheriting properties and methods from a parent class, you can save time and effort in your coding. In this tutorial, we'll explore the basics of inheritance in Java and provide example programs to help you grasp the concept.

In Java, inheritance allows us to reuse code by creating subclasses that inherit all the properties and methods of a parent class.

What is inheritance?

Inheritance is a mechanism in Java that allows you to create new classes based on existing ones. The new class, known as the child class or subclass, inherits properties and methods from the parent class or superclass. This means that the child class can reuse code from the parent class, saving time and effort in the coding process. Inheritance is a fundamental concept in Java programming and is used extensively in object-oriented programming.

In inheritance, we inherit the properties and methods of a parent class. This means that any changes made to the parent class will automatically be inherited by its child classes.

How inheritance works?

Inheritance is one of the fundamental concepts of object oriented programming (OOP). It allows us to reuse code and reduce the number of lines of codes required to write programs. Inheritance works by creating a child object based on the parent object. This means that any properties and methods defined in the parent will also be available to the child. It's not necessary to define every property and method in the child class because the parent class has them.

//base class
public interface Animal{

}
//sub class
public class Dog extends Animal{

}

//sub class
public class Cat extends Animal{

}

Types of Inheritance

There are four types of inheritance in Java: single inheritance, multi-level inheritance, hierarchical inheritance, and multiple inheritance. Single inheritance is when a child class inherits from only one parent class. Multi-level inheritance is when a child class inherits from a parent class, which in turn inherits from another parent class. Hierarchical inheritance is when multiple child classes inherit from the same parent class. Multiple inheritance is when a child class inherits from multiple parent classes. Java does not support multiple inheritance, but it can be achieved through interfaces.

When should we use inheritance ?

There are several reasons why we use inheritance. One reason is to reuse code. If we have a lot of similar code, we can put it into a base class and then extend that class with different features. Another reason is to make our programs more flexible. If we need to add new functionality later, we can just add it to the base class instead of having to rewrite everything.

Object creation with superclass reference

Creating objects with superclass reference, allows a common type for the objects.

    Animal tom = new Cat();
    Animal milo = new Dog();

    List<Animal> animals = new ArrayList<>();
    animals.add(tom); //added cat object
    animals.add(milo) //added dog object

    for(Animal a:animals){

    }

It is easier to iterate over the objects as they are of the same type. Using the instanceof keyword we can check which subclass the object belongs to in the for loop.

Example programs to demonstrate inheritance in Java

Here is an example of inheritance in Java. We start with a base class called Animal. Then we create two subclasses of Animal called Dog and Cat. In both cases, we inherit from Animal.

//base class
interface Animal{
    public String speak();
}
//sub class
class Dog extends Animal{
    @Override
    public String speak(){
        return bark();
    }
}

//sub class
class Cat extends Animal{
    @Override
    public String speak(){
        return meow();
    }
}

public class Main{
    public static void main(String[] args) {
        Animal tom = new Cat();
        Animal milo = new Dog();

        List<Animal> animals = new ArrayList<>();
        animals.add(tom); //added cat object
        animals.add(milo) //added dog object

        for(Animal a:animals){
            System.out.println(a.speak());
        }
    }
}

The above speak() method shows polymorphic behavior depending on subclass object

Best practices for using inheritance in java

Inheritance is a powerful tool in Java programming, but it’s important to use it correctly to avoid creating complex and difficult-to-maintain code. Here are some best practices for using inheritance in your Java programs:

  1. Use inheritance to create hierarchies of related classes, not just to reuse code.
  2. Keep your inheritance hierarchies shallow and simple.
  3. Avoid using multiple inheritance, as it can lead to complex and confusing code.
  4. Use abstract classes and interfaces to define common behavior and ensure consistency across your classes.
  5. Avoid using inheritance for code reuse when composition or delegation would be a better option.
  6. Always test your inheritance hierarchies thoroughly to ensure they work as expected.
  7. Document your inheritance hierarchies clearly to help other developers understand how they work.


Icon For Arrow-up
Comments

Post a comment