Java Abstract Classes - Abstract Animal Class with Lion, Tiger, and Deer Subclasses
Java Abstract Class: Exercise-4 with Solution
Write a Java program to create an abstract class Animal with abstract methods eat() and sleep(). Create subclasses Lion, Tiger, and Deer that extend the Animal class and implement the eat() and sleep() methods differently based on their specific behavior.
In this program, the Animal class is an abstract class that defines the abstract methods eat() and sleep(). The Lion, Tiger, and Deer classes extend the Animal class and provide their own implementations for the eat() and sleep() methods based on their specific behavior. The Main class demonstrates the usage of these classes by creating objects of each subclass and invoking the eat() and sleep() methods accordingly.
Sample Solution:
Java Code:
// Animal.java
// Abstract class Animal
abstract class Animal {
public abstract void eat();
public abstract void sleep();
}
// Lion.java
// Subclass Lion
class Lion extends Animal {
@Override
public void eat() {
System.out.println("Lion eats meat.");
}
@Override
public void sleep() {
System.out.println("Lion sleeps on grassland.");
}
}
class Tiger extends Animal {
@Override
public void eat() {
System.out.println("Tiger eats meat and occasionally hunts in water.");
}
@Override
public void sleep() {
System.out.println("Tiger sleeps in a hidden spot.");
}
}
// Deer.java
// Subclass Deer
class Deer extends Animal {
@Override
public void eat() {
System.out.println("Deer grazes on grass and leaves.");
}
@Override
public void sleep() {
System.out.println("Deer sleeps in open fields.");
}
}
// Main.java
// Subclass Main
public class Main {
public static void main(String[] args) {
Lion lion = new Lion();
lion.eat();
lion.sleep();
System.out.println();
Tiger tiger = new Tiger();
tiger.eat();
tiger.sleep();
System.out.println();
Deer deer = new Deer();
deer.eat();
deer.sleep();
}
}
Sample Output:
Lion eats meat. Lion sleeps on grassland. Tiger eats meat and occasionally hunts in water. Tiger sleeps in a hidden spot. Deer grazes on grass and leaves. Deer sleeps in open fields.
Explanation:
In this program, the Animal class is an abstract class that defines the abstract methods eat() and sleep(). The Lion, Tiger, and Deer classes extend the Animal class and provide their own implementations for the eat() and sleep() methods based on their specific behavior. The Main class demonstrates the usage of these classes by creating objects of each subclass and invoking the eat() and sleep() methods accordingly.
Flowchart:





Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Abstract Bank Account Class with Savings and Current Accounts.
What is the difficulty level of this exercise?
Java: Tips of the Day
How do I remove repeated elements from ArrayList?
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);
Of course, this destroys the ordering of the elements in the ArrayList.
Ref: https://bit.ly/3bYIjNC
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook