• 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

Can inner classes be overidden ?

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can we override an inner class in a derived class ?

thanx in advance.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, classes can never be "overridden". You can extend a class, including an inner class. But if a class is constructed by name in a piece of code, there's no way to make that code use some other class (which is the situation I expect you're facing.)

The way to fix this kind of problem is to move the constructor calls in the base class into a sort of "factory method", and then override the factory method in the subclass; i.e., if you have something like this:



then change it to this:



Now in a subclass, you can say



Now Subclass.foo() will use an instance of SubclassInner, not Inner itself.
[ April 24, 2006: Message edited by: Ernest Friedman-Hill ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would it *mean* to override an inner class?
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry Ilja for asking stupid questions :roll: . Thanx to Ernest.


One last thing is it possible to extend an inner class in a derive class directly.

There is no duty more obligatory than the repayment of kindness.
[ April 24, 2006: Message edited by: faisal usmani ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the inner class is public then you can certainly write
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Paul, but this code gives me error !!

______________________________________________________________________

class Outer
{
public class Inner
{
public void show()
{
System.out.println("Hello");
}
}
}

class INHERIT extends Outer.Inner
{
public static void main(String ar[])
{
System.out.println("Compiled");
}

}
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner needs an enclosing object to be constructed:

I'm not recommending this as good coding style. Needing to subclass Inner is usually a sign that (1) Inner should be a full-fledged top-level class, or (less likely) (2) Inner should be subclassed but only within a subclass of Outer:
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Jeff,







That best portion of a good man's life, His little, nameless, unremembered acts of kindness and of love. No act of kindness, no matter how small, is ever wasted
 
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic