• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is interface ?  RSS feed

 
purushotham singam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,
can anyone explain me what is interface in real time scnarios and like which sitvations we can use the projects in interface and give me one example.
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Interface is like a contract, it has variables(public static final) and methods(public abstract), the class which implements this interface has to guarantee that all the methods of this interface are defined.
for eg:-

Interfaces are more of a design concern and also to support kind of multiple inheritence.
When we know for a particular action "What to be done but not How to be done" then we choose to keep that action in an Interface and it is the responsibility of the implementing Class(es) to provide the action with respect to their own requirements.
Hope this explanation is of some help
 
rahul mehra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...
well lets get started with the meaning of the word interface...
interface as the word explains is the way of connection or communication between two systems... like if you want to switch on the t.v then you press the on button on your remote...so the remote is the interface between you and the tv....


now lets come to java...

Methods form the object's interface with the outside world. if a object has to accesss and variable(private or protected) of its class... it has to use the method of that class...

In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:

interface Bicycle {

void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);
}

To implement this interface, the name of your class would change (to ACMEBicycle, for example), and you'd use the implements keyword in the class declaration:
class ACMEBicycle implements Bicycle {

// remainder of this class implemented as before

}

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

hope you got a general idea of interface...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!