Java Interface

Java Interface

Interface is a template or blue print of the class. This defines some set of instructions that classes which uses the interface should be implemented.

The interface is used to achieve a complete 100% abstraction. Interfaces contain abstract methods, variables are in the interface are static, public & final by default.

Interface is also used to achieve multiple inheritance in java.

Interface is used to achieve complete 100% abstraction. so there will be no method body for the abstract methods, to access the abstract method ,we should create a class for the interface.

info

All methods inside the interface are implicitly public and all fields are implicitly public static final

Syntax

interface interface_name{
//constant fields
//abstract methods
//methods
}

relationship between classes and interfaces

Class can extend to another class

to use an interface, class implements an interface, because we cannot create objects of interfaces

interfaces can extend to another interface

Syntax for class implements an interface

class ClassName implements InterfaceName{
//methods
//data members
}

To use interface, we should create a class using implements keyword to use interface with the class

Example for Using Interface

interface animal{
void sound();
}
class dog implements animal{
public void sound(){System.out.println("Brak");}
public static void main(String args[]){
dog obj = new dog();
obj.sound();
}
}

Output

Bark

In the above example interface is an animal, so every class can be implemented animal interface, the animal interface has a sound( ) method, so in the dog class, body of the sound( ) method can be declared. we are creating an object to the dog class, now we can call the sound( ) method

extends Keyword in Interface

As we discussed above, one interface can extend to a new interface, just like a class can extend to the new class. The extends keyword is used for extending interfaces

Syntax

interface A{
//members of Line interface
//abstract methods
}
interface B extends A {
//members of interface A and Interface B
//abstract methods
}

Example for extending interface

interface A {
void methodA();
}
interface B extends A {
void methodB();
}
// Class will implement methods from both interfaces
// As interface B is also extending interface A
class demo implements B {
public void methodA() {
System.out.println("Method A is called");
}
public void methodB() {
System.out.println("Method B is called");
}
public static void main(String[] args) {
demo obj = new demo();
obj.methodA();
obj.methodB();
}
}

Output

Method A is called
Method B is called

In the above program interface, A is used to extend Interface B, each interface has its own method. we created a class called demo using implements keyword and boy of the two methods methodA( ) and methodB( ) is created in the implemented class. Object is created and two methods are called

How Multiple inheritances in Java is possible by an interface

By using class multiple inheritance is not possible because of ambiguity, but in the interfaces ,methods are abstract,so you can declare the method with the same name ,but can’t write the method body inside the interface, check out the example below

Example Multiple inheritances in Java using interface

interface A {
void show();
}
interface B extends A {
void show();
}
// Class will implement methods from both interfaces
// As interface B is also extending interface A
class demo implements A,B {
public void show() {
System.out.println("MULTIPLE INHERITANCE IS POSSIBLE");
}
public static void main(String[] args) {
demo obj = new demo();
obj.show();
}
}

Output

MULTIPLE INHERITANCE IS POSSIBLE
Last updated on by vishal devxo