w3resource

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:

Flowchart: Abstract class Shape
Flowchart: Subclass SavingsAccount
Flowchart: Subclass CurrentAccount
Flowchart: Subclass Main
Flowchart: Subclass Main

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?



Follow us on Facebook and Twitter for latest update.