jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface 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 "interface" Watch "interface" New topic
Author

interface

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Question no.7 from Marcus 3.
Which of the following statements are true?
1) An interface can only contain method and not variables
2) Java does not allow the creation of a reference to an interface with the new keyword.
3) A class may extend only one other class and implement only one interface
4) Interfaces are the Java approach to addressing the single inheritance model, but require implementing classes to create the functionality of the Interfaces. //true
Why not choice 2) is true, since interface can not be instantiated ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I just took this mock today and did the same thing - just answered #4.
The only thing I can figure about that is if he means:
class one implements oneInterface{
}
public static void main(String args[]){
oneInterface oi = new one();
}
Nilesh Parikh
Ranch Hand

Joined: Mar 14, 2000
Posts: 52
hi
Marcus's third Mock test is still not public and it got lot of errors.
I scored only 62% in it
Alkesh
Rolf Weasel
Ranch Hand

Joined: Feb 26, 2000
Posts: 82
what the question asks is if this is ok:
oneInterface oi=new oneInterface();
it is'nt.
u can have:
oneInterface oi=new one();

May the Moose be with you.
Remco Rotteveel
Greenhorn

Joined: Mar 21, 2000
Posts: 8
And what about this:
<PRE>
interface Inter {
public void amethod();
}

class InterfaceTest {
public static void main (String[] args) {
Inter test = new Inter() {
public void amethod() {
System.out.println("amethod called");
}
};
test.amethod();
}
}
</PRE>

It compiles and runs just fine and I have used the new keyword.
[This message has been edited by Remco Rotteveel (edited March 26, 2000).]
[This message has been edited by Remco Rotteveel (edited March 26, 2000).]
Nalini Mistry
Ranch Hand

Joined: Mar 21, 2000
Posts: 64
how did that work?? shouldnt the class decl read
class InterFaceTest implements Inter

[This message has been edited by Nalini Mistry (edited March 26, 2000).]
Rolf Weasel
Ranch Hand

Joined: Feb 26, 2000
Posts: 82
u have'nt instantiated the interface with the new, you've instantiated an anonymous class that implements the interface.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
What the question asks is if you can create a "reference to an interface". That's a bit vague, and I can see where there is room for disagreement, but in my opinion Remco's answer qualifies. Also, though Tony is probably busy right now, I know that when this same question came up recently on Marcus' group, he thought an anonymous class would qualify.
I see two possible interpretations:
(1) A "reference to an interface" is something that can never exist, since an interface cannot be instantiated, and the phrase is essentially meaningless. Then the rest of the question is irrelevant, and the answer is false.
(2) A "reference to an interface" is shorthand for "a reference to an instance of a class which implements an interface". Then an anonymous class does qualify, and the answer is true.
Is there another possible meaning of the phrase I've missed? The second one makes sense to me. Look at the method addActionListener() in Button and other classes. What kind of argument does it take? An ActionListener. Well, a reference to an ActionListener. Well, really a reference to a class which implements ActionListener. But we usually accept the first as an acceptable shorthand, and only use the second or maybe third when we need to distinguish between a reference and the object it refers to.
So, I think the question is fine as is. Yeah, I know that's out of character for me. I'm gaining respect for how difficult it can be to write a clear, unambiguous test question.

"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for your input, everyone!
I must disagree with the person who took test #3 and said it is full of errors. I only scored a 70 on it, but besides a few SLIGHTLY ambiguous questions, it is well written. I certainly would have a difficult time writing unambiguous questions!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, "full of errors" is an exaggeration. But Marcus himself has acknowledged that he's not done getting the bugs out of this one yet, that's why his site doesn't officially announce test 3 yet. He's fixed a number of things already, and I know there's at least one question he's fixed on his "working copy" that hasn't been posted yet. On the one hand I agree that exam 3 is pretty good overall, and certainly better than a number of other mock exams out there. But at the same time, don't be too surprised if you find something that looks like an error, or that can be interpreted in different ways. Marcus appreciates constructive feedback on his tests too - if something isn't fixed right away, it's only because he's a busy guy.
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Can an interface be declared as private or protected and if we don't specify any access modifier before interface keyword will it be public by default???
Thanks.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It can have access modifiers of public or blank(package) only, just like a class.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
...unless it's a nested interface. Then it can have any access specifier, just like a nested class.
If you omit the access specifier, it's package access, again just like a class.
You may be thinking of the fact that the methods and static final fields defined within an interface are implicitly public, whether they say so or not. In this case private and protected are illegal, and no modifier means the same as public. But this has nothing to do with the access level of the interface itself, defined just before the word "interface" in the declaration.
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Thanks Jim & Pk.
Ihor Strutynskyj
Greenhorn

Joined: Mar 28, 2000
Posts: 22
With all this discussion we missed one point. 1). is also correct. If you run the following code:

which prints 5. Doesn't it mean that interface can contain:
- only methods,
- methods and variables,
- only variables?
The last two seem awkward and conflict with the whole idea of interfaces, but the Java syntax allows it, which means someone can/may use it.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hmmm... I think you're interpreting the "only" a bit differently than the rest of us. In the original statement:
1) An interface can only contain method and not variables
I (and I think most of us) interpret that to mean (a) an interface can contain methods, and (b) an interface can not contain variables. The second part is false as you've demonstrated.
If the statement had been
1) An interface can contain only methods and no variables
then it would have been more ambiguous and could have been interpreted as: it's possible to have an interface which has methods and no variables. Which of course would be true. But I don't think that's what the actual statement means.
Note also that whenever variables appear in interfaces, they are implicitly static and final (even if they don't say so). Which means they're really more like constants. "Variables" like this are often used in interfaces; check out java.sql.ResultSet.TYPE_FORWARD_ONLY etc., java.text.CharacterIterator.DONE, or javax.swing.WindowConstants.DISPOSE_ON_CLOSE.
Ihor Strutynskyj
Greenhorn

Joined: Mar 28, 2000
Posts: 22
This just proves how carefull each question should be read and reread and reread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface
 
Similar Threads
mock question
Marcus #3 Q7 Interfaces can have constructors?
Question on Interfaces
Marcus exam 3 q7
Question on Interfaces