Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Empty interface

Reply
Thread Tools

Empty interface

 
 
-
Guest
Posts: n/a
 
      07-07-2005
If I have an abstract class AbstractSomeClass and in order to avoid a
situation where passing the parameter requires writing
someMethod(AbstractSomeClass asc), I create an empty interface called
SomeClass and then have AbstractSomeClass implements it so that i can do
a someMethod(SomeClass sc)?
 
Reply With Quote
 
 
 
 
Stefan Schulz
Guest
Posts: n/a
 
      07-07-2005
On Thu, 07 Jul 2005 09:38:47 +0800, - wrote:

> If I have an abstract class AbstractSomeClass and in order to avoid a
> situation where passing the parameter requires writing
> someMethod(AbstractSomeClass asc), I create an empty interface called
> SomeClass and then have AbstractSomeClass implements it so that i can do
> a someMethod(SomeClass sc)?


Personally i'd go for the interface declaring the methods used in
AbstractSomeClass

--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"


 
Reply With Quote
 
 
 
 
Kevin McMurtrie
Guest
Posts: n/a
 
      07-07-2005
In article <42cc87a6$(E-Mail Removed)>, - <(E-Mail Removed)> wrote:

> If I have an abstract class AbstractSomeClass and in order to avoid a
> situation where passing the parameter requires writing
> someMethod(AbstractSomeClass asc), I create an empty interface called
> SomeClass and then have AbstractSomeClass implements it so that i can do
> a someMethod(SomeClass sc)?


Sure, that works if all you want to do is type checking on an object
that you never actually look at. I'm not sure how much use it would be.
Sooner or later you'll want to use the object and you'll either need an
interface that defines methods or you'll need a runtime cast to a class.

Define some methods in the interface and have your AbstractSomeClass
implement it. Then you can type check arbitrary classes and use them
without unsafe runtime casting.
 
Reply With Quote
 
Jesper Nordenberg
Guest
Posts: n/a
 
      07-07-2005
- <(E-Mail Removed)> wrote in message news:<42cc87a6$(E-Mail Removed)>...
> If I have an abstract class AbstractSomeClass and in order to avoid a
> situation where passing the parameter requires writing
> someMethod(AbstractSomeClass asc), I create an empty interface called
> SomeClass and then have AbstractSomeClass implements it so that i can do
> a someMethod(SomeClass sc)?


It's a bad idea to create an empty interface because you need to cast
to AbstractSomeClass everywhere you want to call a method on the
object. You should put all the public methods found in
AbstractSomeClass in the interface. This "adapter" pattern is commonly
used in the JRE (see MouseListener -> MouseAdapter).

If you are creating an interface to which you might add new methods in
the future, the best solution is to also create an abstract base class
that implements the interface and note in the interface documentation
that instead of implementing the interface directly, you should sub
class the abstract class. Otherwise, when you add a method to the
interface you will break all classes directly implementing the
interface, but if they inherit from the abstract base class you can
add a default implementation for the new method to avoid breaking sub
classes.

/JN
 
Reply With Quote
 
George Cherry
Guest
Posts: n/a
 
      07-07-2005

"Jesper Nordenberg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
>- <(E-Mail Removed)> wrote in message
>news:<42cc87a6$(E-Mail Removed)>...
>> If I have an abstract class AbstractSomeClass and in order to avoid a
>> situation where passing the parameter requires writing
>> someMethod(AbstractSomeClass asc), I create an empty interface called
>> SomeClass and then have AbstractSomeClass implements it so that i can do
>> a someMethod(SomeClass sc)?

>
> It's a bad idea to create an empty interface


So, would java.io.Serializable--an "empty" interface--be
a bad idea then? It's called a "marker interface". It indicates
the semantics of an implementing class as being serializable.

George W. Cherry

> because you need to cast
> to AbstractSomeClass everywhere you want to call a method on the
> object. You should put all the public methods found in
> AbstractSomeClass in the interface. This "adapter" pattern is commonly
> used in the JRE (see MouseListener -> MouseAdapter).
>
> If you are creating an interface to which you might add new methods in
> the future, the best solution is to also create an abstract base class
> that implements the interface and note in the interface documentation
> that instead of implementing the interface directly, you should sub
> class the abstract class. Otherwise, when you add a method to the
> interface you will break all classes directly implementing the
> interface, but if they inherit from the abstract base class you can
> add a default implementation for the new method to avoid breaking sub
> classes.
>
> /JN



 
Reply With Quote
 
Jesper Nordenberg
Guest
Posts: n/a
 
      07-08-2005
"George Cherry" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "Jesper Nordenberg" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> >- <(E-Mail Removed)> wrote in message
> >news:<42cc87a6$(E-Mail Removed)>...
> >> If I have an abstract class AbstractSomeClass and in order to avoid a
> >> situation where passing the parameter requires writing
> >> someMethod(AbstractSomeClass asc), I create an empty interface called
> >> SomeClass and then have AbstractSomeClass implements it so that i can do
> >> a someMethod(SomeClass sc)?

> >
> > It's a bad idea to create an empty interface

>
> So, would java.io.Serializable--an "empty" interface--be
> a bad idea then? It's called a "marker interface". It indicates
> the semantics of an implementing class as being serializable.


