aspose file tools*
The moose likes Beginning Java and the fly likes Question on Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on Interfaces" Watch "Question on Interfaces" New topic
Author

Question on Interfaces

robl
Greenhorn

Joined: Aug 26, 2000
Posts: 25
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.
The answer did not include number 2, but I thought that an interface could not be instantiated.
Please explain why the new keyword can be used with an interface.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The way I'm reading this, 2 is false because you can create an Interface reference to any object that implements that interface. Such as :

Notice, the reference is of an interface type, however the new keywork creates an Object that implements that interface so... An Interface REFERENCE can be created with the new keyword but, you cannot create a direct object of an interface with the new key word.

I hope I'm on the right track here and that this clears things up for you.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
you need to differentiate b/w interfaces and abstract classes. the later cannot be instantiated you van instantiate the former to a class that implements the interface or to the interface itself. However in the latter case you will not be able to do much because the definitons of the member fuctions have still to be catered to
robl
Greenhorn

Joined: Aug 26, 2000
Posts: 25
Originally posted by Carl Trusiak:
[B]The way I'm reading this, 2 is false because you can create an Interface reference to any object that implements that interface. Such as :

Notice, the reference is of an interface type, however the new keywork creates an Object that implements that interface so... An Interface REFERENCE can be created with the new keyword but, you cannot create a direct object of an interface with the new key word.

I hope I'm on the right track here and that this clears things up for you.[/B]

That must have been what the author of the question was talking about by stating that a REFERENCE to an interface could be created using the new keyword. Excellent example to explain the point. Definitely a tricky question.
Thanks,
Rob
 
 
subject: Question on Interfaces