Write a Java program to Implement single inheritance


This Java program demonstrates single inheritance, which is a concept in object-oriented programming where a class inherits properties and behaviors from a single parent class (superclass). In this program, there are two classes: Animal and Dog, representing a basic hierarchy of animals and a specific animal, a dog. Here's an explanation of the program:

  • public class SingleInheritance: This is the main class that contains the main method, the entry point of the program.
  • public static void main(String[] args): The main method is the starting point of the program. It creates an instance of the Dog class and demonstrates single inheritance by calling methods from both the Animal and Dog classes.
  • Dog dog = new Dog("Buddy");: This line creates an object of the Dog class named "Buddy." The constructor of the Dog class is called, which, in turn, calls the constructor of the Animal class via the super(name) statement.
  • dog.eat();: This line calls the eat method of the Dog object, which is inherited from the Animal class. The output will be "Buddy is Eating."
  • dog.bark();: This line calls the bark method of the Dog object, which is specific to the Dog class. The output will be "Buddy is Barking."
  • class Animal: This is the parent class (superclass) that represents generic animal properties and behaviors. It has an instance variable name and a parameterized constructor that initializes the name.
  • public void eat(): The eat method of the Animal class simply prints a message indicating that the animal is eating, including its name.
  • class Dog extends Animal: This is the child class (subclass) that inherits from the Animal class. It represents a specific type of animal, a dog. The Dog class has its own method, bark, which is not present in the Animal class.
  • public Dog(String name): The constructor of the Dog class takes a name as a parameter and calls the constructor of the Animal class (its parent class) using super(name) to initialize the name property.
  • public void bark(): The bark method of the Dog class prints a message indicating that the dog is barking, including its name.

This program demonstrates the concept of single inheritance, where the Dog class inherits properties and methods from the Animal class, and it also defines its own specific behavior. When an instance of the Dog class is created, it can access both the generic behavior of animals and the specific behavior of dogs.


Source Code

public class SingleInheritance // Main class
{
	public static void main(String[] args)
	{        
		Dog dog = new Dog("Buddy");	 // Create an instance of Dog        
 
		dog.eat();	// Call methods from Animal class        
		dog.bark();	// Call methods from Dog class
	}
}
class Animal // Parent class (Superclass)
{
	protected String name;
 
	public Animal(String name)
	{
		this.name = name;
	}
 
	public void eat()
	{
		System.out.println(name + " is Eating");
	}
}
class Dog extends Animal // Child class (Subclass) inheriting from Animal
{
	public Dog(String name)
	{
		super(name);
	}
 
	public void bark()
	{
		System.out.println(name + " is Barking");
	}
}

Output

Buddy is Eating
Buddy is Barking