aspose file tools*
The moose likes Java in General and the fly likes Using Enums inside interface. Need to implement interface ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Enums inside interface. Need to implement interface ?" Watch "Using Enums inside interface. Need to implement interface ?" New topic
Author

Using Enums inside interface. Need to implement interface ?

Kacee Saxena
Ranch Hand

Joined: Mar 04, 2008
Posts: 54
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

Joined: Jun 04, 2008
Posts: 41

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

Joined: Mar 04, 2008
Posts: 54
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

Joined: Jun 04, 2008
Posts: 41

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

Joined: Mar 04, 2008
Posts: 54
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

Joined: Jun 04, 2008
Posts: 41

Agreed.

I was still hoping to be answered by someone. Untill then what you have come up is right.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

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

Joined: Aug 06, 2012
Posts: 333

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.


OCA7
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Enums inside interface. Need to implement interface ?
 
Similar Threads
cannot find symbol - compiler error?
abstract method within an enum ?
Importing Classes in default package
protected access modifier
cannot find symbol - compiler error?