This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Marker interfaces are sometimes used to indicate that an object is a particular kind of thing, or suitable for a particular application. As they do not have any methods, they can only really be used in things like "instanceof" expressions.
They are a messy and unappealing idea, most of the time, as Tony M has indicated in his inimitable style. Even if one doesn't go along with his total ban on the things, they're something to be used at most very sparingly. In fact, anything with "instanceof" in it is best avoided where possible.
Java itself has some marker interfaces, some of which you are pretty much forced to use, even if you hate them. Serializable is one that you'd find it seriously hard to avoid. Cloneable is another built-in marker interface, but in my view the whole cloning feature of the language is so badly designed as to make it worth ignoring 99% of the time. [ August 16, 2006: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
I agree, but I think that Serializable is worth using. It is useful and does have a narrow contract. Like everything else, when it makes sense it is powerful, if you use it on anything and everything it can cause trouble.
Imagine if BufferedReader or SeverSocket were serializable. The fact that ArrayList is gives a solid and simple solution if you ever need to save its state.
It can aid in encapsulation, if you have objects of a serializable class and you need to save its state for later, you can save them to file with little fuss, and no need for accessors which some frown on. Otherwise you will have to go through convoluted steps to get all the pertitant member values and then write it to a file, then do the reverse when you need to recreate the object. Messy and error prone.
The others I wouldn't waste time on.
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Since Java 5, you should use annotations instead of marker interfaces.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rusty Shackleford
Ranch Hand
Joined: Jan 03, 2006
Posts: 490
posted
0
Thanks for that tidbit Ilja. I have been slowly poking into the new Java 5 stuff, I will look into it if/when I get a good grasp of generics.
Originally posted by Ilja Preuss: Since Java 5, you should use annotations instead of marker interfaces.
That depends on the philosophy you follow
If a marker interface just tells you something about the class, then yes. If the marker interface tells you the class is something special, then a marker interface is a valid OO construct even if it doesn't add functionality.
42
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Jeroen T Wenting:
That depends on the philosophy you follow
If a marker interface just tells you something about the class, then yes. If the marker interface tells you the class is something special, then a marker interface is a valid OO construct even if it doesn't add functionality.
I'm not sure I understand the difference. Can you give an example for the latter? Thanks!
Jeroen T Wenting
Ranch Hand
Joined: Apr 21, 2006
Posts: 1847
posted
0
Serializable doesn't do much, except prevent an exception
Another marker interface might tell you that a class has a special meaning though, rather than guaranteeing it's simply well behaved. Say you have a marker interface Trainee. An employee who is a trainee has no need for special methods, but other parts of the system may treat them differently (like no profit share at the end of the FY, or no invitation to the company Christmas party). To me at least that's a fundamental difference, that traineeship is something that's not just tagged on like an annotation, it's a fundamental part of what that particular person is.
Maybe not the strongest example, but you get the point.
Of course I don't like annotations in general, so will try to avoid them most of the time
There are some marker interfaces in the library. Implement them when you want to indicate something to other library classes that look for them.
Opinion: They were not a very good idea in the first place, maybe a practical workaround, maybe downright evil. Don't create any more of them in your own code.
Opinion: Annotations are a better way to accomplish the same thing.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.