w3resource

Java Polymorphism - Sports Class with Football, Basketball, and Rugby Subclasses for Playing Statements

Java Polymorphism: Exercise-5 with Solution

Write a Java program to create a base class Sports with a method called play(). Create three subclasses: Football, Basketball, and Rugby. Override the play() method in each subclass to play a specific statement for each sport.

In the given exercise, here is a simple diagram illustrating polymorphism implementation:

Polymorphism: Sports Class with Football, Basketball, and Rugby Subclasses for Playing Statements

In the above diagram, the Sports class is the base class with a play() method. The Football, Basketball, and Rugby classes are subclasses that inherit from the Sports class and override the play() method to provide their specific implementation.

Sample Solution:

Java Code:

// Sports.java
// Base class Sports
class Sports {
    public void play() {
        System.out.println("Playing a sport...\n");
    }
}

// Football.java
// Subclass Football
class Football extends Sports {
    @Override
    public void play() {
        System.out.println("Playing football...");
    }
}
// Basketball.java
// Subclass Basketball
class Basketball extends Sports {
    @Override
    public void play() {
        System.out.println("Playing basketball...");
    }
}
// Rugby.java
// Subclass Rugby

class Rugby extends Sports {
    @Override
    public void play() {
        System.out.println("Playing rugby...");
    }
}
// Main.java
// Main class
public class Main {
    public static void main(String[] args) {
        Sports sports = new Sports();
        Football football = new Football();
        Basketball basketball = new Basketball();
        Rugby rugby = new Rugby();

        sports.play();
        football.play();
        basketball.play();
        rugby.play();
    }
}

Sample Output:

Playing a sport...

Playing football...
Playing basketball...
Playing rugby...

Flowchart:

Flowchart: Base class Sports
Flowchart: Subclass Football
Flowchart: Subclass Basketball
Flowchart: Subclass Rugby
Flowchart: Main class

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Shape Class with Circle, Rectangle, and Triangle Subclasses for Area Calculation.
Next: Shape Class with Circle, Rectangle, and Triangle Subclasses for Area and Perimeter Calculation.

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.

HashSet

  • 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.

TreeSet

  • 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