0% found this document useful (0 votes)
39 views5 pages

Java Inheritance Lab-1

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)
39 views5 pages

Java Inheritance Lab-1

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/ 5

JAVA INHERITANCE LAB

QUESTION: Abstract Class Vehicle:

 Attributes: String brand, String model, int year.

 Constructor to initialize these attributes.

 Abstract method: abstract void speed()

 Concrete method: void displayInfo() that prints the vehicle's brand, model, and year.

Interface Engine:

 Method signatures: void start(), void stop().

Concrete Class Car:

 Extends Vehicle and implements Engine.

 Override Speed () to print "The car is speed

 Implement start() and stop() to print "The car's engine has started." and "The car's
engine has stopped." respectively.

Final Class ElectricCar:

 Extends Vehicle and implements Engine.

 Override speed () to print "The electric car is speed "

 Implement start() and stop() to print "The electric motor is humming." and "The
electric motor is off." respectively.

 Add a unique attribute double batteryLevel.

Main Class (Main):

 Create objects of Car and ElectricCar.

 Call displayInfo(), speed (), start(), and stop() on these objects to demonstrate
polymorphism and inheritance.
SOLUTION: package INHERITANCE;

import java.util.Scanner;

abstract class Vehicle {

String brand;

String model;

int year;

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

this.brand = brand;

this.model = model;

this.year = year;

abstract void speed();

void displayInfo() {

System.out.println("Brand: " + brand);

System.out.println("Model: " + model);

System.out.println("Year: " + year);

interface Engine {

void start();

void stop();

class Car extends Vehicle implements Engine {

public Car(String brand, String model, int year) {

super(brand, model, year);//super is used to recall the constructor from the parent class

}
void speed() {

System.out.println("The car is speed");

public void start() {

System.out.println("The car's engine has started.");

public void stop() {

System.out.println("The car's engine has stopped.");

final class ElectricCar extends Vehicle implements Engine {

double batteryLevel;

public ElectricCar(String brand, String model, int year, double batteryLevel) {

super(brand, model, year); //super is used to recall the constructor from the parent
class

this.batteryLevel = batteryLevel;

void speed() {

System.out.println("The electric car is speed");

public void start() {

System.out.println("The electric motor is humming.");

public void stop() {

System.out.println("The electric motor is off.");

void displayBattery() {
System.out.println("Battery Level: " + batteryLevel + "%");

public class QUES{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

Car car = new Car("Hyundai", "Creta", 2022);

System.out.println("Car Details");

car.displayInfo();

car.speed();

car.start();

car.stop();

ElectricCar eCar = new ElectricCar("Tata", "Harrier", 2025, 80.0);

System.out.println("Electric Car Details");

eCar.displayInfo();

eCar.displayBattery();

eCar.speed();

eCar.start();

eCar.stop();

sc.close();

OUTPUT: Car Details

Brand: Hyundai

Model: Creta

Year: 2022
The car is speed

The car's engine has started.

The car's engine has stopped.

Electric Car Details

Brand: Tata

Model: Harrier

Year: 2025

Battery Level: 80.0%

The electric car is speed

The electric motor is humming.

The electric motor is off.

You might also like