overriding inner classes

T

Tom

Doesn't java support overriding inner classes?

// BEGIN EXAMPLE
// class code ------------------------------------------

public class ClassA{
LocalClass instance;

// inner class
private class LocalClass{
public void doit(){
System.out.println("Hello");
}
}

ClassA(){
instance = new LocalClass();
}

public void doit(){
instance.doit();
}
}

public class ClassB extends ClassA{
LocalClass instance;

// inner class
private class LocalClass{
public void doit(){
System.out.println("Goodbye");
}
}
}

public class Main
{
public static void main(String[] args){
ClassA ca;
ClassB cb;

ca = new ClassA();
cb = new ClassB();

ca.doit();
cb.doit();
}
}

// output -------------------------------------

$ java Main
Hello
Hello

// END EXAMPLE

Me is confused!

Tom
 
V

VisionSet

doit in the top level classes only appears in class A

so that will always be executed for class B or class A instances

if that is executed then it calls class A's innerclass method not class B's

As to overriding by CLASS, you have ClassA.LocalClass & ClassB.LocalClass
how can they override the names are different. So basically no you can't.
 
P

P.Hill

Tom said:
Doesn't java support overriding inner classes?

Overriding is not a concept that applies to classes.
You must subclass the inner class.
// inner class
private class LocalClass{

So what class are you suggesting this class should
be the subclass of? Would you like to tell the
compiler your answer?

-Paul
 
A

Adam

Doesn't java support overriding inner classes?
// BEGIN EXAMPLE
// class code ------------------------------------------

public class ClassA{
LocalClass instance;

// inner class
private class LocalClass{
public void doit(){
System.out.println("Hello");
}
}

ClassA(){
instance = new LocalClass();
}

public void doit(){
instance.doit();
}
}

public class ClassB extends ClassA{
LocalClass instance;

// inner class
private class LocalClass{
public void doit(){
System.out.println("Goodbye");
}
}
}

public class Main
{
public static void main(String[] args){
ClassA ca;
ClassB cb;

ca = new ClassA();
cb = new ClassB();

ca.doit();
cb.doit();
}
}

1. There is no such thing like overriding a class, so I don't know
what do you mean by that.
2. You haven't constructed anywhere in your code an instance of
ClassB.LocalClass,
so dont count on executing any code from that class.
3. In you code, ClassB has
a- definition of an inner class ClassA.LocalClass (derived from
ClassA, but invisible - marked private)
b- definition of an inner class ClassB.LocalClass (unrelated to above)
c- reference of type ClassA.LocalClass called 'instance', derived from
ClassA, invisible (private in superclass),
referencing a ClassA.LocalClass object created in ClassA constructor
d- reference of type ClassB.LocalClass, pointing to null
That is really messy, and proves nothing.
Calling cb.doit() calls doit() on reference mentioned in point c,
and Hello is printed.

Anyway, I think what you mean is:
/// InnerOverride.java
class Base{
protected class InnerBase{
void doit(){
System.out.println("Hello");
}
}

void doit(){
getInner().doit();
}
InnerBase getInner(){
return new InnerBase();
}
}

class Derived extends Base{
protected class InnerDerived extends InnerBase {
void doit() {
System.out.println("GoodBye");
}
}
InnerBase getInner(){
return new InnerDerived();
}

}

public class InnerOverride{
public static void main(String[] args) {
Base one = new Base();
Base two = new Derived();
one.doit();
two.doit();
}
}
///end of code
Adam
 
T

Tom

Hi Mike,

Thanks for your reply. If i change ClassB's code to

public class ClassB extends ClassA{
LocalClass instance;

// inner class -- should override inner class of ClassA
private class LocalClass{
public void doit(){
System.out.println("Goodbye");
}
}

ClassB(){
instance = new LocalClass();
}

public void doit(){
instance.doit();
}
}

and leave the rest unaltered i get the desired behavior:
$ java Main
Hello
Goodbye

but that was not the point, i just wondered why ClassB.LocalClass
can't override ClassA.LocalClass. Why should different names prevent
overriding? Overriding methods and member variables in subclasses also
have different names, or not?? I suspect that the problem is that they
have different types, but then again, they are both Objects...

Tom
 
P

P.Hill

Tom said:
Hi Mike,

Thanks for your reply. If i change ClassB's code to

public class ClassB extends ClassA{
LocalClass instance;

// inner class -- should override inner class of ClassA
private class LocalClass{

This is still wrong. Subclassing ALWAYS requires you to
specify what class you are subclassing, consider TWO
inner class, how would the compiler know that one should
be a subclass and the other shouldn't. It can't guess.
You are missing something like
private class LocalClass extends ClassA.LocalClass {
.....
and leave the rest unaltered i get the desired behavior:
$ java Main
Hello
Goodbye

Not without the various points made by Adam in this thread.

Consider his comment:
2. You haven't constructed anywhere in your code an instance of
ClassB.LocalClass,

Also consider that you have defined _2_ variables called instance, not
one.

I think you need to work on basic subclassing before you start subclassing
inner classes.

-Paul
 
Y

Yakov

Overriding inner classes is a bit too much unless your goal is to make
your code as convoluted as possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top