Interface implementations

Discussion in 'Java' started by Sparko, Mar 2, 2004.

  1. Sparko

    Sparko Guest

    If I define an interface with no method(s) just some public static
    final constants for instance, can I then refer to these statics in the
    implementing class or are they only available within an interfaces
    implemented methods?
     
    Sparko, Mar 2, 2004
    #1
    1. Advertising

  2. Sparko wrote:

    > If I define an interface with no method(s) just some public static
    > final constants for instance, can I then refer to these statics in the
    > implementing class or are they only available within an interfaces
    > implemented methods?


    Have you tried it? What happens?

    Btw, it is discussable that implemnting an interface to use static final
    constants is bad form. You should use a class for it, and then refer to
    them explicitly, like ClassWithConstants.SOME_CONSTANT instead.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Mar 2, 2004
    #2
    1. Advertising

  3. Sparko

    Peter Kirk Guest

    "Christophe Vanfleteren" <> skrev i en meddelelse
    news:8iZ0c.19697$-ops.be...

    > Btw, it is discussable that implemnting an interface to use static final
    > constants is bad form. You should use a class for it, and then refer to
    > them explicitly, like ClassWithConstants.SOME_CONSTANT instead.


    But you could still define the constants in the interface, and use them
    without implementing the interface. What is the difference between defining
    public constants in a "class" and in an "interface"?

    Peter
     
    Peter Kirk, Mar 2, 2004
    #3
  4. Sparko

    Andy Fish Guest

    I think it's a matter of choice depending on the particular circumstance

    if you have a few classes that don't have a common superclass and they make
    extensive use of the same few constants then I think it would generally add
    to readibility. I don't think it would usually be wise to implement more
    than one constant-only interface in any given class though.

    just my 2 cents

    "Christophe Vanfleteren" <> wrote in message
    news:8iZ0c.19697$-ops.be...
    > Sparko wrote:
    >
    > > If I define an interface with no method(s) just some public static
    > > final constants for instance, can I then refer to these statics in the
    > > implementing class or are they only available within an interfaces
    > > implemented methods?

    >
    > Have you tried it? What happens?
    >
    > Btw, it is discussable that implemnting an interface to use static final
    > constants is bad form. You should use a class for it, and then refer to
    > them explicitly, like ClassWithConstants.SOME_CONSTANT instead.
    >
    > --
    > Kind regards,
    > Christophe Vanfleteren
     
    Andy Fish, Mar 2, 2004
    #4
  5. "Peter Kirk" <peter> wrote:

    > "Christophe Vanfleteren" <> skrev i en meddelelse
    > news:8iZ0c.19697$-ops.be...
    >
    >> Btw, it is discussable that implemnting an interface to use static final
    >> constants is bad form. You should use a class for it, and then refer to
    >> them explicitly, like ClassWithConstants.SOME_CONSTANT instead.

    >
    > But you could still define the constants in the interface, and use them
    > without implementing the interface. What is the difference between
    > defining public constants in a "class" and in an "interface"?
    >
    > Peter


    You are correct, you could use both in such a case. My point was more about
    the fact that it isn't necessary to implement an interface, and is even
    better not to, for reasons like binary compatibitly, encapsulation (other
    classes using your class don't really care that you use certain constants
    to do the job, so why make this public?), ...

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Mar 2, 2004
    #5
    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. Wildepiet
    Replies:
    0
    Views:
    1,880
    Wildepiet
    Jun 14, 2004
  2. Marcus Crafter
    Replies:
    1
    Views:
    359
    Mike Schilling
    Feb 19, 2004
  3. z-man
    Replies:
    15
    Views:
    620
    Chris Uppal
    Oct 11, 2006
  4. Chris
    Replies:
    2
    Views:
    324
    Chris
    Mar 15, 2007
  5. Replies:
    11
    Views:
    539
Loading...

Share This Page