Interface in Java

0 Comments

Interface in java is a model/prototype of a class. It has static constants and methods (without implementation).

There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritance in Java. It cannot be instantiated.

Three reasons to use interface. They are given below.

  • Used to achieve abstraction.
  • It support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.

Loosely Coupled and Tightly Coupled difference:

Loose coupling is when a group of classes are independent on one another

Tight coupling is when a group of classes are highly dependent on one another.

Example:

interface greetings{

void print();

}

class Howdy implements greetings

{

public void print()

{

System.out.println("Hello World");

}

}

public static void main(String args[])

{

Howdy obj = new Howdy  ();

obj.print();

}

}

 


Leave a Reply

Your email address will not be published.