6

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?
    }
}
Tester101
  • 8,042
  • 13
  • 55
  • 78
  • Currently your code looks fine... What exactly do you want to do with the `super_class` reference?? – chancea Feb 11 '15 at 20:00
  • can you not pass it in subclass like you did `super(super_class_value);`? – Apurva Feb 11 '15 at 20:02
  • Why do you want to pass the super class object? – Vivin Feb 11 '15 at 20:05
  • 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? – Andrew_CS Feb 11 '15 at 20:05
  • 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. – chancea Feb 11 '15 at 20:12

5 Answers5

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++)?

Community
  • 1
  • 1
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Yes, but super_class might be fairly complex, and I don't want to copy over every value manually. – Tester101 Feb 11 '15 at 20:19
  • 1
    @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](http://stackoverflow.com/questions/17171012/how-to-avoid-constructor-code-redundancy-in-java). – einpoklum Feb 11 '15 at 20:22
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

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
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;
    }
}
Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
  • I want to avoid this, as the super_class might be very complex – Tester101 Feb 11 '15 at 20:18
  • 1
    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. – Andrew_CS Feb 11 '15 at 20:20
0

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

Shane Voisard
  • 1,145
  • 8
  • 12
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;
    }
}
Apurva
  • 7,871
  • 7
  • 40
  • 59