0% found this document useful (0 votes)
11 views3 pages

Java Inheritance: Vehicle System Design

The document outlines a Java assignment focused on implementing inheritance through a Vehicle Management System. It requires the creation of a base class Vehicle and two subclasses, Car and Motorcycle, each with specific attributes and overridden methods. The assignment emphasizes key concepts such as method overriding, constructor chaining, and polymorphism, demonstrated through a main method that creates and displays vehicle objects.

Uploaded by

txil7a
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Java Inheritance: Vehicle System Design

The document outlines a Java assignment focused on implementing inheritance through a Vehicle Management System. It requires the creation of a base class Vehicle and two subclasses, Car and Motorcycle, each with specific attributes and overridden methods. The assignment emphasizes key concepts such as method overriding, constructor chaining, and polymorphism, demonstrated through a main method that creates and displays vehicle objects.

Uploaded by

txil7a
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Assignment: Inheritance – Vehicle

Management System
Objective:
To understand and implement inheritance in Java by creating a simple system that manages
different types of vehicles using parent and child classes.

Assignment Description:
You are asked to develop a small Java application for a vehicle management system. The
system should have:

1. A base class called Vehicle that contains general properties and behaviors common to all
vehicles.

2. Two subclasses called Car and Motorcycle that inherit from Vehicle and add specific
attributes and behavior.

3. Demonstrate the use of inheritance, method overriding, super keyword, and constructor
chaining.

Requirements:
1. The Vehicle class should include:
- Attributes: brand, year, and color
- Methods: displayInfo()
2. The Car class should:
- Add attribute: numberOfDoors
- Override the displayInfo() method to include car-specific information
3. The Motorcycle class should:
- Add attribute: hasSidecar (boolean)
- Override the displayInfo() method
4. In the main method:
- Create objects of Car and Motorcycle
- Call their methods to demonstrate polymorphism

Solution (with Comments):

// Base class (superclass)


class Vehicle {
String brand;
int year;
String color;

public Vehicle(String brand, int year, String color) {


[Link] = brand;
[Link] = year;
[Link] = color;
}

public void displayInfo() {


[Link]("Brand: " + brand);
[Link]("Year: " + year);
[Link]("Color: " + color);
}
}

// Subclass Car inherits from Vehicle


class Car extends Vehicle {
int numberOfDoors;

public Car(String brand, int year, String color, int numberOfDoors) {


super(brand, year, color);
[Link] = numberOfDoors;
}

@Override
public void displayInfo() {
[Link]();
[Link]("Number of Doors: " + numberOfDoors);
}
}

// Subclass Motorcycle inherits from Vehicle


class Motorcycle extends Vehicle {
boolean hasSidecar;

public Motorcycle(String brand, int year, String color, boolean hasSidecar) {


super(brand, year, color);
[Link] = hasSidecar;
}

@Override
public void displayInfo() {
[Link]();
[Link]("Has Sidecar: " + (hasSidecar ? "Yes" : "No"));
}
}

// Main class to test the inheritance


public class VehicleTest {
public static void main(String[] args) {
Car myCar = new Car("Toyota", 2020, "Red", 4);
[Link]("=== Car Info ===");
[Link]();

Motorcycle myBike = new Motorcycle("Harley-Davidson", 2022, "Black", true);


[Link]("\n=== Motorcycle Info ===");
[Link]();
}
}

Key Concepts Covered:


Concept Description

extends Used to create a subclass that inherits from


a superclass

super() Calls the constructor of the superclass

Method Overriding Subclass provides its own implementation


of a superclass method

Constructor Chaining Calling a superclass constructor from a


subclass

Polymorphism Different implementations of displayInfo()


for different subclasses

Common questions

Powered by AI

The 'super' keyword is used to call methods and constructors of the superclass from the subclass. In the Vehicle Management System, 'super' is used in the constructors of Car and Motorcycle to chain to the constructor of Vehicle and initialize common attributes before proceeding to initialize subclass-specific ones. This allows polymorphic behavior during object construction, ensuring that all attributes across the inheritance hierarchy are correctly initialized before an object becomes functional .

