0% found this document useful (0 votes)
4 views

Interfaces in Java

The document explains Java interfaces, which define what methods a class must implement without specifying how. It highlights the benefits of interfaces, such as achieving total abstraction and enabling multiple inheritance. Examples of interfaces and their implementations in classes like Bicycle and Bike are provided to illustrate their usage in real-world scenarios.

Uploaded by

SHAHIDHA
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)
4 views

Interfaces in Java

The document explains Java interfaces, which define what methods a class must implement without specifying how. It highlights the benefits of interfaces, such as achieving total abstraction and enabling multiple inheritance. Examples of interfaces and their implementations in classes like Bicycle and Bike are provided to illustrate their usage in real-world scenarios.

Uploaded by

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

Interfaces in Java

 Interfaces specify what a class must do and not how. It is the blueprint of the
class.
 An Interface is about capabilities like a Player may be an interface and any
class implementing Player must be able to (or must implement) move(). So it
specifies a set of methods that the class has to implement.
 If a class implements an interface and does not provide method bodies for all
functions specified in the interface, then the class must be declared abstract.

Syntax :
interface <interface_name> {

// declare constant fields


// declare methods that abstract
// by default.
}

Why do we use interface ?


 It is used to achieve total abstraction.
 Since java does not support multiple inheritance in case of class, but by
using interface it can achieve multiple inheritance .
 It is also used to achieve loose coupling.

interface Player
{
final int id = 10;
int move();
}

To implement an interface we use keyword: implements

// Java program to demonstrate working of


// interface.
import java.io.*;

// A simple interface
interface In1
{
// public, static and final
final int a = 10;

// public and abstract


void display();
}

// A class that implements the interface.


class TestClass implements In1
{
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Geek");
}

// Driver Code
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}

Output:
Geek
10

A real-world example:
Let’s consider the example of vehicles like bicycle, car, bike………, they have common
functionalities. So we make an interface and put all these common functionalities. And lets
Bicycle, Bike, car ….etc implement all these functionalities in their own class in their own way.

import java.io.*;

interface Vehicle {

// all are the abstract methods.


void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}

class Bicycle implements Vehicle{

int speed;
int gear;

// to change gear
@Override
public void changeGear(int newGear){

gear = newGear;
}

// to increase speed
@Override
public void speedUp(int increment){

speed = speed + increment;


}

// to decrease speed
@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;

// to change gear
@Override
public void changeGear(int newGear){

gear = newGear;
}

// to increase speed
@Override
public void speedUp(int increment){

speed = speed + increment;


}

// to decrease speed
@Override
public void applyBrakes(int decrement){

speed = speed - decrement;


}

public void printStates() {


System.out.println("speed: " + speed
+ " gear: " + gear);
}
}
class GFG {

public static void main (String[] args) {

// creating an inatance of Bicycle


// doing some operations
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);

System.out.println("Bicycle present state :");


bicycle.printStates();

// creating instance of the bike.


Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);

System.out.println("Bike present state :");


bike.printStates();
}
}

Output;
Bicycle present state :
speed: 2 gear: 2
Bike present state :
speed: 1 gear: 1

You might also like