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

Java

A Java interface is an abstract type that defines a set of behaviors for classes to implement, facilitating abstraction and multiple inheritances. Interfaces contain public, static, and final variables by default, and all methods are abstract without implementation. The document also highlights the differences between classes and interfaces, emphasizing that interfaces cannot be instantiated and are used to promote code reusability and consistency.

Uploaded by

mdhameed08
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)
2 views5 pages

Java

A Java interface is an abstract type that defines a set of behaviors for classes to implement, facilitating abstraction and multiple inheritances. Interfaces contain public, static, and final variables by default, and all methods are abstract without implementation. The document also highlights the differences between classes and interfaces, emphasizing that interfaces cannot be instantiated and are used to promote code reusability and consistency.

Uploaded by

mdhameed08
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 Interface

Last Updated : 03 Feb, 2025



An Interface in Java programming language is defined as an abstract


type used to specify the behavior of a class. An interface in Java is a
blueprint of a behavior. A Java interface contains static constants and
abstract methods.
 The interface in Java is a mechanism to achieve abstraction.
 By default, variables in an interface are public, static, and final.
 It is used to achieve abstraction and multiple inheritances in Java.
 It is also used to achieve loose coupling.
 In other words, interfaces primarily define methods that other classes
must implement.
 An interface in Java defines a set of behaviors that a class can
implement, usually representing an IS-A relationship, but not always in
every scenario.
Example:
{...}
interface testInterface {

// public, static and final


final int a = 10;

// public and abstract


void display();
}

// Class implementing interface


class TestClass implements testInterface {

// Implementing the capabilities of


// Interface
public void display(){
System.out.println("Geek");
}
}

class Geeks

{...}

Output
Geek
10
Note: In Java, the abstract keyword applies only to classes and
methods, indicating that they cannot be instantiated directly and must be
implemented. When we decide on a type of entity by its behaviour and
not via attribute we should define it as an interface.
Syntax
interface {
// declare constant fields
// declare methods that abstract
// by default.
}
To declare an interface, use the interface keyword. It is used to provide
total abstraction. That means all the methods in an interface are declared
with an empty body and are public and all fields are public, static, and
final by default. A class that implements an interface must implement all
the methods declared in the interface. To implement the interface, use
the implements keyword.
Relationship Between Class and Interface
A class can extend another class, and similarly, an interface can extend
another interface. However, only a class can implement an interface, and
the reverse (an interface implementing a class) is not allowed.
Difference Between Class and Interface
Although Class and Interface seem the same there are certain differences
between Classes and Interface. The major differences between a class
and an interface are mentioned below:
Class Interface

In an interface, you must initialize variables


In class, you can instantiate variables and
as they are final but you can’t create an
create an object.
object.

A class can contain concrete (with The interface cannot contain concrete (with
implementation) methods implementation) methods.

The access specifiers used with classes are In Interface only one specifier is used-
private, protected, and public. Public.
Implementation: To implement an interface, we use the keyword
implements
Let’s consider the example of Vehicles like bicycles, cars, and bikes share
common functionalities, which can be defined in an interface, allowing
each class (e.g., Bicycle, Car, Bike) to implement them in its own way.
This approach ensures code reusability, scalability, and consistency
across different vehicle types.
Example:
import java.io.*;

interface Vehicle {

// Abstract methods defined


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

// Class implementing vehicle interface


class Bicycle implements Vehicle{

int speed;
int gear;

// Change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}

// Increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// Decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}

public void printStates() {


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

// Class implementing vehicle interface


class Bike implements Vehicle {

int speed;
int gear;

// Change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}

// Increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}

// Decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}

public void printStates() {

You might also like