Empty interface

Discussion in 'Java' started by -, Jul 7, 2005.

  1. -

    - Guest

    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)?
     
    -, Jul 7, 2005
    #1
    1. Advertising

  2. 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"
     
    Stefan Schulz, Jul 7, 2005
    #2
    1. Advertising

  3. In article <42cc87a6$>, - <> 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.
     
    Kevin McMurtrie, Jul 7, 2005
    #3
  4. - <> wrote in message news:<42cc87a6$>...
    > 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
     
    Jesper Nordenberg, Jul 7, 2005
    #4
  5. "Jesper Nordenberg" <> wrote in message
    news:...
    >- <> wrote in message
    >news:<42cc87a6$>...
    >> 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
     
    George Cherry, Jul 7, 2005
    #5
  6. "George Cherry" <> wrote in message news:<>...
    > "Jesper Nordenberg" <> wrote in message
    > news:...
    > >- <> wrote in message
    > >news:<42cc87a6$>...
    > >> 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
     
    Jesper Nordenberg, Jul 8, 2005
    #6
  7. "Jesper Nordenberg" <> wrote in message
    news:...
    > "George Cherry" <> wrote in
    > message news:<>...
    >> "Jesper Nordenberg" <> wrote in message
    >> news:...
    >> >- <> wrote in message
    >> >news:<42cc87a6$>...
    >> >> 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
     
    George Cherry, Jul 8, 2005
    #7
  8. "George Cherry" <> wrote in message news:<>...
    > "Jesper Nordenberg" <> wrote in message
    > news:...
    > > 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
     
    Jesper Nordenberg, Jul 11, 2005
    #8
  9. "Jesper Nordenberg" <> wrote in message
    news:...
    > "George Cherry" <> wrote in
    > message news:<>...
    >> "Jesper Nordenberg" <> wrote in message
    >> news:...
    >> > 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
     
    George Cherry, Jul 11, 2005
    #9
  10. "George Cherry" <> wrote in message news:<>...
    > "Jesper Nordenberg" <> wrote in message
    > news:...
    > > 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
     
    Jesper Nordenberg, Jul 12, 2005
    #10
  11. "Jesper Nordenberg" <> wrote in message
    news:...
    > "George Cherry" <> wrote in
    > message news:<>...
    >> "Jesper Nordenberg" <> wrote in message
    >> news:...
    >> > 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.


    Ahh, that's a distinct advantage. GWC

    > 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
     
    George Cherry, Jul 12, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,054
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    850
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    600
    Dave Thompson
    Feb 14, 2004
  4. Brian Roberts

    empty lists vs empty generators

    Brian Roberts, May 3, 2005, in forum: Python
    Replies:
    12
    Views:
    676
    Jeremy Bowers
    May 4, 2005
  5. ButlerDJIAM
    Replies:
    0
    Views:
    578
    ButlerDJIAM
    Nov 9, 2006
Loading...

Share This Page