In this article Interface in Java we give the information about Like Class, there are variables and methods inside Interface also, but there are only abstract methods inside it, that is, there is no implementation body { } of the method.

Interface in Java:

Interface is a way to achieve abstraction in Java. It looks exactly like Class but the concepts of both are completely different.

Like Class, there are variables and methods inside Interface also, but there are only abstract methods inside it, that is, there is no implementation body { } of the method.

In other words, “Interface in Java is a reference type and it has abstract methods and static constants.” In Java it is used to achieve abstraction and inheritance.

Definition of Interface:

“An Interface are similar to abstract class but having all the methods of abstract type.”

  • Interface has only abstract methods but it does not have method body.
  • It represents IS-A relationship.
  • It cannot be instantiated like an abstract class. That means we cannot create an instance of the interface.
  • Since Java 8, interfaces can also have default and static methods.
  • Since Java 9, it can also have private methods.
  • All methods in interface are abstract and public by default and all variables are public, static and final by default.
  • That is to say, even if we write only “void methodShow()” the compiler will consider it as “public abstract void methodShow()”.
  • In the same way, the compiler will understand “int q” as “public static final int q”.

Advantages of Interface:

  • It is used to achieve complete abstraction.
  • Through this we can achieve multiple inheritance.
  • It is also used to achieve loose coupling.

Declaration of Interface:

An interface is declared using the interface keyword. It provides complete abstraction. This means that all methods in the interface are declared with an empty body.

syntax:-

interface interface_name

{

        methods / functions    // abstract methods only and all public

         variables / fields      //  it should always public static final

         // 8th version – default concrete method() and public static methods

       // 9th version –  private methods

}

Interface Important points –

  1. We cannot create an instance of an interface but we can create its reference which refers to the object of the implemented class.
  2. A Java class can implement many interfaces.
  3. An interface can extend more than one interface.
  4. A class that implements an interface is required to implement all the methods declared in the interface.
  5. In this all the methods are abstract and public and all the fields are public, static and final.
  6. One interface cannot implement another interface.
  7. The interface which is declared in another interface is called nested interface.
  8. At the time of declaration, it is necessary to initialize the variable otherwise the compiler will throw error.
  9. A class cannot implement two interfaces whose methods have the same name but different return types.

Interface implementation –

Implements keyword is used to implement interface in Java.

Example of this: In the example given below, we have created an interface named Animal and implemented it through Dog class.

interface Animal

{

public void test();

}

class Dog implements Animal

{

public void test()

{

System.out.println(“Interface Method Implemented”);

}

public static void main(String args[])

{

Animal a = new Dog();

a.test();

}

}

Output: – Interface Method Implemented

Multiple inheritances in Interface –

In Java, a class does not support multiple inheritances but a class can implement many interfaces.

In the example given below, two interfaces are implemented by one class.

interface Eatable

{

void eat();

}

interface Drinkable

{

void drink();

}

class Food implements Eatable,Drinkable

{

public void eat()

{

System.out.println(“this is eatable”);

}

public void drink()

{

System.out.println(“this is drinkable”);

}

public static void main(String args[])

{

Food obj = new Food();

obj.eat();

obj.drink();

}

}

Output:-
this is eatable
this is drinkable.

Extending Interface –

In Java, one interface extends another interface. For this extends keyword is used.

interface NewsPaper

{

news();

}

interface Magazine extends NewsPaper

{

colorful();

}

class Dog implements Animal

{

public void breath()

{

System.out.println(“Breathing as Dog…”);

}

}

class Fish implements Animal

{

public void breath()

{

System.out.println(“Breathing as Fish…”);

}

}

class InterfaceDemo

{

public static void main(String args[])

{

Animal a;

a = new Dog();

a.breath();

a = new Fish();

a.breath();

}

}

OutPut:

Breathing as Dog

Breathing as Fish

Explanation:

In the above example, we have created an interface named Animal.

An abstract method breath() has been declared in it.

After that, we have created two new classes Dog and Fish, which implement the interface Animal.

Notice, here both classes Dog and Fish, after implementing the interface Animal, are overriding its abstract method breath() and giving it implementation body {}.

In Class Interface demo we have created a reference variable a of interface Animal. The first time we assign the Dog object to the reference variable a, then a. On calling breath(), the overridden method of Dog class was called.

After that, when we assigned the object of class Fish to the reference variable a, then on calling a.breath(), the overridden method of Fish class was called.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *