Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Enums inside interface. Need to implement interface ?

 
Kacee Saxena
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello techies,

I have a code where I have declared one interface, that only contains one enum and a setter for it. Since enum can be directly accessed, and interface fields are public , static by default, (final doesnt matter to me , with enums) . Here is a code that doesnt need that interface to implement ever. Although i dont think it is a good practice to declare an interface, and never implement it , and directly access its state variables. Please have a look.


One user class , that needs to access the information from interface


Output :
displaying: RUPEE

Here I have few questions:
1) First , why need an interface just for a single state variable. This interface holds nothing more than one variable and its setter. ( I have been told that its a good design practice if Currency information is coded this way)
2) In case point 1 is accepated, then question is if Test.java class implementation is working fine, then when the need would arise to implement the interface ?
3) Other way , just for the sake of implementing, I can have Test class implement the interface and have knit looking getters/setters like



// I consider above as a bad practice.

3) Somehow I am not convinced with the code shown with class Test.java ( previous one, not implementing interface). What is the best way to access Enum from an interface.

4) In my app, Test class is exposed to other layers. The value in Test class is getting set from outside class. In my opinion, better way would be

- From outside class,
public void outsideClassSomeMethod{
Test.CurrencyType = RUPEE; // instead of setting via setter
}

- In Test Class,
public class Test implements Currency and
Sysout("Display : " + Test.CurrencyType.toString()); // should access static fields in static way

Please pour in your comments, which is the best way to do this.
 
Harsha Gedala
Ranch Hand
Posts: 41
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kacee,
I am not a techie guy, but thought this would be helpful. I would rather not use interface int his kind of scenario but, it it is there then will use it as mentioned below

Interface:


Test Class:


Hope this helps.
 
Kacee Saxena
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have to directy access like - <interface>.<variable>
i.e.
Currency.currency currencyVal
setVal(Currency.currency.RUPEE);

then why to implement interface ? This can be anyways done just by importing interface. As the field (enum) is already public static and is accessible outside to any class importing this interface.
 
Harsha Gedala
Ranch Hand
Posts: 41
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess i was late in replying.
Yes you are right. No need to implement the interface as you can directly access it. I dont know the other method and its related declarations in interface which are useful for other classes. If you only have enum inside that interface then you can implement as below.

Enum:


Test Class:


But interfaces has better definition in larger implementations than just small implementation. I have found a good explanation, have a look at it.


Why do we NEED interfaces? Well we probably don't NEED them but they do come in ever so helpful.

At a very basic level they allow us to specify a features of a class. For example if a class implements Comparable than I know that it has the compareTo() method. I also know that this object is compatible with various methods that use the compareTo() method. For example if I make a List that is Comparable then I can use Collections.sort() to sort my list.

They also allow for abstraction. I may want to implement an algorithm that searches a string for a particular word. Now I COULD just have the argument be a String -- but since strings are an immutable class they are not always the best place to store textual data (especially while it is being processed) so if I step back and make take an argument of CharSequence then I now know that I can pass in Strings or CharBuffer, or StringBuffer -- So my method is now more abstract and more useful.

Interfaces let us decouple the implementation from the interface. This has solved a great number of problems dealing with versions. Allowing newer code to plugin to older code without having to recompile the whole project.

In Java you can only have one parent class -- but you can implement many interfaces. That means that I can create a class that is Compareable, Serializable, CharSequence, DataInput, And DataOutput. Meaning that I created a string that can be both written to (DataOutput) and read From (DataInput), I can make comparisons and I can serialize it (for persistance or to transmit over a communications channel). I can do all of this and have still derive the class from any base class I want.

Interfaces allow me to abstract objects so that I can have a collection of say Animals without careing what kind of Animals are in the collection.

Interfaces for the key to many design patterns.
Interfaces are key to most application frameworks.
Interfaces increase abstraction allowing us to make more and more generalized designs (and thus fancy application frameworks).




 
Kacee Saxena
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Harsha.
I also thought somewhat on the similar lines. Theoretically, Interfaces define a default behavior . In my class, Currency term itself is bit abstract. I can not think much about what behavior this class can hold. Still, roughly, there may be a need of certain methods like
getCurrency()
currencyConvert(from,to)
currencyExchangeRate(year,from,to)

and if I use a class here, which later results in a superClass, then for a subclass usage of these methods may be a small job, and it may have to do bigger task by extending one of other more relevant and useful class. Extending Currency superclass may result into a waste.
Hence, interface can be used.

Is this understaning correct for having an interface for Currency ? Any better explainations are most welcome.

After some thinking , now I am using this class in following manner:


 
Harsha Gedala
Ranch Hand
Posts: 41
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed.

I was still hoping to be answered by someone. Untill then what you have come up is right.
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kacee Saxena wrote:Although i dont think it is a good practice to declare an interface, and never implement it , and directly access its state variables.


Then you don't understand interfaces. An interface can never have a state variable, you can only declare static final variables in an interface.

Your example interface contains a single method:


So anything which implements the interface must implement that method. An implementer might use a CurrencyType variable internally to assist with implementing that method, but it might not. That's the whole point -- the interface just says what the implementer must do, it doesn't say how it must do it.

As for this particular example: you seem to be fishing for somebody to provide you a real-life example of how it might be used. Sorry, I can't do that because I can't think of one either.

You also seem to have a question about whether it's a good idea to declare an enum inside an interface. That's a completely separate question. As you've seen, the language permits it. The language also permits declaring an enum on its own, not inside any class or interface. Both are legitimate things to do. However declaring an enum inside an interface doesn't require any class to ever implement that interface -- if that was a question you had. Declaring an interface likewise doesn't require any class to ever implement the interface, although it's rather pointless to write an interface which isn't going to be implemented. That's true whether or not it contains an enum.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
You also seem to have a question about whether it's a good idea to declare an enum inside an interface. That's a completely separate question. As you've seen, the language permits it. The language also permits declaring an enum on its own, not inside any class or interface. Both are legitimate things to do. However declaring an enum inside an interface doesn't require any class to ever implement that interface -- if that was a question you had. Declaring an interface likewise doesn't require any class to ever implement the interface, although it's rather pointless to write an interface which isn't going to be implemented. That's true whether or not it contains an enum.


Thanks Paul, this was i am looking.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic