• 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

what is the use of interface which has no methods?

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the use?
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search a little on Marker Interface and things will get clearer.
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());


why is the castin g done?
because 'o' is a SomeObject reference?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasanna,

Bart's example is a good one.

In addition, when designing it can sometimes help to group a number of classes together that may share some common purpose with an interface that has no methods.

You may not actively use the common interface, but from a design aspect it can help to clarify a group of classes and provide less clutter in a UML class diagram for example. Also, you may use the interface later on when the implementing classes become more defined and they do start to exhibit common behaviour that you want to enforce in the interface contract.

A bit of a "wooly" reply, but the "marker" interfaces help me when understanding large class diagrams.

Cheers,

Dave
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't we use annotations for the same purpose?
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());


why is the castin g done?
because 'o' is a SomeObject reference?



This has nothing to do with interfaces as such.
clone() returns something of type Object and you can not assign something of type Object to the ref of type SomeObject, even when you know it is of type SomeObject.
Therefore you have to tell the compiler you know what you are doing by adding the cast to SomeObject.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by D Rog:
Why can't we use annotations for the same purpose?



You can, and should. But Annotations weren't introduced until Java 5. Marker interfaces are the old solution to the problem.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question comes up so frequently that it's discussed in the FAQ: JavaIntermediateFaq.
 
reply
    Bookmark Topic Watch Topic
  • New Topic