How should I construct a subclass using an instance of the superclass?
Asked Answered
C

5

7

Let's say I have the following code

public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }
}

public class Subclass extends SuperClass {
    protected int subclass_value;

    public SubClass(int super_class_value, int subclass_value) {
        super(super_class_value);
        this.subclass_value = subclass_value;
    }
}

However, now I want to be able to pass a SuperClass object into the SubClass constructor. How would I do that?

public SubClass(SuperClass super_class, int subclass_value) {
    //What do I do here?
    this.subclass_value = subclass_value;
}

Basically, I'd like to do something like this...

public SubClass(SuperClass super_calss, int subclass_value) {
    super(super_class.super_class_value);
    this.subclass_value = subclass_value;
}

But if SuperClass is more complex, I don't want to add each value to the super() call. Instead, I'd like to simply pass in an object that already exists, and use it as the super class.

I want to do this...

public SubClass(SuperClass super_class, int subclass_value) {
    super(super_class);
    this.subclass_value = subclass_value;
}

But I'm not sure if that's allowed?


Seems as though I can do the above, if I add a constructor to the SuperClass definition. But what would that look like?

public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }

    public SuperClass (SuperClass super_class_object) {
        //What is done here?
    }
}
Chlorous answered 11/2, 2015 at 19:58 Comment(5)
Currently your code looks fine... What exactly do you want to do with the super_class reference??Nerve
can you not pass it in subclass like you did super(super_class_value);?Causerie
Why do you want to pass the super class object?Sporran
Are you asking how you would pass in the int to the super call when you only have an instance of the SuperClass instead of being passed an int for the super class?Solana
In reply to your current update, I'd like to simply pass in an object that already exists, and use it as the super class. well that is not really possible. What you could do is make a constructor in your super class that makes a copy of itself using the passed in reference.Nerve
T
3

Don't you just want to have a copy constructor for your Superclass?

public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }
    protected SuperClass (SuperClass super_class) {
        this.super_class_value = super_class.supper_class_value;
    }
}

public class Subclass extends SuperClass {
    protected int subclass_value;

    public SubClass(SuperClass super_class, int subclass_value) {
        super(super_class);
        this.subclass_value = subclass_value;
    }
}

See also a question here on SO about writing copy ctors:

Does Java have a default copy constructor (like in C++)?

Telegony answered 11/2, 2015 at 20:18 Comment(2)
Yes, but super_class might be fairly complex, and I don't want to copy over every value manually.Chlorous
@Tester101: Ah, but you aren't! At least, not in SubClass. SubClass just passes super_class along. As for SuperClass itself, if its construction is complicated, you might use constructor chaining.Telegony
S
2

However, now I want to be able to pass a SuperClass object into the SubClass constructor. How would I do that?

You are perfectly doing the same

public SubClass(SuperClass super_class, int subclass_value) {
    //What do I do here?
    // Do Whatever you want to do here ,
    this.subclass_value = subclas_value;
}

Now i think you might be thinking of how to pass Object to SuperClass from subclass then this can be done by super keyword in Java

  • super() is used to invoke immediate parent class constructor.

Now for passing the Object to SuperClass , in your Subclass's constructor you very first line must be

super(super_class);

Now In your super class you must have a constructor which takes Object of itself otherwise the above line will throw compile time error

public SuperClass (SuperClass object) {
         // Do whatever you want to do .
          this.anyInstanceVariable=object.thatInstanceVariable;
    }

P.S. : I still don't understand what did you want to achieve with this , but this is the way you'll achieve it whatever you have mentioned


Update

As you said in comments you don't want to copy each single variable then you can use clone method of Object class

Here is an example how you will achieve it in your case

class SuperClass implements Cloneable{ // otherwise clone() method will throw CloneNotSupportedException

    int a;
    SuperClass obj=null; // this will point to the copy of the cloned object

    SuperClass(int a){// just to test that cloning is successfull
        this.a=a;
    }
    SuperClass(SuperClass a){
        try{
        this.obj=(SuperClass)a.clone();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        display();
    }
    public void display(){// to Verify that Cloning has been done successfully
        try{
        System.out.println("Hello from cloned Object "+obj.a);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    @Override
    protected Object clone() throws CloneNotSupportedException  { // Overridden Method
        return super.clone();
    }
}

Check out this nice Article about Object cloning in java

Serenata answered 11/2, 2015 at 20:11 Comment(5)
What would the body of the constructor in SuperClass look like? Where "// Do whatever you want to do ." is, what goes there?Chlorous
There you can write any valid java statement , but i think the task you want to perform there is copy constructor's workSerenata
Let's say SuperClass is more complex, and I don't want to copy over each variable. Is there a way to simply "copy" the whole object?Chlorous
I think you want to create a clone of objectSerenata
@Chlorous , Check out the updated part Hope it will help you and might be this is what you wantedSerenata
S
0

You could add getter methods inside of SuperClass and use them in the SubClass constructor.

public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }

    public int getSuperClassValue(){ return super_class_value;}
}

public class Subclass extends SuperClass {
    protected int subclass_value;

    public SubClass(SuperClass super_class, int subclass_value) {
        super(super_class.getSuperClassValue());
        this.subclass_value = subclass_value;
    }
}
Solana answered 11/2, 2015 at 20:1 Comment(2)
I want to avoid this, as the super_class might be very complexChlorous
You're going to have to set the data somewhere, whether inside of SuperClass by adding an additional constructor to SuperClass or in SubClass by something similar to what I wrote here.Solana
N
0

Check out the Decorator Pattern for a well-known example of this. The page includes a good java example with UML diagram too.

Northwesterly answered 11/2, 2015 at 20:8 Comment(0)
C
0
public class SuperClass {
    protected int super_class_value;

    public SuperClass (int value) {
        this.super_class_value = value;
    }

    Object obj = new Object();
}

public class Subclass extends SuperClass {
    protected int subclass_value;

    private SuperClass superCl = new SuperClass();

    public SubClass(int super_class_value, Object obj2 ,int subclass_value) {
        super(super_class_value);
        obj2 = superCl.obj;
        this.subclass_value = subclass_value;
    }
}
Causerie answered 11/2, 2015 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.