Tuesday, 26 January 2016

Interface in java

An interface is a reference type in Java, it is similar to class, it is a collection of abstract methods. Its a java mechanism to achieve full abstraction. In other way interface is the mechanism to achieve fully abstraction and multiple inheritance in java. An interface can contain any number of methods

The interface keyword is used to declare an interface. Here is a simple example to declare an interface

Declaring Interfaces:
import java.lang.*;

interface syntax:
public interface NameOfInterface
//Any number of final, static fields
//Any number of abstract method declarations\

interface example:
interface Car{
public void drive();
public void travel();

Relationship between classes and interfaces
A class extends another class, an interface extends another interface but a class implements an interface.

Example of interface
interface sports{
void score();
class Sample implements sports{
public void score(){System.out.println("Score here");}
public static void main(String args[]){
Sample obj = new Sample ();

Score here

And through interfaces we can achieve multiple inheritance in java.

No comments :

Post a Comment