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:
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
// Define the Sports class
class Sports {
// Public method play that prints a message to the console
public void play() {
// Print "Playing a sport..." followed by a new line
System.out.println("Playing a sport...\n");
}
}
// Football.java
// Subclass Football
// Define the Football class as a subclass of Sports
class Football extends Sports {
// Override the play method from the Sports class
@Override
public void play() {
// Print "Playing football..." to the console
System.out.println("Playing football...");
}
}
// Basketball.java
// Subclass Basketball
class Basketball extends Sports { // Declare a subclass Basketball that extends the Sports class
@Override // Override the play method from the Sports class
public void play() { // Define the play method
System.out.println("Playing basketball..."); // Print "Playing basketball..." to the console
}
}
// Rugby.java
// Subclass Rugby
class Rugby extends Sports { // Declare a subclass Rugby that extends the Sports class
@Override // Override the play method from the Sports class
public void play() { // Define the play method
System.out.println("Playing rugby..."); // Print "Playing rugby..." to the console
}
}
// Main.java
// Main class
public class Main { // Declare the Main class
public static void main(String[] args) { // Define the main method
Sports sports = new Sports(); // Create an instance of the Sports class
Football football = new Football(); // Create an instance of the Football class
Basketball basketball = new Basketball(); // Create an instance of the Basketball class
Rugby rugby = new Rugby(); // Create an instance of the Rugby class
sports.play(); // Call the play method on the Sports instance
football.play(); // Call the play method on the Football instance
basketball.play(); // Call the play method on the Basketball instance
rugby.play(); // Call the play method on the Rugby instance
}
}
Output:
Playing a sport... Playing football... Playing basketball... Playing rugby...
Flowchart:
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?
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-polymorphism-exercise-5.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics