Summary: In this tutorial, you will learn about inheritance and its types in Java with the help of examples.

Inheritance in Java

In Java, inheritance is a mechanism that allows one class to acquire the properties and behaviors of another class. It is an important aspect of object-oriented programming and is a way to reuse code and to define a hierarchy of classes.

To use inheritance in Java, you must use the extends keyword in the class definition. For example:

class Animal {
    // properties and methods go here
}

class Dog extends Animal {
    // additional properties and methods go here
}

In this example, the Dog class is a subclass of the Animal class. The Animal class is the superclass of the Dog class. The Dog class inherits all of the properties and methods of the Animal class, and can also have additional properties and methods of its own.

Example: Java Inheritance

Here is an example of inheritance in Java:

// This is the superclass
class Animal {
    protected String name;
    protected int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat() {
        System.out.println(name + " is eating.");
    }

    public void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

// This is the subclass
class Dog extends Animal {
    private String breed;

    public Dog(String name, int age, String breed) {
        super(name, age);
        this.breed = breed;
    }

    public void bark() {
        System.out.println(name + " is barking.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a new Dog object
        Dog dog = new Dog("Fido", 3, "Labrador");

        // Use the methods inherited from the Animal superclass
        dog.eat();
        dog.sleep();

        // Use the method defined in the Dog subclass
        dog.bark();
    }
}

This example defines an Animal superclass with two methods, eat and sleep, and two instance variables, name and age. It also defines a Dog subclass that extends the Animal superclass. The Dog subclass has a bark method and a breed instance variable.

In the main method, we create a new Dog object and use the eat, sleep, and bark methods. The Dog object inherits the eat and sleep methods from the Animal superclass, and has its own implementation of the bark method.

protected Members in Inheritance

In Java, a protected member is a member of a class (such as a field or a method) that is visible to subclasses of that class, as well as to other classes in the same package.

It is more restricted in its visibility than a member with the public access modifier, but it is more visible than a member with the private access modifier.

Here is an example of a class with a protected field:

class Animal {
    protected int age;
}

In this example, the age field is protected, which means that it is visible to subclasses of the Animal class, as well as to other classes in the same package as the Animal class. However, it is not visible to classes outside of the package.

Here is an example of a subclass that can access the protected field of its superclass:

class Dog extends Animal {
    public void printAge() {
        System.out.println("Age: " + age);
    }
}

In this example, the Dog class has a printAge() method that can access the age field of its superclass, because it is protected.

The protected access modifier can be useful in inheritance, because it allows subclasses to access certain members of their superclass that are not intended to be visible to other classes.

This can help to encapsulate the implementation details of the superclass, while still allowing the subclass to access the necessary information to function correctly.

‘is a’ Relationship in Inheritance

In Java, inheritance is an “is-a” relationship, refers to the relationship between a subclass and its superclass. It means that the subclass is a specific type of the superclass.

For example, consider the following class hierarchy:

class Animal {
    // properties and methods go here
}

class Mammal extends Animal {
    // additional properties and methods go here
}

class Dog extends Mammal {
    // additional properties and methods go here
}

In this example, the “is a” relationship is represented by the inheritance hierarchy. A Dog “is a” Mammal, and a Mammal “is an” Animal.

This means that a Dog object has all of the properties and methods of a Mammal object, as well as all of the properties and methods of an Animal object.

The “is a” relationship is an important concept in object-oriented programming, because it allows you to create a hierarchy of classes that reflects the real-world relationships between different types of objects.

It also allows you to reuse code and define common behavior in a superclass, which can save a lot of time and effort when you are designing your program.

Types on Inheritance in Java

In Java, there are four types of inheritance:

1. Single inheritance

This is the most basic type of inheritance, in which a subclass has only one superclass. For example:

class Animal {
    // properties and methods go here
}

class Dog extends Animal {
    // additional properties and methods go here
}

2. Multilevel inheritance

This is a type of inheritance in which a subclass can have a superclass, which in turn can have another superclass, and so on. For example:

class Animal {
    // properties and methods go here
}

class Mammal extends Animal {
    // additional properties and methods go here
}

class Dog extends Mammal {
    // additional properties and methods go here
}

In this example, the Dog class has a superclass of Mammal, which has a superclass of Animal.

3. Hierarchical inheritance

This is a type of inheritance in which a single class serves as a superclass for multiple subclasses. For example:

class Animal {
    // properties and methods go here
}

class Dog extends Animal {
    // additional properties and methods go here
}

class Cat extends Animal {
    // additional properties and methods go here
}

In this example, the Animal class is the superclass for both the Dog and Cat classes.

4. Multiple inheritance

This is a type of inheritance in which a subclass has multiple superclasses. Java does not support multiple inheritance, because it can lead to ambiguity and complex interactions between the multiple superclasses.

However, you can use interface inheritance and delegation to achieve similar effects.

For example, you could define multiple interfaces with the methods and properties that you want to inherit, and then have your class implement those interfaces.

This would allow you to reuse code from multiple sources, but it would not create a direct class hierarchy like traditional inheritance does. You will learn to do these in later tutorials.

Why use Inheritance?

There are several benefits to using inheritance in object-oriented programming:

  1. Code reuse: Inheritance allows you to create a new class that is a modified version of an existing class, rather than starting from scratch. This can save a lot of time and effort, because you can reuse code that has already been tested and debugged.
  2. Method overriding: Inheritance allows you to create a new method with the same name and parameter list as a method in the superclass. This can be useful when you want to provide a different implementation of a method that is more specific to a particular subclass.
  3. Improved organization: Inheritance allows you to create a hierarchy of classes, which can make it easier to organize your code and understand the relationships between different types of objects.

Overall, inheritance is a powerful tool that can help you design more flexible and modular code, which can make your programs easier to maintain and extend over time.


Leave a Reply