• 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

Clear Understanding of Inner Class..

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shall we come to a conclusion like this for inner class?
Anonymous Inner Class
1) Anonymous inner class is allowed to extend AT MOST one class or to implement AT MOST one interface explicitly.
2) Anonymous inner class always assumed to extend Object if it is not extending any other class or any other interface explicitly.
3) If Anonymous inner class extends a class, then it is not possible to extend anyother class including Object or implement an interface explicitly. But of course, the extended class must be derived from Object.
4) If Anonymous inner class implements a interface, then it is not possible to extend anyother class including Object or implement an interface explicitly. But of course, the extended interface must be derived from Object.
5) Anonymous inner class is one type of Local Inner class.
6) It can call superclass constructor. But how?
Local Inner Class
1) It has only access to the final variables of the enclosing method or final paramer passed to the method.
2) It can extend any number of classes or implement any number of interfaces.(Not sure)
3) Can not be decared with any modifier(only default is allowed)
4) static is not allowed
5) Can be declared inside a block like { /*Inner Class */} But why?
Member Inner class
1) All modifiers are allowed including private and protected.
2) static class is possible
3) Has access to enclosing class including private.
Correct me guys, if anything wrong in this and add more points to this to get a basic understanding of the Inner Classes.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thiru
For your annonymous section:
1, 3 and 4 say about the same thing just in different words. In #2 you say it is assumed to extend Object if it doesn't explicitly name a parent class or interface. You can't have an implied super class for an annonymous class or how would you write the code?
On the local class section:
3 and 4 say the same thing.
For number 5, it has to be defined in the block it is local to, thus the name.
Number two, no class whether it is a nested class or a top level class can extend more then one other class.
On #1, it also has access to all variables of the enclosing class instance unless it is defined inside a static block.
You could also break up your last section into two parts: inner classes and static member classes. from my notes:
static member classes:
can not access member variables of enclosing class, can only access static variables of the enclosing class.
created with: InnerClass ic = new OuterClass.InnerClass();
inner classes:
Cannot have member interfaces, can have static variables but they must be final
cannot have static initializers. Because the static variables are final they must be defined when declared
Cannot contain static methods (even if they are final)
All non static inner classes have a reference to (are associated with) an outer class object.
Can access the outer class variables by simply refering to the variable name.
If an inner class shadows a variable of the enclosing class then the enclosing class variable can be accessed by doing: OuterClass.this.variable Create an inner class instance in a method of the enclosing class (ie there is an instance of the enclosing class) using only the inner class name.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Thiru, this code responds some of your questions. (I hope)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic