wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes what is the use of interface which has no methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the use of interface which has no methods?" Watch "what is the use of interface which has no methods?" New topic
Author

what is the use of interface which has no methods?

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
what is the use?
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Search a little on Marker Interface and things will get clearer.
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());


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

Joined: Jul 07, 2007
Posts: 6
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
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Why can't we use annotations for the same purpose?


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
This question comes up so frequently that it's discussed in the FAQ: JavaIntermediateFaq.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the use of interface which has no methods?
 
Similar Threads
What does it mean
Reflection
about syncronization
about servers
what is wsdl ?