What is an Interface?
An interface is just like Java Class, but it only has static constants and abstract
method. Java uses Interface to implement multiple inheritance. A Java class can
implement multiple Java Interfaces. All methods in an interface are implicitly public
and abstract.
To use an interface in your class, append the keyword "implements" after your class
name followed by the interface name.
Example
class Dog implements Pet
To understand the concept of Java Interface better, let see an example. The class
"Media Player" has two subclasses: CD player and DVD player. Each having its
unique implementation method to play music.
Another class "Combo drive" is inheriting both CD and DVD (see image below).
Which play method should it inherit? This may cause serious design issues. And
hence, Java does not allow multiple inheritance.
interface Pet
{
public void test();
}
class Dog implements Pet
{
public void test()
{
System.out.println("Interface Method
Implemented");
}
public static void main(String args[])
{
Pet p = new Dog();
p.test();
}
}
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 .
Difference between Class and Interface
Class Interface
In class, you can instantiate variable and In an interface, you can't instantiate
create an object. variable and create an object.
Class can contain concrete(with The interface cannot contain
implementation) methods concrete(with implementation) methods
The access specifiers used with classes In Interface only one specifier is used-
are private, protected and public. Public.
Must know facts about Interface
A Java class can implement multiple Java Interfaces. It is necessary that the
class must implement all the methods declared in the interfaces.
Class should override all the abstract methods declared in the interface
The interface allows sending a message to an object without concerning
which classes it belongs.
Class needs to provide functionality for the methods declared in the interface.
All methods in an interface are implicitly public and abstract
An interface cannot be instantiated
An interface reference can point to objects of its implementing classes
An interface can extend from one or many interfaces. Class can extend only
one class but implement any number of interfaces
An interface cannot implement another Interface. It has to extend another
interface if needed.
An interface which is declared inside another interface is referred as nested
interface
At the time of declaration, interface variable must be initialized. Otherwise, the
compiler will throw an error.
The class cannot implement two interfaces in java that have methods with
same name but different return type.
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 Bicylce, 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;
public void changeGear(int newGear)
{
gear = newGear;
}
public void speedUp(int increment)
{
speed = speed + increment;
}
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;
public void changeGear(int newGear)
{
gear = newGear;
}
public void speedUp(int increment)
{
speed = speed + increment;
}
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)
{
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
System.out.println("Bicycle present state :");
bicycle.printStates();
// creating instance of bike.
Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);
System.out.println("Bike present state :");
bike.printStates();
}
}