Constructor chaining is important as it enables initialization of object attributes defined in the superclass from the subclass, maintaining the integrity and consistency of data. In this system, constructor chaining is achieved using the super() call in the constructors of Car and Motorcycle. This call invokes the constructor of the Vehicle superclass to initialize the shared attributes (brand, year, and color) before initializing subclass-specific attributes (numberOfDoors for Car and hasSidecar for Motorcycle).

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. In the Vehicle Management System, the Vehicle class defines a displayInfo() method. Both subclasses, Car and Motorcycle, override this method to append their subclass-specific information (numberOfDoors and hasSidecar) to the output. This allows objects of Car and Motorcycle to produce different behaviors when displayInfo() is called, despite sharing the same method name as defined in Vehicle .

Method overriding allows subclasses such as Car and Motorcycle to provide their own specific implementation of the displayInfo() method, which is initially defined in the Vehicle superclass. In the main method, polymorphism is demonstrated by creating Car and Motorcycle objects and calling their respective displayInfo() methods. Despite the method calls being the same, the output varies according to the class-specific implementation due to method overriding, thereby illustrating polymorphism .

Inheritance enhances the system’s extensibility by allowing new types of vehicles to be added with minimal changes to the existing codebase. New subclasses can easily be created from the Vehicle superclass to extend its functionality. For instance, if a new type of vehicle, such as a Truck, needs to be added, a Truck class can be made by extending Vehicle, inheriting its attributes and methods, and adding truck-specific details. This provides a scalable structure that simplifies integration and expansion .

Constructor chaining facilitates correct instantiation by ensuring that objects are built hierarchically starting from the most general (superclass) to the most specific (subclass) parts. In the Vehicle Management System, Car and Motorcycle constructors utilize super() to invoke the Vehicle constructor, thereby setting common attributes like brand, year, and color before initializing their own specific attributes like numberOfDoors and hasSidecar. This ensures that all inherited characteristics are accurately initialized, maintaining object integrity across the class hierarchy .

The Vehicle Management System exemplifies encapsulation by bundling the data (attributes like brand, year, and color) and the methods (like displayInfo()) that operate on the data in one unit, i.e., the Vehicle class. Information hiding is achieved by using methods to manipulate internal states rather than allowing direct access, promoting controlled data access. Although the given example does not explicitly show private fields and getters/setters for access, the usage of constructors and method overriding implies intent for controlled interaction with object data, which aligns with the principles of encapsulation and information hiding .

The Vehicle Management System demonstrates polymorphism primarily through method overriding. Polymorphism allows methods to do different things based on the object it is acting upon. In the implementation, the displayInfo() method is overridden in both Car and Motorcycle classes. When the main method calls displayInfo() on Car and Motorcycle objects, each executes its own overridden version of the method, printing vehicle-specific information. By treating Car and Motorcycle objects as Vehicle objects, the program flexibly accommodates different types of vehicles with unique behaviors .

Inheritance allows the Vehicle class to define common properties and behaviors that are shared among all vehicle types, such as brand, year, and color attributes, as well as a displayInfo() method. The Car and Motorcycle subclasses can inherit these methods and attributes, reducing duplication. This promotes code reuse because the common logic does not need to be redefined in each subclass. Instead, the subclasses extend the Vehicle class and add only the specific attributes and behaviors needed for each, such as numberOfDoors in Car and hasSidecar in Motorcycle .

Polymorphism in the Vehicle Management System is utilized through method overriding, where the displayInfo() method is defined in the Vehicle class and overridden in both Car and Motorcycle. This allows the main method to invoke displayInfo() on Car and Motorcycle objects without knowing their specific types at compile time. As a result, the system can handle different vehicle types uniformly through a common interface, and additional vehicle types can be integrated seamlessly by subclassing Vehicle, without modifying existing code .

You might also like