Java Program for Inheritance

Java program for Inheritance
Reading Time: 8 minutes

Introduction:

This Java program illustrates the concept of inheritance, a fundamental aspect of object-oriented programming. Inheritance allows a class to inherit properties and behaviours from another class, promoting code reuse and creating a hierarchical relationship between classes. In this example, we have a parent class Animal and two child classes, Dog and Cat, which inherit from the Animal class.

Algorithm:

  1. Animal Class:
    • Define a class named Animal with two methods: eat() and sleep(). These methods represent basic behaviours common to all animals.
  2. Dog Class (Child of Animal):
    • Create a class named Dog that extends the Animal This implies that a Dog is a specialized type of Animal.
    • Introduce a method bark() specific to dogs.
  3. Cat Class (Child of Animal):
    • Create a class named Cat that also extends the Animal
    • Add a method meow() representing the unique behaviour of cats.
  4. InheritanceExample Class (Main Class):
    • Develop a class named InheritanceExample for testing the inheritance hierarchy.
    • Inside the main method:
      • Create objects of the Dog and Cat classes (myDog and myCat).
      • Demonstrate calling methods from the parent class (eat() and sleep()).
      • Illustrate calling methods from the Dog class (bark()).
      • Showcase calling methods from the Cat class (meow()).

Explanation:

  • The Animal class serves as a blueprint with general behaviours that are common to all animals, such as eating and sleeping.
  • The Dog and Cat classes extend the Animal class, inheriting its methods. They also introduce specific behaviors (bark() and meow()) that are unique to dogs and cats, respectively.
  • In the main method of the InheritanceExample class, objects of the Dog and Cat classes are created, demonstrating how inherited methods and class-specific methods can be called.
  • This program highlights the reusability and organization benefits of inheritance in creating a hierarchy of classes with shared and specialized behaviours.

Code :

				
					// Parent class
class Animal {
    void eat() {
        System.out.println("The animal is eating");
    }
    void sleep() {
        System.out.println("The animal is sleeping");
    }
}
// Child class inheriting from Animal
class Dog extends Animal {
    void bark() {
        System.out.println("The dog is barking");
    }
}
// Another child class inheriting from Animal
class Cat extends Animal {
    void meow() {
        System.out.println("The cat is meowing");
    }
}
// Main class to test the inheritance
public class InheritanceExample {
    public static void main(String[] args) {
        // Creating objects of the child classes
        Dog myDog = new Dog();
        Cat myCat = new Cat();
        // Calling methods from the parent class
        myDog.eat();
        myDog.sleep();
        // Calling methods from the child class
        myDog.bark();
        // Calling methods from another child class
        myCat.eat();
        myCat.sleep();
        myCat.meow();
    }
}

				
			

In this example, Dog and Cat are child classes that inherit from the Animal class. They inherit the eat() and sleep() methods from the parent class, and each of them has their own additional method (bark() for Dog and meow() for Cat). The main method demonstrates creating objects of these classes and calling their methods.

FAQs :

Q: Can you provide an example of a Java program on inheritance?

A: Certainly! Check out the Java program above that demonstrates inheritance with a parent Animal class and child classes Dog and Cat.

Q: What is the significance of Java program inheritance?

A: Java program inheritance allows the creation of a hierarchical structure, promoting code reuse and organization. It enables a child class to inherit properties and behaviours from a parent class.

Q: How can I create a Java program of inheritance for my custom classes?

A: To create a Java program of inheritance, define a parent class with common attributes and methods. Then, extend it in child classes, adding specialized behaviours. Instantiate objects of the child classes to test inheritance.

Q: What are the key steps in developing a Java program using inheritance?

A: In a Java program using inheritance, start by defining a parent class. Extend it in child classes, introducing unique features. Create objects of the child classes in the main program to showcase the inherited and specialized behaviours.

Q: Can you explain Java program on multiple inheritance with an example?

A: Java doesn’t support multiple inheritance for classes, but it allows multiple inheritance through interfaces. Define interfaces with methods, and a class can implement multiple interfaces. Check out Java’s interface concept for a form of multiple inheritance.

Q: How can I implement a Java program for hybrid inheritance?

A: Hybrid inheritance involves a combination of single and multiple inheritances. In Java, you can achieve this by creating a class hierarchy with a mix of single inheritance (class extending one class) and multiple inheritance through interfaces. Design your classes accordingly to showcase hybrid inheritance.