Interface implementations

S

Sparko

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?
 
C

Christophe Vanfleteren

Sparko said:
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.
 
P

Peter Kirk

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
 
A

Andy Fish

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
 
C

Christophe Vanfleteren

Peter Kirk said:
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?), ...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top