Vehicle, Car, and Truck Classes in Java - Display Details
Write a Java program to create a class called "Vehicle" with attributes for make, model, and year. Create subclasses "Car" and "Truck" that add specific attributes like trunk size for cars and payload capacity for trucks. Implement a method to display vehicle details in each subclass.
Sample Solution:
Java Code:
Vehicle.java
The above Java code defines a Vehicle class with attributes for the make, model, and year of the vehicle. It includes:
- Attributes: Private fields for make, model, and year.
- Constructor: A constructor that initializes these attributes.
- Method to display details (displayDetails): Prints the vehicle's make, model, and year.
- Getter methods: getMake(), getModel(), and getYear() methods return the values of the make, model, and year attributes, respectively.
This class provides a basic blueprint for a vehicle with methods to access and display its details.
Car.java
This Java code defines a 'Car' subclass that extends the Vehicle class, adding an additional attribute and behavior specific to cars:
- Attribute: trunkSize to store the size of the car's trunk in cubic feet.
- Constructor: Initializes the 'Car' object with make, model, year, and trunk size. It calls the constructor of the Vehicle superclass to initialize the common attributes.
- Method to display details (displayDetails): Overrides the displayDetails method from the Vehicle class to include the trunk size. It first calls the superclass method to display common vehicle details, then adds the trunk size.
- Getter and Setter methods: getTrunkSize() returns the trunk size, and setTrunkSize() updates the trunk size if the value is positive.
This subclass enhances the Vehicle class by adding a specific feature for cars and overriding the method to display complete car details.
Truck.java
The above Java code defines a 'Truck' subclass that extends the Vehicle class, adding an attribute specific to trucks:
- Attribute: payloadCapacity to store the truck's payload capacity in tons.
- Constructor: Initializes the Truck object with make, model, year, and payload capacity. It calls the constructor of the Vehicle superclass to initialize the common attributes.
- Method to display details (displayDetails): Overrides the displayDetails method from the Vehicle class to include the payload capacity. It first calls the superclass method to display common vehicle details, then adds the payload capacity.
- Getter and Setter methods: getPayloadCapacity() returns the payload capacity, and setPayloadCapacity() updates the payload capacity if the value is positive.
This subclass extends the Vehicle class by adding a specific feature for trucks and overriding the method to display complete truck details.
Main.java
The above Java code defines a Main class with a main method used to test the Vehicle, Car, and Truck classes:
- Car Object Creation: An instance of the Car class is created with the make "Suzuki", model "Swift", year 2015, and trunk size 15.1 cubic feet. The displayDetails method is called to print the details of the car.
- Truck Object Creation: An instance of the Truck class is created with the make "Ford", model "F-150", year 2016, and payload capacity 3.5 tons. The displayDetails method is called to print the details of the truck.
This code tests the functionality of the Vehicle, Car, and Truck classes by creating instances of each and displaying their details.
Sample Output:
Vehicle Details: Make: Suzuki Model: Swift Year: 2015 Trunk Size: 15.1 cubic feet Vehicle Details: Make: Ford Model: F-150 Year: 2016 Payload Capacity: 3.5 tons
For more Practice: Solve these Related Problems:
- Write a Java program where the "Vehicle" class keeps a record of service history.
- Write a Java program to implement a feature in the "Car" subclass that calculates fuel efficiency based on distance traveled.
- Write a Java program where the "Truck" subclass enforces a maximum weight limit.
- Write a Java program to create a method in the "Vehicle" class that estimates resale value based on age and mileage.
Go to:
Java Code Editor:
Improve this sample solution and post your code through Disqus.
PREV : BankAccount and SavingsAccount Classes in Java.
NEXT : Java Program to Manage Customer Purchases with Discounts.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.