Passing a superclass object as a parameter to a subclass constructor (java)

I did a little work, but I either don’t ask the right question, or I don’t remember correctly. In any case, in Java, I wonder if it is possible to pass an object of a superclass as a parameter to a subclass and what is the most efficient way to make this object data available to the superclass of the 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 a superclass that was public superclass(superclass sc) { // assign sc properties to this properties, correct? } public superclass(superclass sc) { // assign sc properties to this properties, correct? } , then I could just use super(sc);

+7
java inheritance
source share
3 answers

It makes no sense to pass a reference to the superclass of the object in the constructor. Your subclass is already an instance of the superclass.

Even if you cannot directly see the private components of the superclass, but they still exist, and calls to public access methods will still cause normal behavior.

In answer to your second question, the most efficient way to access data inside a parent class is to access this parent class. If it has get / set properties methods that populate some data structure full of properties, just call these methods from your child class and they will work exactly the same as for the parent. Now, if these internal data structures are populated with the constructor of the parent class, you will need to call this constructor with the correct methods when creating the instance of the child constructor that they need, usually by calling the corresponding super () at the beginning of the child constructor.

If you are trying to get around a restriction that you do not see in the private parts of the superclass, java intentionally does not allow you to do this. You can get around this with a reflection if you are not stuck inside a runtime environment that prohibits this, but I generally do not consider this a safe or elegant approach.

From the comment below, I understand what the OP is trying to do, and this should work, although obviously it depends on your ability to make changes to the superclass:

 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 } } 
+4
source share

You can not. When you create an object with Java (for example, with new ), this instance has a class, and this class has a parent object (possibly Object).

The parent-child relationship only runs between classes, not between objects! No object has a parent object (at least not at the language level) - therefore, you cannot accept a parent in the constructor and save it in a specific Java location.

However, your domain may have parent and child entities, in which case you will need fields or data structures to store the relationships between them.

+2
source share

To answer the previous question, suppose that a superclass has a large number of properties. In this case, the class design 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 } 

Thus, a subclass can simply call super.copy(sc) . Of course, I will need another constructor in the superclass that will set the default values: public superclass() { // set defaults }

Such a subclass may 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); } } 

Thus, I only need to print these parameters, and any subclasses that would like to accept the superclass object will not have to define this structure every time. (less typing, less chance of making a mistake, or forgetting something.)

0
source share

All Articles