Forums Register Login

what is the use of interface which has no methods?

+Pie Number of slices to send: Send
what is the use?
+Pie Number of slices to send: Send
Search a little on Marker Interface and things will get clearer.
+Pie Number of slices to send: Send
SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());


why is the castin g done?
because 'o' is a SomeObject reference?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Why can't we use annotations for the same purpose?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
This question comes up so frequently that it's discussed in the FAQ: JavaIntermediateFaq.
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2990 times.
Similar Threads
What does it mean
about syncronization
what is wsdl ?
about servers
Reflection
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:46:24.