• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Interface qn

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Mani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic