posted 12 years ago
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 ]
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 ]
posted 12 years ago
What would it *mean* to override an inner class? 

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
faisal usmani
Ranch Hand
Posts: 139
faisal usmani
Ranch Hand
Posts: 139
posted 12 years ago
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");
}
}
______________________________________________________________________
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");
}
}
posted 12 years ago
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:
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:
There is no emoticon for what I am feeling!
