Single Inheritance in java

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

1310

Program:

 class AnimalClass{
void eat(){System.out.println("eating...");}
}

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

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

Output:

barking...
eating...
Press any key to continue . . .

Explanation:

This code demonstrates single inheritance in Java. The AnimalClass is a base class that has a method named eat(). The Dog class extends the AnimalClass and has an additional method called bark(). The InheritanceClass contains the main method and creates an object of Dog class to access its methods.

When the program runs, it creates an object of the Dog class and calls the bark() method, which prints "barking..." to the console. It then calls the eat() method, which is inherited from the AnimalClass and prints "eating..." to the console. This demonstrates how the Dog class inherits the behavior of the AnimalClass and extends it with its own behavior.


This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.