File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Interfaces with no body Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interfaces with no body" Watch "Interfaces with no body" New topic
Author

Interfaces with no body

zaeem masood ashar
Ranch Hand

Joined: Mar 17, 2001
Posts: 54

Why do we have interfaces such as "Serializable" which do not define any method?

------------------
SCJP2


SCJP2
Prithvi Raj
Greenhorn

Joined: Jul 13, 2001
Posts: 9
interfaces like "Serializable" , "cloneable" are empty interfaces that do not contain any methods, but are used for tagging other classes with properties indicated by the interface type.


prithvi
zaeem masood ashar
Ranch Hand

Joined: Mar 17, 2001
Posts: 54

What are those properties (that tag-along)and where are they mentioned....i mean physically.....(knowing what a Serializable interface connotates)?
thanks
Scott Palmer
Ranch Hand

Joined: Jul 11, 2001
Posts: 31
The fact that the interface is implemented IS the tag.
It is just liek some sort of boolean property on the class that doesn't really take up data in the class. At least that's how I understand it.
E.g. For Serializable, if the serialization APIs can cast the Object to Serializable the they know it is ok to serialize the class. Because the APIs get a generic Object there is no data member they can check or method they can call unless EVERY class implemented that member/method. So interfaces are a usefull may of tagging.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It allows you to test a class easily for that property.
Using SingleThreadModel from servlets as an example, if a Servlet implements this Interface then only a single thread can access a Servlet at a time.
Now you can write some code like this:

So this is a pretty simple example, but it means that the class you wrote has an extra (boolean) property associated with it that can be tested without any properties or additional code or difficulty. Quick and easy flag to say 'this class is different'
Dave.
zaeem masood ashar
Ranch Hand

Joined: Mar 17, 2001
Posts: 54
thanks a bunch fellows !!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interfaces with no body