File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constant Specific class body [ENUM] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constant Specific class body [ENUM]" Watch "Constant Specific class body [ENUM]" New topic
Author

Constant Specific class body [ENUM]

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Hi,
As we know that a constant specific class body will usually have a overriden method. For example in below code take from SCJP book, getLidCode() method is overriden in LARGE and EXTRA_LARGE constants. Notice that EXTRA_LARGE constant has another method defined getSpecial(), Can anyone tell me how to invoke getSpecial()?
enum CoffeeSizesWithOverriden {

SMALL(1), BIG(3), LARGE(5){
public String getLidCode() {
return "B";
}
}, EXTRA_LARGE(10){
public String getLidCode() {
return "C";
}
int special=10;
public int getSpecial() {
return special;
}
};

private int ounces;

CoffeeSizesWithOverriden(int ounces) {
this.ounces = ounces;
}

public int getOunces() {
return ounces;
}

public String getLidCode() {
return "A";
}
};

Thanks
Deepak


SCJP, SCWCD, SCBCD
Murali Kakarla
Ranch Hand

Joined: Jul 11, 2007
Posts: 80
You cannot invoke getSpecial() method unless it is defined in enclosing enum type.

Here is the notes from java lang spec 3.0 (page 250):

Instance methods declared in these class bodies are may be invoked outside the enclosing enum type only if they override accessible methods in the enclosing enum type.


Murali...
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well!!!
But as per the program given the method getSpecial()....is defined inside the enum body
EXTRA LARGE(),than why we cannot call the method getspecial()........
please if anyone could ellaborate and explain it will be kinda of you ..........


Thanks in advance.................
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by dhwani mathur:
than why we cannot call the method getspecial()
The overriding is implemented with anonymous inner classes. As result, it's impossible (without using the refelction API) to call methods that are not in the parent class.
[ July 13, 2007: Message edited by: Manfred Klug ]
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well!!!Thank you Manfred
for the explanation,but now i am bit confused with anonymous inner class ,is there a difference between anonymous inner class and normal inner class,if yes please can you explain?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by dhwani mathur:
is there a difference between anonymous inner class and normal inner class
There is no distinct type for anonymous inner classes. There is no way to tell the compiler that there is a class which extends Animal and has a method named hello.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well!!! Manfred what i understood from the below shown code is here
class Test is anonymous inner class please let me know if i am correct or not ?
but code i have made it bold just to ask one more question after method hello() there are empty braces but why have you used a semicolon is it syntax also the keyword static?


It will be kinda of you if you ellaborate and explain..........Thanks in advance.........
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by dhwani mathur:
is here class Test is anonymous inner class
Not Test, the instance of Animal is an anonymous inner class.

but why have you used a semicolon is it syntax also the keyword static?
Try it.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
welll!!!Manfred definately i will try the above given code.........and let you know if again arrive on some doubts....anyways thanks for the explanation up to now..............
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
You guys have diverted the topic to inner classes, Can we return back to enum and solve this question.
Please help
Thanks
Deepak
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Deepak Jain:
You guys have diverted the topic to inner classes, Can we return back to enum and solve this question.
Since the overriding is implemented with anonymous inner classes, they are the key for this question. If you are interested in the answer without internals, look at the post from Murali Kakarla.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constant Specific class body [ENUM]