• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces with no body

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

------------------
SCJP2
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
zaeem masood ashar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What are those properties (that tag-along)and where are they mentioned....i mean physically.....(knowing what a Serializable interface connotates)?
thanks
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a bunch fellows !!
 
reply
    Bookmark Topic Watch Topic
  • New Topic