Home           J2EE Concepts           JMS           Java Language           Contact           Back           Next           Bookmark this Page          










Some useful tips related to:
 - health & fitness
 - vehicle care
 - cooking
 - misc household tasks

Interfaces Overview


In simple words: An interface is like a class that declares methods but does not implement them (in fact, an interface must not implement any method).

One might ask that what is the purpose of having such thing, where you can not provide any implementation. An interface basically defines a contract which must be followed by any class that decides to implement this interface; i.e. any class that implements an interface, must implement all the methods declared in the interface.

This offers few advantages in terms of design principles:
- If large no. of people are working on different modules, then having an interface at top level would ensure that all of them implement all desired functionalities.
- More on above point: when a method is to be called, both the calling & called classes need to be present at compile time. One option to resolve this is to put all implementation in the top level class (which is not something that would be recommended); other option is to use interface.
- If one needs to expose the functionality offered by his/her application, he/she may do so by exposing interfaces. This would ensure that the implementation is hidden from other people (more of a security thing).
- Using interfaces offers great amount of flexibility when it comes to runtime polymorphism ( more on this in next article).

Some more points worth noting:
- One can not instantiate an interface; i.e. you can not create an object of an interface by using new operator.
- An interface may extend other interfaces (i.e. even more than one interface).
- An interface can also be used to declare some constants.
- This means that all constant values defined in an interface are implicitly public, static & final.
- This also means that constant values defined in an interface can not be transient, volatile or synchronized.
- One cannot use private or protected access modifiers.