Java Polymorphism Programming - Vehicle Class with Car and Bicycle Subclasses for Speed Control

Java Polymorphism: Exercise-2 with Solution

Write a Java program to create a class Vehicle with a method called speedUp(). Create two subclasses Car and Bicycle. Override the speedUp() method in each subclass to increase the vehicle's speed differently.

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

Polymorphism: Vehicle Class with Car and Bicycle Subclasses for Speed Control

In the above diagram, Vehicle is the base class, and Car and Motorcycle are the subclasses. The arrow represents the inheritance relationship, indicating that Car and Motorcycle inherit from Vehicle.

Since both Car and Motorcycle are subclasses of Vehicle, they inherit the speed field and the speedUp() method. However, they override the speedUp() method to provide their own implementation.

This polymorphic relationship allows objects of type Car and Motorcycle to be treated as objects of type Vehicle. This means that you can use a Car or Motorcycle object where a Vehicle object is expected, which enables flexibility and code reuse.

Sample Solution:

Java Code:

// Vehicle.java
// Base class Vehicle

class Vehicle {
    private int speed;

    public void speedUp() {
        speed += 10;

    public int getSpeed() {
        return speed;

// Car.java
// Subclass Car

class Car extends Vehicle {
    public void speedUp() {
        System.out.println("\nCar speed increased by 22 units.");
// Motorcycle.java
// Subclass Motorcycle

class Motorcycle extends Vehicle {
    public void speedUp() {
        System.out.println("Motorcycle speed increased by 12 units");
// Main.java
// Main class

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        Motorcycle motorcycle = new Motorcycle();
        System.out.println("Car initial speed: " + car.getSpeed());
        System.out.println("Motorcycle initial speed: " + motorcycle.getSpeed());
        System.out.println("\nCar speed after speeding up: " + car.getSpeed());
        System.out.println("Motorcycle speed after speeding up: " + motorcycle.getSpeed());

Sample Output:

Car initial speed: 0
Motorcycle initial speed: 0

Car speed increased by 22 units.
Motorcycle speed increased by 12 units

Car speed after speeding up: 10
Motorcycle speed after speeding up: 10


Flowchart: Base class Vehicle
Flowchart: Subclass Car
Flowchart: Subclass Motorcycle
Flowchart: Main class

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Animal Class with Bird and Cat Subclasses for Specific Sounds.
Next: Shape Class with Circle, Rectangle, and Triangle Subclasses for Area 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.


  • 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