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
// Abstract class Animal
abstract class Animal {
    public abstract void sound();

// Lion.java
// Subclass Lion
class Lion extends Animal {
    public void sound() {
        System.out.println("Lion roars!");
// Tiger.java
// Subclass Tiger
class Tiger extends Animal {
    public void sound() {
        System.out.println("Tiger growls!");
// Main.java
// Subclass Main

public class Main {
    public static void main(String[] args) {
        Animal lion = new Lion();
        lion.sound(); // Output: Lion roars!

        Animal tiger = new Tiger();
        tiger.sound(); // Output: Tiger growls!

Sample Output:

Lion roars!
Tiger growls!


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?

Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

Hashset vs Treeset:

HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet.


  • the class offers constant time performance for the basic operations (add, remove, contains and size).
  • it does not guarantee that the order of elements will remain constant over time
  • iteration performance depends on the initial capacity and the load factor of the HashSet.
  • It's quite safe to accept default load factor but you may want to specify an initial capacity that's about twice the size to which you expect the set to grow.


  • guarantees log(n) time cost for the basic operations (add, remove and contains)
  • guarantees that elements of set will be sorted (ascending, natural, or the one specified by you via its constructor) (implements SortedSet)
  • doesn't offer any tuning parameters for iteration performance
  • offers a few handy methods to deal with the ordered set like first(), last(), headSet(), and tailSet() etc

Important points:

  • Both guarantee duplicate-free collection of elements
  • It is generally faster to add elements to the HashSet and then convert the collection to a TreeSet for a duplicate-free sorted traversal.
  • None of these implementations are synchronized. That is if multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
  • LinkedHashSet is in some sense intermediate between HashSet and TreeSet. Implemented as a hash table with a linked list running through it, however,it provides insertion-ordered iteration which is not same as sorted traversal guaranteed by TreeSet.

So a choice of usage depends entirely on your needs but I feel that even if you need an ordered collection then you should still prefer HashSet to create the Set and then convert it into TreeSet.

  • e.g. SortedSet<String> s = new TreeSet<String>(hashSet);

Ref: https://bit.ly/3d3waGh


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