Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class mult choice

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which statement is true about an inner class?
A. It must be anonymous
B. It can not implement an interface
C. It is only accessible in the enclosing class
D. It can only be instantiated in the enclosing class
E. It can access any final variables in any enclosing scope.
Their answer: E
My answer: C and E
C- because you need:
OuterClass.InnerClass x = new OuterClass().new InnerClass()
Whos right?
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Salerno:
My answer: C and E
C- because you need:
OuterClass.InnerClass x = new OuterClass().new InnerClass()


You could do OuterClass.InnerClass x = new OuterClass().new InnerClass() from some OTHER class, though, and doesn't have to be done inside the "enclosing class"(the class that has the inner class in it).
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its only E
as the statement C says---it "can only" be instantiated in the enclosing class
but this is not true as the non static inner classes can be instantiated in any class but
it's instance can be created in context of an
instance of the enclosing class..ie..
Outer.Inner in=Outer.new().new Inner();

swapna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Salerno:
Which statement is true about an inner class?
A. It must be anonymous
B. It can not implement an interface
C. It is only accessible in the enclosing class
D. It can only be instantiated in the enclosing class
E. It can access any final variables in any enclosing scope.
Their answer: E
My answer: C and E
C- because you need:
OuterClass.InnerClass x = new OuterClass().new InnerClass()
Whos right?

The option C says
C. It is only accessible in the enclosing class
They can be accessed outside of the enclosing class too. Yes you need an Outer Class instance to INSTANTIATE an Inner Class instance(not true for static inner classes), but that instance can invoke methods of Inner class from Other classes too.
So C is not right!!!
Amish
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic