aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green Exam #3, Question #22 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 "Marcus Green Exam #3, Question #22" Watch "Marcus Green Exam #3, Question #22" New topic
Author

Marcus Green Exam #3, Question #22

Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61
Here is the original question:
Which of the following statements are true?
1) All of the variables in an interface are implicitly static
2) All of the variables in an interface are implicitly final
3) All of the methods in an interface are implicitly abstract
4) A method in an interface can access class level variables
The answer given is 1, 2, 3
I would contend that 4 is also correct. If a class implements
an interface, then all the methods in the interface would be
able to access class level variables, right? Please give your
opinion. Thanks! Sam
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
No, Marcus is correct. Interfaces have no bodies, so how would they access any variables. Even so, your theory is still incorrect as if you had two classes and one class subclassed another, the base class doesn't have access to the subs variables, it is the subclass that has access. So in this case, if it said:
A method in a class implementing an interface can access the interfaces variables (which are static and final), that would be true.
But it doesn't work the other way around.
Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61
Hi, Bill,
Thanks for the reply. Here is what I mean...and I believe is
what the question in the exam means :-)
interface Marcus{
void green1();
}
class MyClass implements Marcus{
int x;
float y;
void green1(){
//Bill, here method green1() should be able to
//access any class level variables x, y declared in
//MyClass, right? If your answer here is yes, then
//answer 4 should be correct.
}
}
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
You are right that void green1 inside MyClass can access x and y, but that method is inside MyClass, not the interface's method, but the classes method. The question says:

A method in an interface can access class level variables

And the method in the interface cannot, the overridden method in the class can, but not the method in the interface.
Bill
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
I think that the possibilities for subtle ambiguity in this question illustrate why Sun went for an exam format with more code snippets. Generally with a code snippet you are right or wrong, it will either compile or not, and you can test it with the java compiler.
On the other hand questions like this pack in more knowlege testing per question.

Marcus

------------------
http://www.jchq.net Mock Exams, FAQ, Tutorial, Links, Book reviews
=================================================
Almost as good as JavaRanch
=================================================


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Marcus Green Exam #3, Question #22