• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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:
  • Quote
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're following up directly on an old post, it might make more sense next time to post your question as a followup to the old post, rather than starting a new thread. The whole thread will then show up as "new" for people logging in, and this way the discussion is easier for everyone to follow. Anyway though...
This behavior is indeed specified in the JLS 2nd edition draft, section 8.8.5.1 - Explicit Constructor Invocations. (I don't recall seeing it in the original inner classes specification, but that's too poorly written for me to want to look for it.) I'm pretty sure it's outside the scope of the exam - it's a rather subtle detail.
As for the statement: "A non-static inner class can be extended by code outside the enclosing class (True/False)?" - this is true, as you've shown. You appear to have caught the mighty Ajith in an error, congratulations!
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good catch! That's a tricky one, wow.
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic