Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Interfaces

 
robl
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic