It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface qn Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface qn" Watch "Interface qn" New topic
Author

Interface qn

Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
Marcus no:7
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 its single inheritance model, but require implementing classes to create the functionality of the Interfaces.
Ans given is 4)
I think choice 2 is also correct.
Java won't allow creation of the reference of an interface with new keyword. Actually if we do so we won't get the reference to the interface but will get reference to an anonymous inner class which implements the interface. Correct me if I am wrong.
RGDs
Manoj
bongadi
Greenhorn

Joined: Apr 17, 2000
Posts: 23
Rule # 1 : Read the question carefully
2) Java does not allow the creation of a reference to an interface with the new keyword.
This is false.
<pre>
<code>
interface anIface {
public void aMethod(); //Interface's method
}
class aClass implements anIface {
public void aMethod() {} // Implementation of aMethod()
}

anIface myIface = new aClass(); // absolutely legal.
</code>
</pre>
I am creating a reference to an Interface with new.
Originally posted by Mani:
Marcus no:7
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 its single inheritance model, but require implementing classes to create the functionality of the Interfaces.
Ans given is 4)
I think choice 2 is also correct.
Java won't allow creation of the reference of an interface with new keyword. Actually if we do so we won't get the reference to the interface but will get reference to an anonymous inner class which implements the interface. Correct me if I am wrong.
RGDs
Manoj

[This message has been edited by bongadi (edited May 05, 2000).]
[This message has been edited by bongadi (edited May 05, 2000).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
I guess both Mani and Manoj ( M & M ) are right.
However, the Million Dollar question is
how should one treat such wordings in the actual
exam? Should we read between the lines or should we
read the text as it appears?. Should we evaluate
the answer applying all the concepts that we know
or only use what has been given to us?
I am very very very . I guess this applies very
well to Maha Anna's new post regarding ambiguous
wordings. I would really like to hear opinions from
people who have already taken the test and have passed
with scores.
Please participate!!

Ajith


[This message has been edited by Ajith Kallambella (edited May 05, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
Bongadi,
I dont agree with you. In your program you are not creating reference to the interface. Actual reference stored is that of aClass object.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
i have added a new method doit() to the subclass and calling it from the reference to anIface interface. this does not work saying "Method doit() not found in interface anIface"
Thus can we conclude that we can only call methods of the interface and not any other.
for example
class xyz implements x,y{}
x abc=new xyz() is permisible but any invocation of methods of the y class on abc reference will raize the above error.

interface anIface
{
public void aMethod(); //Interface's method
}
class aClass implements anIface {
public void aMethod() {} // Implementation of aMethod()

public static void main(String args[]){
anIface x= new aClass(); // absolutely legal.
x.doit();
}
public void doit(){
System.out.println("doit");
}
}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I don't think the test was ambiguous at all. One question was a maybe, but that's because I didn't know that particular material overly well. I think the better you understand the material, the less ambiguous the questions seem.
BTW, Ajith, I think you're ready for the test! What are you waiting for?
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Folks,
I agree that this question is not ambiguous, and we do not really have to read it extra carefully to understand it. But I IMHO 2 should be also a correct answer. I agree with Bongadi that in Mani's snippet... anIface myIface = new aClass we are not creating a reference to an interface, rather we are creating a refence to an obect of type aClass and assigning it to a variable of type anIface. This is only being possible because aClass implements myIface and Java's safe casting works here. It is a plain fact that we can not use new to create a reference to an interface. The wording of the question is oky, but 2 is a correct answer also.Let us admit this.
Herbert.
Eric Barnhill
Rancher

Joined: Feb 25, 2000
Posts: 233

I had one thought on this:
this line:
anIface myIface = new aClass();
might be ambiguous in terms of whether or not you're creating a reference to an interface. Because the reference and the class type are different. But what about this:
1: class firstClass implements Runnable {
2: public void run() {};
3: }
4:
5: class secondClass{
6: public static void main(String[] args){
7: Runnable r = new Runnable(firstClass);
8: r.start();
9: }
10: }
in this case what are you doing in line 7? It seems to me you're creating a reference to an interface with the new keyword. Obviously you're not instantiating the interface, but a reference to it, it seems like you're making.
Eric
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Eric,
1: class firstClass implements Runnable {
2: public void run() {};
3: }
4:
5: class secondClass{
6: public static void main(String[] args){
7: Runnable r = new Runnable(firstClass);
8: r.start();
9: }
10: }
Hey, actually I think line 7 is illegal here, very Illegal I think, unless
I am missing out something. Runnable is an interface and does not have a
constructor. Unless you meant to write as
Runnable r = (Runnable)new firstClass();
In this case, you still would not be creating an instance of the Runnable interface,
rather, you would be creating a reference to an instance of an object of type
firstClass and casting it to the Runnable interface.
Herbert.
Tom P
Ranch Hand

Joined: May 06, 2000
Posts: 88
Actually if you read the question then Runnable r = (Runnable)new firstClass();
proves that two is not a correct answer. You are creating a reference to an interface and you are using the new keyword.
You should notice that 2 does not say anything about instantiating an interface. It simply states that you can create reference to an interface using the new keyowrd.
[This message has been edited by Tom P (edited May 15, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface qn