0% found this document useful (0 votes)
24 views6 pages

Assignment 5

The document defines an interface called Vehicle that contains common methods for objects like bicycles, cars, and bikes. It then defines classes for Bicycle, Car and Bike that each implement the Vehicle interface and provide their own implementation for the methods. The main method allows a user to select a vehicle type, creates an instance of the class, and calls the interface methods to demonstrate polymorphism.

Uploaded by

Utkal Pansare
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)
24 views6 pages

Assignment 5

The document defines an interface called Vehicle that contains common methods for objects like bicycles, cars, and bikes. It then defines classes for Bicycle, Car and Bike that each implement the Vehicle interface and provide their own implementation for the methods. The main method allows a user to select a vehicle type, creates an instance of the class, and calls the interface methods to demonstrate polymorphism.

Uploaded by

Utkal Pansare
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/ 6

// Assignment 5: Inheritance

Name: Vaishnavi Appasaheb Nemane


Roll No:2041
Design and develop a context for given case study and implement an
interface for Vehicles
Consider the example of vehicles like bicycle, car, and bike. All Vehicles
have common
functionalities such as Gear Change, Speed up and apply breaks . Make
an interface and put all
these common functionalities. Bicycle, Bike, Car classes should be
implemented for all these
functionalities in their own class in their own way.

import java.util.Scanner;
interface vehicle
{
public void gear_change();
public void speed_up();
public void apply_break();
}

class bicycle implements vehicle


{
bicycle()
{
System.out.println("------------------------------------");
System.out.println("\tBicycle Start : ");
System.out.println("------------------------------------");
}

public void gear_change()


{
System.out.println("\n\tGear Change - Bicycle");
}
public void speed_up()
{
System.out.println("\tSpeed Up - Bicycle");
}

public void apply_break()


{
System.out.println("\tApply Break - Bicycle\n");
}

class car implements vehicle


{
car()
{
System.out.println("------------------------------------");
System.out.println("\tCar Start : ");
System.out.println("------------------------------------");
}

public void gear_change()


{
System.out.println("\n\tGear Change - car");
}

public void speed_up()


{
System.out.println("\tSpeed Up - car");
}

public void apply_break()


{
System.out.println("\tApply Break - car\n");
}

class bike implements vehicle


{
bike()
{
System.out.println("------------------------------------");
System.out.println("\tBike Start : ");
System.out.println("------------------------------------");
}

public void gear_change()


{
System.out.println("\n\tGear Change - bike");
}

public void speed_up()


{
System.out.println("\tSpeed Up - bike");
}

public void apply_break()


{
System.out.println("\tApply Break - bike\n");
}

class interfaces
{
public static void main(String args[])
{
int c;
do
{
System.out.println("Enter option for vehicle : ");
System.out.println("\t1.Bicycle");
System.out.println("\t2.Car");
System.out.println("\t3.Bike");
System.out.println("\tExit");
Scanner sc = new Scanner(System.in);
System.out.print("Enter choice : ");
c = sc.nextInt();

switch(c)
{
case 1 :
bicycle b = new bicycle();
b.gear_change();
b.speed_up();
b.apply_break();

System.out.println("------------------------------------");
break;
case 2 :
car c1 = new car();
c1.gear_change();
c1.speed_up();
c1.apply_break();

System.out.println("------------------------------------");
break;

case 3 :
bike b1 = new bike();
b1.gear_change();
b1.speed_up();
b1.apply_break();

System.out.println("------------------------------------");
break;

case 4 : break;
}
}while(c<4);
}
}
Output

Vostro-260s:~$ javac interfaces.java


Vostro-260s:~$ java interfaces
Enter option for vehicle :
1.Bicycle
2.Car
3.Bike
Exit
Enter choice : 1
------------------------------------
Bicycle Start :
------------------------------------

Gear Change - Bicycle


Speed Up - Bicycle
Apply Break - Bicycle

------------------------------------
Enter option for vehicle :
1.Bicycle
2.Car
3.Bike
Exit
Enter choice:

You might also like