• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Marcus's third Mock test is still not public and it got lot of errors.
I scored only 62% in it
Alkesh
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what the question asks is if this is ok:
oneInterface oi=new oneInterface();
it is'nt.
u can have:
oneInterface oi=new one();
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u have'nt instantiated the interface with the new, you've instantiated an anonymous class that implements the interface.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can have access modifiers of public or blank(package) only, just like a class.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim & Pk.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This just proves how carefull each question should be read and reread and reread.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic