• 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

Question on Interfaces

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic