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.
Next: Abstract Employee class with Manager and Programmer subclasses.
What is the difficulty level of this exercise?
Java: Tips of the Day
DropElements
Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.
Loop through the array, using Arrays.copyOfRange() to drop the first element of the array until the returned value from the function is true. Returns the remaining elements.
public static int[] dropElements(int[] elements, IntPredicate condition) { while (elements.length > 0 && !condition.test(elements[0])) { elements = Arrays.copyOfRange(elements, 1, elements.length); } return elements; }
Ref: https://bit.ly/37YWqzD
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- 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
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