Java Inheritance Programming - Vehicle class hierarchy
Write a Java program to create a vehicle class hierarchy. The base class should be Vehicle, with subclasses Truck, Car and Motorcycle. Each subclass should have properties such as make, model, year, and fuel type. Implement methods for calculating fuel efficiency, distance traveled, and maximum speed.
Sample Solution:
Java Code:
Explanation:
This is an abstract class that serves as the parent class for the other vehicle classes. It contains five private instance variables (make, model, year, fuelType, and fuelEfficiency) and six public methods (a constructor, five getters for the instance variables, and three abstract methods). The abstract methods are meant to be overridden by child classes with specific implementations.
Explanation:
The above class is a child class of Vehicle and extends the Vehicle class. It has an additional instance variable, cargoCapacity. The class has a constructor that accepts all the necessary parameters including cargo capacity. The class overrides the three abstract methods of the parent class and provides specific implementations of the methods.
Explanation:
The above class is another child class of Vehicle and extends the Vehicle class. It has an additional instance variable, numSeats. The class has a constructor that accepts all the necessary parameters including the number of seats. The class overrides the three abstract methods of the parent class and provides specific implementations of the methods.
Explanation:
This is also a child class of Vehicle and extends the Vehicle class. It has an additional instance variable, engineDisplacement. The class has a constructor that accepts all the necessary parameters. The class overrides the three abstract methods of the parent class and provides specific implementations of the methods.
Explanation:
The above class is the main class that contains the main method. It creates instances of each vehicle type, sets their values, and then prints their respective details and calculations such as fuel efficiency, distance traveled, and max speed.
Output:
Truck Model: Tatra 810 4x4 Fuel Efficiency: 8.075659532105526 mpg Distance Traveled: 65.50975012444003 miles Max Speed: 80.0 mph Car Model: Virtus Fuel Efficiency: 2.355 mpg Distance Traveled: 14.419665 miles Max Speed: 120.0 mph Motorcycle Model: Warrior200 Fuel Efficiency: 2.1 mpg Distance Traveled: 4.41 miles Max Speed: 80.0 mph
Flowchart of Vehicle class:
Flowchart of Child class Truck:
Flowchart of Child class Car:
Flowchart of Child class Motorcycle:
Flowchart of Vehicle class Main:
For more Practice: Solve these Related Problems:
- Write a Java program where the "Truck" subclass calculates load capacity based on cargo weight.
- Write a Java program where the "Car" subclass implements a method to estimate travel time for long trips.
- Write a Java program where the "Motorcycle" subclass includes a method to check tire pressure before a ride.
- Write a Java program where the "Vehicle" class introduces a method for maintenance scheduling.
Go to:
Java Code Editor:
Contribute your code and comments through Disqus.
PREV : Create a class called Shape with methods called getPerimeter() and getArea().
NEXT : Employee Class Hierarchy.
What is the difficulty level of this exercise?