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

Public Abstract Class Vehicle Assignment

Uploaded by

moiz88053
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)
34 views3 pages

Public Abstract Class Vehicle Assignment

Uploaded by

moiz88053
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
You are on page 1/ 3

public abstract class Vehicle {

protected String name;


protected int speed;
protected int capacity;

public Vehicle(String name, int speed, int capacity) {


this.name = name;
this.speed = speed;
this.capacity = capacity;
}

public void displayDetails() {


System.out.println("Name: " + name);
System.out.println("Speed: " + speed + " km/h");
System.out.println("Capacity: " + capacity + " persons");
}

public abstract void start();


public abstract void stop();
}
ElectricVehicle.java (Interface)
public interface ElectricVehicle {
void chargeBattery();
int getBatteryLevel();
}
Car.java (Concrete Class implementing Vehicle and ElectricVehicle)
public class Car extends Vehicle implements ElectricVehicle {
private int batteryLevel;

public Car(String name, int speed, int capacity) {


super(name, speed, capacity);
this.batteryLevel = 0; // Initialize battery level to 0
}

@Override
public void start() {
System.out.println("Car started. Electric motor on.");
}

@Override
public void stop() {
System.out.println("Car stopped. Electric motor off.");
}

@Override
public void chargeBattery() {
batteryLevel = 100; // Simulate full charge
System.out.println("Car battery fully charged.");
}

@Override
public int getBatteryLevel() {
return batteryLevel;
}
}
Bike.java (Concrete Class extending Vehicle)
public class Bike extends Vehicle {
public Bike(String name, int speed, int capacity) {
super(name, speed, capacity);
}

@Override
public void start() {
System.out.println("Bike started. Engine revved up.");
}

@Override
public void stop() {
System.out.println("Bike stopped. Engine off.");
}
}
Main.java
public class Main {
public static void main(String[] args) {
// Instantiate objects
Car myCar = new Car("Tesla Model S", 250, 5);
Bike myBike = new Bike("Honda CBR", 180, 2);

// Demonstrate Car functionality


System.out.println("--- Car Demo ---");
myCar.displayDetails();
myCar.start();
myCar.chargeBattery();
System.out.println("Battery Level: " + myCar.getBatteryLevel() + "%");
myCar.stop();

// Demonstrate Bike functionality


System.out.println("\n--- Bike Demo ---");
myBike.displayDetails();
myBike.start();
myBike.stop();
}
}
Output
--- Car Demo ---
Name: Tesla Model S
Speed: 250 km/h
Capacity: 5 persons
Car started. Electric motor on.
Car battery fully charged.
Battery Level: 100%
Car stopped. Electric motor off.

--- Bike Demo ---


Name: Honda CBR
Speed: 180 km/h
Capacity: 2 persons
Bike started. Engine revved up.
Bike stopped. Engine off.
Analysis Questions and Answers
1. Why can't we instantiate an abstract class or interface directly?
 Abstract Class: Because an abstract class is incomplete (has at least one abstract
method without an implementation), it cannot be instantiated on its own. You must
create a concrete subclass that implements all abstract methods.
 Interface: An interface is purely abstract (all methods are abstract without
implementations). It serves as a contract specifying methods that must be
implemented by any class implementing it. Thus, it cannot be instantiated directly.
2. What is the advantage of using an interface when Vehicle already has abstract
methods?
 Multiple Inheritance: Interfaces allow for multiple inheritance of behavior (not state),
which is not possible with abstract classes alone. This enables a class to implement
multiple interfaces, providing more flexibility in design.
 Contract Specification: Interfaces clearly specify a contract that must be honored by
implementors, which is particularly useful in scenarios where diverse classes need to
guarantee certain functionalities.
 Decoupling: Interfaces help in decoupling the dependent components, making the
system more modular and easier to maintain.
3. How would you modify this program if you need to add a new type of vehicle, such
as a Truck?
 Step 1: Decide if the Truck shares the same base characteristics as Vehicle. If yes,
extend the `

You might also like