w3resource

Java Abstract Classes - Abstract Animal Class with Lion and Tiger Subclasses

Java Abstract Class: Exercise-1 with Solution

Write a Java program to create an abstract class Animal with an abstract method called sound(). Create subclasses Lion and Tiger that extend the Animal class and implement the sound() method to make a specific sound for each animal.

Above exercise represents the class structure and the relationship between the abstract class and its subclasses.

In the following code, the Animal class is shown as an abstract class with the abstract method sound(). The Lion and Tiger classes are subclasses of Animal and provide their own sound() implementations. The arrow indicates the inheritance relationship, where Lion and Tiger inherit from Animal.

Sample Solution:

Java Code:

// Animal.java

// Define an abstract class named Animal
abstract class Animal {
    // Declare an abstract method named sound
    public abstract void sound();
}

The above code defines an abstract class named Animal. An abstract class is a class that cannot be instantiated on its own and must be subclassed. The Animal class contains an abstract method named sound(), which means any subclass of Animal must provide an implementation for the sound() method. Here is a brief explanation of each part:

  • abstract class Animal: This line defines an abstract class called Animal.
  • public abstract void sound();: This line declares an abstract method named sound that must be implemented by any concrete subclass of Animal.
// Lion.java

// Define a subclass named Lion that extends Animal
class Lion extends Animal {
    // Override the abstract method sound from the Animal class
    @Override
    public void sound() {
        // Print "Lion roars!" to the console
        System.out.println("Lion roars!");
    }
} 

The above code defines a subclass named Lion that extends the abstract class Animal. The Lion class provides an implementation for the abstract method sound() defined in the Animal class. Here's a brief explanation of each part:

  • class Lion extends Animal: This line declares that Lion is a subclass of Animal.
  • @Override: This annotation indicates that the following method overrides a method declared in a superclass.
  • public void sound(): This line defines the implementation of the abstract method sound() from the Animal class.
  • System.out.println("Lion roars!");: This line prints "Lion roars!" to the console, providing the specific behavior for the Lion class.
// Tiger.java

// Define a subclass named Tiger that extends Animal
class Tiger extends Animal {
    // Override the abstract method sound from the Animal class
    @Override
    public void sound() {
        // Print "Tiger growls!" to the console
        System.out.println("Tiger growls!");
    }
} 

The above code defines a subclass named Tiger that extends the abstract class Animal. The Tiger class provides an implementation for the abstract method sound() defined in the Animal class. Here's a brief explanation of each part:

  • class Tiger extends Animal: Declares that Tiger is a subclass of Animal.
  • @Override: Indicates that the following method overrides a method declared in a superclass.
  • public void sound(): Defines the implementation of the abstract method sound() from the Animal class.
  • System.out.println("Tiger growls!");: Prints "Tiger growls!" to the console, providing the specific behavior for the Tiger class.
// Main.java

// Define the Main class
public class Main {
    // Main method to run the program
    public static void main(String[] args) {
        // Create an instance of Lion and assign it to an Animal reference
        Animal lion = new Lion();
        // Call the sound method on the Lion instance
        lion.sound(); // Output: Lion roars!

        // Create an instance of Tiger and assign it to an Animal reference
        Animal tiger = new Tiger();
        // Call the sound method on the Tiger instance
        tiger.sound(); // Output: Tiger growls!
    }
}

Output:

Lion roars!
Tiger growls!

Flowchart:

Flowchart: Abstract class Animal
Flowchart: Subclass Lion
Flowchart: Subclass Tiger
Flowchart: Subclass Main

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Java Abstract Classes Exercises Home.
Next: Abstract Shape Class with Circle and Triangle Subclasses.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/java-abstract-class-exercise-1.php