Reputation: 3272
I've done a bit of searching, but I'm either not asking the right question or not remembering correctly. In any case, in Java, I am wondering if it is possible to pass the super class object in as a parameter to a subclass and what the most efficient way of making that object's data available to the class's super class.
Code examples:
public class superclass {
String myparm1;
String myParm2;
int myParmN;
public superclass(String p1, String p2, int pn)
{
this.myparm1 = p1;
this.myparm2 = p2;
this.myParmN = pn;
}
// other methods here
}
public class subclass extends superclass {
double b1;
double b2;
public subclass(superclass sc, double b1, double b2) {
// easy way to make sc data available to this class?
// Do I create a copy or clone method, or some other way?
// calling super(sc); wouldn't exactly work
this.b1 = b1;
this.b2 = b2;
}
}
if I had a constructor in superclass that was public superclass(superclass sc) { // assign sc properties to this properties, correct? }
then I could simply use super(sc);
Upvotes: 7
Views: 25462
Reputation: 512
There's no point to passing in a reference to the superclass of an object in the constructor. Your subclass is already an instance of the superclass.
Even though you can't directly see the private components of the superclass, but they still exist and calls to public accessor methods will still produce normal behavior.
In answer to your second question, the most efficient way to access the data inside the parent class is with the accessor methods of that parent class. If it has get/set properties methods that populate some data structure full of properties, just call those methods from your child class and they'll work exactly the same as they did for the parent. Now, if those internal data structures are populated by the constructor of the parent class, you'll have to invoke that constructor with the correct methods when you create an instance of the child constructor that needs them- typically by calling the appropriate super() at the beginning of the child's constructor.
If you're trying to get around the restriction that you can't see the private parts of the superclass, java intentionally doesn't let you do that. You can get around this with reflection unless you're stuck inside an execution environment that disallows this, but I generally wouldn't consider this a safe or elegant approach.
From comment below, I understand what the OP is trying to do and this should work, though obviously it depends upon your ability to make changes to the super class:
public class Super
{
public Super (Super other)
{
//copy stuff from other to this
}
}
public class Child extends Super
{
public Child (Super other)
{
super(other);
//continue constructor
}
}
Upvotes: 7
Reputation: 3272
To answer a previous question, suppose the superclass has large number of properties. In that case, the design of the class may be bad, of course.
Perhaps the best answer is:
public class superclass {
// properties and constructors as defined in OP
public void copy(superclass sc) {
this.myParm1 = sc.getMyParm1();
this.myParm2 = sc.getMyParm2();
this.myParmN = sc.getMyParmN();
}
// other methods as needed
}
That way, the sub class can just call the super.copy(sc)
. Of course, I would need another constructor in superclass that will set defaults: public superclass() { // set defaults }
So subclass could be:
public class subclass extends superclass {
//properties as defined in OP
public subclass(superclass sc, double b1, double b2) {
this.b1 = b1;
this.b2 = b2;
super.copy(sc);
}
}
In this way, I'm only having to type those parameters out, and any subclasses that would want to accept a superclass object won't have to define that structure each and every time. (less typing, less chance for mistake or forgetting something.)
Upvotes: 0
Reputation: 20875
You can't. When you build an object with Java (for example with new
), that instance has a class, and that class has a parent (possibly Object).
The parent-child relation only holds between classes, not between objects! No object has a parent object (at least, not at the language level) - so you can't accept the parent in the contructor and store it in some Java-defined place.
However your domain can have parent and child entities, in which case you need fields or data structures to store the links between them.
Upvotes: 2