I'm well aware of the use of marker interfaces, but I don't think the
OP was refering to a marker interface, but rather an interface with
public methods. Btw, in Java 5 many uses of marker interfaces can be
replaced with annotations.

/JN
 
Reply With Quote
 
George Cherry
Guest
Posts: n/a
 
      07-08-2005

"Jesper Nordenberg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> "George Cherry" <(E-Mail Removed)> wrote in
> message news:<(E-Mail Removed)>...
>> "Jesper Nordenberg" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) m...
>> >- <(E-Mail Removed)> wrote in message
>> >news:<42cc87a6$(E-Mail Removed)>...
>> >> If I have an abstract class AbstractSomeClass and in order to avoid a
>> >> situation where passing the parameter requires writing
>> >> someMethod(AbstractSomeClass asc), I create an empty interface called
>> >> SomeClass and then have AbstractSomeClass implements it so that i can
>> >> do
>> >> a someMethod(SomeClass sc)?
>> >
>> > It's a bad idea to create an empty interface

>>
>> So, would java.io.Serializable--an "empty" interface--be
>> a bad idea then? It's called a "marker interface". It indicates
>> the semantics of an implementing class as being serializable.

>
> I'm well aware of the use of marker interfaces, but I don't think the
> OP was refering to a marker interface, but rather an interface with
> public methods.


I'm a little confused by the above. If an interface has
methods, then it isn't empty, and all methods in an
interface are public (because it's a specification of
a contract between the class which implements the
interface and any user of the class).


> Btw, in Java 5 many uses of marker interfaces can be
> replaced with annotations.


Interesting. What does one gain by replacing marker
interfaces with annotations?

GWC


 
Reply With Quote
 
Jesper Nordenberg
Guest
Posts: n/a
 
      07-11-2005
"George Cherry" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "Jesper Nordenberg" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> > I'm well aware of the use of marker interfaces, but I don't think the
> > OP was refering to a marker interface, but rather an interface with
> > public methods.

>
> I'm a little confused by the above. If an interface has
> methods, then it isn't empty, and all methods in an
> interface are public (because it's a specification of
> a contract between the class which implements the
> interface and any user of the class).


Correct. I don't see the cause of your confusion.

> > Btw, in Java 5 many uses of marker interfaces can be
> > replaced with annotations.

>
> Interesting. What does one gain by replacing marker
> interfaces with annotations?


Annotations are more powerful than marker interfaces, for example they
can be inherited to sub classes or not (interfaces are always
inherited), and they can have parameters.

/JN
 
Reply With Quote
 
George Cherry
Guest
Posts: n/a
 
      07-11-2005

"Jesper Nordenberg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> "George Cherry" <(E-Mail Removed)> wrote in
> message news:<(E-Mail Removed)>...
>> "Jesper Nordenberg" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) m...
>> > I'm well aware of the use of marker interfaces, but I don't think the
>> > OP was refering to a marker interface, but rather an interface with
>> > public methods.

>>
>> I'm a little confused by the above. If an interface has
>> methods, then it isn't empty, and all methods in an
>> interface are public (because it's a specification of
>> a contract between the class which implements the
>> interface and any user of the class).

>
> Correct. I don't see the cause of your confusion.
>
>> > Btw, in Java 5 many uses of marker interfaces can be
>> > replaced with annotations.

>>
>> Interesting. What does one gain by replacing marker
>> interfaces with annotations?

>
> Annotations are more powerful than marker interfaces, for example they
> can be inherited to sub classes or not


Why is this an advantage? Doesn't it break
the polymorphism contract?
Polymorphism: an object of a given class can
have multiple forms, either its own class or
any class it extends. If a class does not honor
all the contract of its superclass, doesn't it break
polymorphism?

George W. Cherry


> (interfaces are always
> inherited), and they can have parameters.
>
> /JN



 
Reply With Quote
 
Jesper Nordenberg
Guest
Posts: n/a
 
      07-12-2005
"George Cherry" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "Jesper Nordenberg" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> > Annotations are more powerful than marker interfaces, for example they
> > can be inherited to sub classes or not

>
> Why is this an advantage? Doesn't it break
> the polymorphism contract?
> Polymorphism: an object of a given class can
> have multiple forms, either its own class or
> any class it extends. If a class does not honor
> all the contract of its superclass, doesn't it break
> polymorphism?


Yes, it breaks polymorphism, but in the case of marker interfaces
without methods you don't have much polymorphism anyway. Actually, in
the serialization framework of a project I'm involved in
(jcore.dev.java.net) we've been pondering the different aspects of
marker interfaces and annotations with regard to marking a class as
serializable. We decided on using an annotation that is not inherited
to sub classes, as this will force developers to explicitly mark their
classes as serializable and thus think about the serialization issue.
Serializing instances of classes not marked as serializable will cause
a runtime exception. We feel that this better than automatically
inheriting the serializable attribute from the super class, and
possibly do incorrect serialization of the class instances
(serializing fields that should be transient, incorrect versioning
etc.).

/JN
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DetailsView ASP Control and an empty field not an empty record. ButlerDJIAM ASP .Net 0 11-09-2006 06:40 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
empty lists vs empty generators Brian Roberts Python 12 05-04-2005 08:59 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM
empty/non-empty element John XML 1 07-16-2003 10:23 AM



Advertisments