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

Extending Inner class outside the enclosing class

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I was going through an earlier discussion about �Extending Inner Classes�.
The conclusion drawn was �Inner classes cannot be extended outside their outer scope�
http://www.javaranch.com/ubb/Forum24/HTML/001456.html
However, an outer class can extend non-local inner classes though it is not very straightforward.
To extend inner classes, the inner class should have a special constructor that takes a reference to an object of the outer class as an argument. Inside this constructor, a call like �outerclass.super()� should be made to associate the outerclass object to the innerclass object being created. Note that the extended class cannot define a default constructor.
If you know, please share whether this is a specified behaviour or is a hack - fooling the compiler - that may no longer work in future. I think, JLS doesn�t mention about extending inner classes in this way. Is it relevant to SCJP exam? What would be the answer to the SCJP question: A non-static inner class can be extended by code outside the enclosing class (True/False)?
An example of extending an inner class outside its enclosing class follows.
Please correct me if I am wrong.
Thanx,
RajSim

Output:
O + E
O.display
O.I.display
E.display
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
this question seems to be good and also woking very fine.
a non static inner can be extended out side the outer class.
fine
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This thread seems to be posted twice. Let's close this one and follow the other here
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic