Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
    }
}
like image 422
Tester101 Avatar asked Feb 11 '15 19:02

Tester101


People also ask

Can a superclass be an instance of a subclass?

It is possible to reference a subclass as an instance of one of its superclasses. For instance, using the class definitions from the example in the previous section it is possible to reference an instance of the Car class as an instance of the Vehicle class.

How do you define a subclass from a superclass?

Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.

How do you write a subclass superclass?

You declare that a class is the subclass of another class within The Class Declaration. For example, suppose that you wanted to create a subclass named SubClass of another class named SuperClass. You would write: class SubClass extends SuperClass { . . . }


1 Answers

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

like image 98
einpoklum Avatar answered Sep 27 '22 23:09

einpoklum