Interface is a collection of abstract methods.A class implements an interface, thereby inheriting the abstract method of the interface.
Declaring Interafces:
interface keyword is used to declare an interface.simple example:
import java.lang.*; public interface NameOfInherts{ //ANy number of final,static fields //Any number of abstract method declaration }
Implementing/Declaring Interfaces:
– An interface is implicitly abstract.You do not need to use the abstract keyword while declaring an interface.
– Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
– Methods in an interface are implicitly public.
example:
Animal.java
/* File Name: Animal.java */ interface Animal{ public void eat(); public void travel(); }
Implementing Interfaces:
When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific of the behaviors of the iterafce, the class must declare itself as abstract.
A class uses the implements keyword to implement an interface.Thje implements keyword appears in the class declaration following the extends portion of the declaration.
example:
MammalInt.java
public class MammalInt implements Animalz { public void eat(){ System.out.println("Mammal eats"); } public void travel() { System.out.println("Mammal travels"); } public int noOfLegs() { return 0; } public static void main(String[] args) { MammalInt m=new MammalInt(); m.eat(); m.travel(); } }
Output:
Mammal eats Mammal travels
When overriding methods defined in interfaces there are several rules to be followed:
– Checked exceptions should not be declared on implementation methdos other than the ones declared by the interface method or subclasses of those declared by the interface methods.
– The signature of the interface method and the same retrun type or subtype should be maintained when overriding teh methods.
– An implementation class itself can be abstract and if so interface methods need not to implemented.
When implementation interfaces there are several rules:
-A class can implement ore than one interface at a time.
– A class can extend only one class,but implement many interfaces.
-An interface can extend another interface,similarly to the way that a class can extend another class.
Extending Interfaces:
An interface can extend another interface, similarly to the way that a class can extend another class.The extends keyword is used to extend an intrerface, and the child interface inherits the methods of the the parent interface.
The following sports interface is extended by Hockey and Footbal interfaces.
Filename: Sports.java
//Filename: Sports.java public interface Sports { public void setHomeTeam(String name); public void setVisitingTeam(String name); }
//Filename: Football.java public interface Football extends Sports{ public void homeTeamSored(int points); public void visitingScored(int points); public void endOfQuarter(int quarter); }
//Filename: Hockey.java public interface Hocky extends Sports { public void homeGoalScored(); public void visitingGoalScored(); public void endOfPeriod(int period); public void overtimePeriod(int ot); }
The hockey interface has four methods, but it inherits two from Spors;
thus a class that implements Hockey need to implement all six methods.Similarly, a class that implements Football need to define the three methods from Football and the two methods from Sports.
Extending Multiple Interfaces:
An interface is not class so Interface can extend more than one parent interface.
example:
public interface Hockey extends Sports,Event
Tagging Interfaces:
the MouseListener interface in the java.awt.event package extended java.util.EventListener, which is:
package java.util.*; public interface EventListener{ }
An interface with no methods in it is referred to as a tagging interface.There are two basic design purposes of tagging interfaces
Creates a common parent:
Adds a data type to a class:
Reference:
http://www.tutorialspoint.com/java/java_interfaces.htm