0% found this document useful (0 votes)
40 views4 pages

Experiment No:-04: Title: Problem Statement

1. The document defines an interface called Vehicle with abstract methods for changing gears, speeding up, and applying brakes. 2. Classes Bicycle and Bike implement the Vehicle interface and override its abstract methods to change their speed and gear based on calls to those methods. 3. The main method creates instances of Bicycle and Bike, calls their methods to change speed and gears, and prints out their states.

Uploaded by

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

Experiment No:-04: Title: Problem Statement

1. The document defines an interface called Vehicle with abstract methods for changing gears, speeding up, and applying brakes. 2. Classes Bicycle and Bike implement the Vehicle interface and override its abstract methods to change their speed and gear based on calls to those methods. 3. The main method creates instances of Bicycle and Bike, calls their methods to change speed and gears, and prints out their states.

Uploaded by

305CHAVANMRUNAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment no :- 04

Title: To study Inheritance, Interfaces and implement the program.


Problem Statement: Create an interface and classes like bicycle, bike,etc,
having common functionalities and put all the common functionalities in the
interface classes like bicycle, car etc implement all these functionalities in their
own way.
Theory:

An interface in Java is a blueprint of a class. It has static constants and abstract


methods.The interface in Java is a mechanism to achieve abstraction. There can be
only abstract methods in the Java interface, not method body. It is used to achieve
abstraction and multiple inheritance in Java. In other words, you can say that
interfaces can have abstract methods and variables. It cannot have a method body.

Syntax:

interface <interface_name>
{
// declare constant fields
// declare methods that abstract
//by default.
}
Algorithm:
1. Start the program
2. Create an interface Vehicle
3. Declare all the abstract methods
4. Create a class bicycle that implements interface vehicle
5. Implement the capablities of the interface
6. Create a class bike that implements interface vehicle
7. Implement the capablities of the interface
8. Call the main method
9. Create an instance of bicyle doing some operations
10.Create an instance of bike
11.Stop the program
Program:
import java.io.*;
import java.util.*;
interface Vehicle {
void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}
class Bicycle implements Vehicle{
int speed;
int gear;
@Override
public void changeGear(int newGear){
gear = newGear;
}
@Override
public void speedUp(int increment){
speed = speed + increment;
}
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed
+ " gear: " + gear);
}}
class Bike implements Vehicle {
int speed;
int gear;
@Override
public void changeGear(int newGear){
gear = newGear;
}
@Override
public void speedUp(int increment){
speed = speed + increment;
}
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed
+ " gear: " + gear);
}}
class Exp_4 {
public static void main (String[] args) {
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
System.out.println("Bicycle present state :");
bicycle.printStates();
Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);
System.out.println("Bike present state :");
bike.printStates();
}}

You might also like