• 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

Object of an Interface

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus Mock exam 2: Q#57
Which of the following statements are true?
1) Adding more classes via import statements will cause a performance overhead, only import classes you actually use.
2) Under no circumstances can a class be defined with the private modifier
3) A inner class may under some circumstances be defined with the protected modifier
4) An interface cannot be instantiated
the given answers are
3 & 4
but it sould be only 3 because we can make an object of an interface in the case of anonymous Inner Class by the following way:
interfaceX obj=new interfaceX(){ //anonymous inner class};

please put some comments on it
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"but it sould be only 3 because we can make an object of an interface in the case of anonymous Inner Class by the following way:"
interfaceX obj=new interfaceX(){ //anonymous inner class};
That code is shorthand for creating a class that subclasses Object and implements the interface.
Bill
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InteraceX obj = new InterfaceX( {anon. class}); will create an object of that class and reference will be stored in variable obj. You're not instantiating an INTERFACE rather a CLASS(although and anon. one)
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to think of it this way.
Ifc i = new Ifc() { /* class body statements */ };
is not instantiating the interface Ifc, but is instantianting a class that implements the interface.
You can see this if you look at the class objects for the interface, and the class you instantiated:
System.out.println(Ifc.class.toString());
System.out.println(i.getClass().toString());
Think of it as shorthand for
final class AClass implements Ifc {
/* class body statements with no constructor*/
};
Ifc i = new AClass();
You are instantiating class the defined by the body statements that implements the interface, not the interface itself.
You may find it helpful to consider this example of an anonymous class that extends another class, instead of implementing an interface.
abstract class AClass {
protected int x;
AClass(int i) { this.x = i;}
abstract void m() ;
}
class Junk7 {
public static void main(String[] args) {
AClass a = new AClass(7) {
void m() {
System.out.println(x); // Just so we can see it do something
}
};
a.m();
System.out.println(AClass.class.toString());
System.out.println(a.getClass().toString());
}
}
Clearly, refers to an instance of an anonymous class (probably the class named Junk7$1), not to an instance of the abstract class AClass.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4) An interface cannot be instantiated
An interface is abstract by definition and therefore can not be instantiated. Variables of an interface can be declared and objects of classes implements the interfaces (as the anonymous class shown) and their subclasses can be assigned to the variable.
You may want to refer to Mughal's book, it has a good discussion on this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic