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

Interfaces

A Java interface is an abstract type that defines a blueprint for class behavior, containing static constants and abstract methods. It enables abstraction, multiple inheritances, and loose coupling in Java, while differences between classes and interfaces include instantiation, method implementation, and access specifiers. The document provides examples of interfaces and their implementation in classes, demonstrating multiple inheritance through interfaces.

Uploaded by

sufiyapraveen01
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)
3 views6 pages

Interfaces

A Java interface is an abstract type that defines a blueprint for class behavior, containing static constants and abstract methods. It enables abstraction, multiple inheritances, and loose coupling in Java, while differences between classes and interfaces include instantiation, method implementation, and access specifiers. The document provides examples of interfaces and their implementation in classes, demonstrating multiple inheritance through interfaces.

Uploaded by

sufiyapraveen01
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/ 6

Java Interface

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 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.

Difference Between Class and Interface

Class Interface

In an interface, you must initialize


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

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

The access specifiers used with classes In Interface only one specifier is used-
are private, protected, and public. Public.

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() {


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

class Main
{
public static void main (String[] args)
{

// Instance of Bicycle(Object)
Bicycle bicycle = new Bicycle();

bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);

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


bicycle.printStates();

// Instance of Bike (Object)


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

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


bike.printStates();
}
}
Output
Bicycle present state : speed: 2 gear: 2
Bike present state : speed: 1 gear: 1

Multiple Inheritance in Java Using Interface


Multiple Inheritance is an OOPs concept that can’t be implemented in Java using classes.
But we can use multiple inheritances in Java using Interface. Let us check this with an
example.
Example:

import java.io. *;

// Add interface

interface Add {

int add (int a,int b);

// Sub interface

interface Sub {

int sub (int a,int b);

// Calculator class implementing

// Add and Sub

class Cal implements Add, Sub

// Method to add two numbers

public int add (int a,int b){


return a+b;

// Method to sub two numbers

public int sub (int a,int b){

return a-b;

class GFG {

// Main Method

public static void main (String [] args)

// instance of Cal class

Cal x = new Cal ();

System.out.println("Addition: " + x.add(2,1));

System.out.println("Subtraction: " + x.sub(2,1));

Output

Addition: 3

Subtraction: 1

You might also like