0

I want to pass an instance of a super class to a constructor of a sub class. My first idea was to swap the instance of the super class in the sub class similar to javascripts prototypes, but I was told here that Java does not support swapping the reference of the super instance because there is no super instance per se.

To circumvent this issue I want to use a copy constructur which accepts a super class instance. Then I will have to relink all references manually which on the long run will invite bugs when other people extend the code of the super class and forget the copy constructur in the sub class.

Question: Is there some neat and nice way to copy all references automatically, maybe with some reflection mechanism to prevent future bugs?

4
  • I am not sure if I understand what your are trying to do here. But will using the super in the subclass will give you access to the super class. public class A{ public void test(){ } } public class B extends A{ public B(){ super.test(); } } Commented Dec 29, 2011 at 17:13
  • An ugly option that works without a constructor is clone(). go4expert.com/forums/showthread.php?t=5424 Commented Dec 29, 2011 at 17:38
  • Is having a copy constructor in the super class, and calling this copy constructor from the copy constructor of your subclass not what you are looking for ? Or do I not fully understand your problem ? Perhaps the question is more clear if you can describe what problem you are trying to solve Commented Dec 29, 2011 at 20:04
  • Yeah having the copy constructor directly in the super class and calling it from the sub class is easier. But I think I will refactor tomorrow and see if a delegating interface solution can't do the trick. Copy constructur or clone seem too messy. I was really exspecting some neet reflection trick. Commented Dec 30, 2011 at 0:32

1 Answer 1

1

You shouldn't copy all the references from the superclass instance to the subclass instance. BTW, all these references should not even be accessible from the subclass, if they are declared as private fields in the superclass (as they should be, generally).

What you probably need is delegation, instead of inheritance:

// Foo is the superclass
private class FooAdapter {
    private Foo foo;

    public FooAdapter(Foo foo) {
        this.foo = foo;
    }

    public void doThis() {
        return foo.doThis();
    }
}

FooAdapter could extend Foo or (better) theyr should implement a common interface, but that's not necessarily needed.

If this doesn't answer your problem, please tell us what you want to do, instead of telling us how you want to do it. What's the problem you want to solve?

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah you get my problem, but I don't want to delegate because the classes are otherwise a clean inheritance. That would just be tonnes of extra code with one line functions delegating to the other instance. I really only want to extract a common interface if the "broken" clone is my only option. But I will review the code, maybe a clean interface will make some access patterns obsolete and compile down to a neat interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.