gudii9
asked on
abstract method in enum
Hi,
I was reading below link
http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html#ixzz39ALX5kTO
and the example
I wonder why we need abstract kind of methods inside enums. what are the practical use of it. what design pattern is being used.Please advise
I was reading below link
http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html#ixzz39ALX5kTO
and the example
public enum Currency implements Runnable{
PENNY(1) {
@Override
public String color() {
return "copper";
}
}, NICKLE(5) {
@Override
public String color() {
return "bronze";
}
}, DIME(10) {
@Override
public String color() {
return "silver";
}
}, QUARTER(25) {
@Override
public String color() {
return "silver";
}
};
private int value;
public abstract String color();
private Currency(int value) {
this.value = value;
}
..............
}
I wonder why we need abstract kind of methods inside enums. what are the practical use of it. what design pattern is being used.Please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This half of the code uses the enum that you defined:
It can go into any class you like.
E.g. You can put that code inside the "main" method of a program to make it run.
This code also uses the enum that you defined but it doesn't make use of the "color" method in the enum.
So it's showing the benefit of having that method inside the enum because this "printMoneyColor" method is a lot longer than the method above.
I hope that helps explain things.
Doug
Currency myCoins = Currency.QUARTER ;
Currency myOtherCoins = Currency.DIME ;
public void printMoneyColor(Currency coins) {
System.out.println("The color of money " + coins + " is " + coins.color()) ;
}
printMoneyColor(myCoins) ;
printMoneyColor(myOtherCoins) ;
It can go into any class you like.
E.g. You can put that code inside the "main" method of a program to make it run.
This code also uses the enum that you defined but it doesn't make use of the "color" method in the enum.
So it's showing the benefit of having that method inside the enum because this "printMoneyColor" method is a lot longer than the method above.
public void printMoneyColor(Currency coins) {
System.out.println("The color of money " + coins + " is ") ;
switch (coins) {
case NICKLE: System.out.println("Bronze") ; break ;
case QUARTER: System.out.println("Silver") ; break ;
...
}
}
I hope that helps explain things.
Doug
ASKER
public abstract String color();
Above is abstract method right in enum right. please advise
ASKER
can you please share complete working example which i can run and see output and understand it completely?
Something like this should work:
Then replace the printMoneyColor() with the other method if you wish to see the alternative.
Doug
public class MyDemo {
public enum Currency implements Runnable{
PENNY(1) {
@Override
public String color() {
return "copper";
}
}, NICKLE(5) {
@Override
public String color() {
return "bronze";
}
}, DIME(10) {
@Override
public String color() {
return "silver";
}
}, QUARTER(25) {
@Override
public String color() {
return "silver";
}
};
private int value;
public abstract String color();
private Currency(int value) {
this.value = value;
}
}
public static void printMoneyColor(Currency coins) {
System.out.println("The color of money " + coins + " is " + coins.color()) ;
}
public static void main(String args[]) {
Currency myCoins = Currency.QUARTER ;
Currency myOtherCoins = Currency.DIME ;
printMoneyColor(myCoins) ;
printMoneyColor(myOtherCoins) ;
}
}
Then replace the printMoneyColor() with the other method if you wish to see the alternative.
Doug
ASKER
package eePackage;
public class MyDemo {
public enum Currency implements Runnable{
PENNY(1) {
@Override
public String color() {
return "copper";
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}, NICKLE(5) {
@Override
public String color() {
return "bronze";
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}, DIME(10) {
@Override
public String color() {
return "silver";
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}, QUARTER(25) {
@Override
public String color() {
return "silver";
}
@Override
public void run() {
// TODO Auto-generated method stub
}
};
private int value;
public abstract String color();
private Currency(int value) {
this.value = value;
}
}
public static void printMoneyColor(Currency coins) {
System.out.println("The color of money " + coins + " is " + coins.color()) ;
}
public static void main(String args[]) {
Currency myCoins = Currency.QUARTER ;
Currency myOtherCoins = Currency.DIME ;
printMoneyColor(myCoins) ;
printMoneyColor(myOtherCoins) ;
}
}
I have to implement Run() method in all methods like PENNY, DIME etc
I wonder why i have to implement run method in all those methodss?
I got output like
The color of money QUARTER is silver
The color of money DIME is silver
Then replace the printMoneyColor() with the other method if you wish to see the alternative.
can you please post the complete code for this alternate solution also. I have not got it.
OK here's the same code with a different print method:
The whole point is to compare the code inside the two printMoneyColor methods. I hope you're able to see that one is shorter than the other.
Also I showed you how to implement Runnable with just one method, not adding it to each member of the enum.
Hope that helps,
Doug
public class MyDemo {
public enum Currency implements Runnable{
PENNY(1) {
@Override
public String color() {
return "copper";
}
}, NICKLE(5) {
@Override
public String color() {
return "bronze";
}
}, DIME(10) {
@Override
public String color() {
return "silver";
}
}, QUARTER(25) {
@Override
public String color() {
return "silver";
}
};
private int value;
public abstract String color();
public void run() {
System.out.println("You can implement Runnable in one place if you want") ;
}
private Currency(int value) {
this.value = value;
}
}
public static void printMoneyColor(Currency coins) {
System.out.println("The color of money " + coins + " is ") ;
switch (coins) {
case DIME: System.out.println("Silver") ; break ;
case NICKLE: System.out.println("Bronze") ; break ;
case QUARTER: System.out.println("Silver") ; break ;
}
}
public static void main(String args[]) {
Currency myCoins = Currency.QUARTER ;
Currency myOtherCoins = Currency.DIME ;
printMoneyColor(myCoins) ;
printMoneyColor(myOtherCoins) ;
}
}
The whole point is to compare the code inside the two printMoneyColor methods. I hope you're able to see that one is shorter than the other.
Also I showed you how to implement Runnable with just one method, not adding it to each member of the enum.
Hope that helps,
Doug
ASKER
public class MyDemo {
public enum Currency implements Runnable{
PENNY(1) {
@Override
public String color() {
return "copper";
}
}, NICKLE(5) {
@Override
public String color() {
return "bronze";
}
}, DIME(10) {
@Override
public String color() {
return "silver";
}
}, QUARTER(25) {
@Override
public String color() {
return "silver";
}
};
private int value;
public abstract String color();
public void run() {
System.out.println("You can implement Runnable in one place if you want") ;
}
private Currency(int value) {
this.value = value;
}
}
public static void printMoneyColor(Currency coins) {
System.out.println("The color of money " + coins + " is ") ;
switch (coins) {
case DIME: System.out.println("Silver") ; break ;
case NICKLE: System.out.println("Bronze") ; break ;
case QUARTER: System.out.println("SilverQuarter") ; break ;
}
}
public static void main(String args[]) {
Currency myCoins = Currency.QUARTER ;
Currency myOtherCoins = Currency.DIME ;
printMoneyColor(myCoins) ;
printMoneyColor(myOtherCoins) ;
}
}
I ran code as above.
I got response as below
The color of money QUARTER is
SilverQuarter
The color of money DIME is
Silver
It is feeling better to see run() method implemented only once at the bottom instead of implementing exch DIME, QUARTER etc blocks.
I wonder why are we implementing runnable? Is there is any relationof runnable with reference to enum or abstract methods. Please advise
ASKER
PENNY(1) {
@Override
public String color() {
return "copper";
}
}
what do you call above block which says PENNY(1).
It is containg color() method but looks like method only, is it is like outer method? or enum method or block. please advise
ASKER
The whole point is to compare the code inside the two printMoneyColor methods. I hope you're able to see that one is shorter than the other.
I see big method is giving more specific message to type of coin being selected with switch case condition right?
I see big method is giving more specific message to type of coin being selected with switch case condition right?
The intention is that both sets of code generate basically the same output. The bigger method takes more code in the printMoneyColor() method. The abstract method approach inside the enum puts more code into the enum.
I wonder why are we implementing runnable? Is there is any relationof runnable with reference to enum or abstract methods. Please adviseThey chose to implement Runnable in the example just because it's a convenient interface that already exists. It's a ridiculous choice for this - you would never "Run" a color. You "Run" tasks. So don't focus on the choice of Runnable. Just that this is an example of any interface and how you could implement it inside an enum.
Hope it's all clear now.
Doug
ASKER
The abstract method approach inside the enum puts more code into the enum.
when you say abstract method you mean below method right
public abstract String color();
which is basically inside the enum.
I do not see enum extends abstract class etc.
So how the abstract method implementation also given in same enum. I am bit confused there. please advise
I do not see enum extends abstract class etc.
Right. In an enum it's a bit different. You can define an abstract method like this:
public enum Currency {
...
public abstract String color();
} ;
which is then implemented by the instances of the enum:
PENNY(1) {
@Override
public String color() {
return "copper";
}
It behaves almost like "PENNY" is a subclass of the enum Currency.
It's not, but it's like that.
Doug
ASKER
which is then implemented by the instances of the enum:
PENNY(1) {
@Override
public String color() {
return "copper";
}
these are instances of enum. Interesting to know and see instances of enum which looks weird compared to object instances but now it all makes more sense
ASKER
It behaves almost like "PENNY" is a subclass of the enum Currency.
does the psudo subclass PENNY does anything other than implementing the abstract method. Does it obey any other object oriented properties of inheritance etc? please advise
ASKER
I am not able to understand above code. Is above code is in a single class or enum?
can you please post me full code.
Is strategy design pattern is java design patern or j2ee design pattern?
please advise