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

Java Polymorphism Assignment Guide

The document outlines a Java assignment focused on exploring polymorphism through a Vehicle Management System. It includes objectives such as understanding method overloading and overriding, applying the 'final' keyword, and demonstrating the 'protected' access modifier. Students are instructed to implement a base class Vehicle and subclasses Car and Truck, along with tasks to enhance their understanding of polymorphism and class interactions.

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)
14 views3 pages

Java Polymorphism Assignment Guide

The document outlines a Java assignment focused on exploring polymorphism through a Vehicle Management System. It includes objectives such as understanding method overloading and overriding, applying the 'final' keyword, and demonstrating the 'protected' access modifier. Students are instructed to implement a base class Vehicle and subclasses Car and Truck, along with tasks to enhance their understanding of polymorphism and class interactions.

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: Exploring

Polymorphism
Objectives
- Understand method overloading and overriding.
- Apply 'final' keyword to prevent method/variable overriding.
- Demonstrate use of 'protected' access modifier.
- Implement upcasting to access subclass objects via superclass reference.

Instructions
Write a Java program that simulates a simple Vehicle Management System using
polymorphism. Implement the following:

Class Definitions

1. Base Class: Vehicle

public class Vehicle {


protected String brand;

public Vehicle(String brand) {


[Link] = brand;
}

public void displayInfo() {


[Link]("Vehicle Brand: " + brand);
}

// Overloaded methods
public void service() {
[Link]("Generic vehicle service.");
}

public void service(String serviceType) {


[Link]("Vehicle service: " + serviceType);
}

// final method
public final void fuelType() {
[Link]("Uses petrol or diesel.");
}
}

2. Subclass: Car

public class Car extends Vehicle {


private String model;

public Car(String brand, String model) {


super(brand);
[Link] = model;
}

@Override
public void displayInfo() {
[Link]("Car Brand: " + brand + ", Model: " + model);
}

public void honk() {


[Link]("Car horn: Beep Beep!");
}
}

3. Subclass: Truck

public class Truck extends Vehicle {


private int capacity;

public Truck(String brand, int capacity) {


super(brand);
[Link] = capacity;
}

@Override
public void displayInfo() {
[Link]("Truck Brand: " + brand + ", Capacity: " + capacity + " tons");
}
}
Main Class

public class Main {


public static void main(String[] args) {
Vehicle v1 = new Car("Toyota", "Corolla");
Vehicle v2 = new Truck("Volvo", 10);

[Link]();
[Link]();

[Link]();

[Link]();
[Link]("Full Service");

if (v1 instanceof Car) {


Car c = (Car) v1;
[Link]();
}
}
}

Tasks for the Student


1. Explain the difference between overloading and overriding in your own words.
2. Modify the Truck class to include a new protected variable driverName, and add a
method to display it.
3. Add one more subclass, Motorbike, and override the displayInfo method.
4. Try to override the fuelType() method in any subclass and observe the result.
5. Add a method in Main that accepts a Vehicle parameter and calls displayInfo() —
demonstrate polymorphism in action.

Deliverables
- Source code (.java files)
- Answers to theory questions (Word or PDF)

Common questions

Powered by AI

The `instanceof` check is a safety measure used to confirm the actual object's type before performing downcasting, as seen in Main where `if (v1 instanceof Car)` ensures v1 is indeed a Car before downcasting it. This prevents ClassCastException at runtime by ensuring the variable is an instance of the type it's being cast to. This ensures valid method invocation on the downcast object like c.honk() and maintains program stability .

The 'protected' access modifier allows the brand variable in the Vehicle class to be accessed not only within the same package but also by subclasses in different packages, promoting encapsulation while still allowing for inheritance flexibility . This choice supports a balance between data hiding and necessary access, allowing subclasses like Car and Truck to directly reference and use the brand variable when overriding methods or adding new features without exposing it to all classes that are not derived from Vehicle .

The 'final' keyword is used in the Vehicle class to prevent the method fuelType() from being overridden by any subclass. This ensures that the specific behavior defined within fuelType(), stating the usage of petrol or diesel, remains consistent across all instances of Vehicle and its subclasses . If you attempt to override the fuelType() method in a subclass, the Java compiler will throw a compilation error, which protects the design decision of making this behavior unmodifiable .

Method overloading involves having multiple methods with the same name but different parameters, either in the number of parameters or their types, within the same class. This is demonstrated in the Vehicle class where the service method is overloaded with two versions: service() and service(String serviceType). Method overriding, on the other hand, involves redefining a method in a subclass that already exists in the superclass with the same signature. This is seen in the Car class where the displayInfo method is overridden to provide additional details specific to Car .

Creating a method in Main like `public void showVehicleInfo(Vehicle v) { v.displayInfo(); }` demonstrates polymorphism because it allows any subclass of Vehicle to be passed to the method, enabling dynamic method resolution. When a Car or Truck object is passed, the overridden displayInfo() method specific to the object's actual type (Car or Truck) is invoked, not just the Vehicle's version . This showcases polymorphism by allowing objects of different types to be handled through a uniform interface, without the need for type-specific methods each time .

To include a new protected variable driverName in the Truck class, declare it as `protected String driverName;`. Add a method `public void displayDriverName() { System.out.println("Driver Name: " + driverName); }` . Making driverName protected allows subclasses within the same package or any subclass outside the package to access and modify it directly, which is crucial for encapsulation as it allows necessary access control while keeping the variable hidden from non-related classes. This ensures that only related class contexts have direct access to potentially sensitive data like driver name .

Upcasting in the Main class is demonstrated by assigning instances of Car and Truck to a Vehicle reference type like Vehicle v1 = new Car("Toyota", "Corolla") and Vehicle v2 = new Truck("Volvo", 10). This allows the program to use a single reference type, Vehicle, to access overridden methods such as displayInfo(). This polymorphic behavior enables a single method signature to call different method implementations based on the object type being referenced at runtime, highlighting the flexibility and extensibility of the polymorphic design .

The benefits of preventing method overriding using the 'final' keyword include ensuring consistent behavior across all subclasses and protecting original class design intentions, which can simplify maintenance and debugging by reducing unexpected changes in method behavior . However, this approach limits flexibility, making it challenging to alter or enhance functionality in subclass contexts. It can lead to unnecessarily rigid class designs, discouraging reuse and extension in dynamic, evolving systems, potentially requiring workarounds or entirely new class structures for desired customizations .

When adding a new subclass Motorbike that overrides the displayInfo method, consider what additional information the Motorbike class should convey compared to the base Vehicle class, such as specific features or specifications unique to a motorbike. It's also important to maintain consistency in method signatures to ensure polymorphism works across various Vehicle subclasses. Additionally, consider subclass-specific attributes that might enhance the information display, while ensuring the overriding method adheres to the base class's contract of method behavior . This design supports coherent class hierarchies and informativ... (text clipped)

Adding polymorphic features, such as method overriding and using superclass references for subclass objects in a Vehicle Management System, enhances scalability by allowing seamless integration of new vehicle types without modifying existing code structures. Polymorphism enables the system to process objects differently depending on their actual types while maintaining an interface based on the superclass, promoting code reuse and further extensions. It aids software maintenance by reducing redundancy, simplifying control flow, and leveraging inheritance hierarchies to ensure any changes in base functionali... (text clipped)

You might also like