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?
}
}
super_class
reference?? – Nervesuper(super_class_value);
? – Causerieint
to the super call when you only have an instance of theSuperClass
instead of being passed anint
for the super class? – Solana