| Author |
About Interface
|
kayanaat sidiqui
Ranch Hand
Joined: Sep 04, 2008
Posts: 122
|
|
Hi there, Well i just want to ask that what is the purpose of "marker interface"? And if it does'nt have body then how it applies default behaviour? please clear my doubt. Thanks.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
A marker interface usually still has some contract an object must follow, even if there are no methods for enforcing that contract. By implementing the interface you are informing anyone who cares that 'Yes, I know the contract that this Marker represents and this code follows that Contract.' My favorite example from the API is Serializable. When you look at the API you will see there is a pretty hefty contract that goes along with that interface, all designed to make sure that the instance of any class that implements the Serializable interface can be stored externally to the JVM (to a file, DB, or sent over a socket for examples). There are no methods that the Serializable interface requires, all the work needs to be done from ObjectOutputStreams, and ObjectInputStreams. But my labeling your class as Serializable you tell anyone who reads your code - and the ObjectOuputStream class, that your code can, indeed have its code stored externally following the rules that Serializable sets forth.
|
Steve
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
And there's a bit more in one of these FAQ.
|
 |
Bill Shirley
Ranch Hand
Joined: Nov 08, 2007
Posts: 457
|
|
|
And note that marker interfaces were a design solution before @annotations were added to the language. You would usually want to implement this behavior with annotations, but there's no pressing reason to change pre-existing marker interfaces into annotations.
|
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
|
 |
kayanaat sidiqui
Ranch Hand
Joined: Sep 04, 2008
Posts: 122
|
|
Thank you Steve for explaining the marker interface. Thank you Campbell for giving me examples of marker interface with great deatials. And Thank you Bill for providing me design level solution.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
You're welcome
|
 |
 |
|
|
subject: About Interface
|
|
|