Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling abstract methods in a superclass constructor
    primarykey
    data
    text
    <p>I have an abstract method call in my super's constructor. I run into the error "Constructor call must be the first statement in the Constructor." because I need to initialize some state in my subclasses before calling the super's constructor.</p> <p>I understand that the constructor call must be first.. but it's making me have some problems.</p> <p>I have an abstract method in my superclass that all subclasses implements theirselves. But the subclass constructor requires arguments that must be processed before the runs the abstract method.</p> <p>In my code, I have the abstract method in the SuperClass constructor, so you will understand of course the problem: Super class does:</p> <ol> <li>Get info1 and info2 using super(i1, i2)</li> <li>Performs abstract method through the subclass</li> </ol> <p>But little did the Superclass constructor know that it also needed info3 and info4, which are declared on the lines beneath the super()-line.</p> <p>I've been trying to think of ways for the Superclass constructor to have to go back and gather the information from the subclass before it executes the method, but I haven't thought of anything.</p> <p>Also the method cannot require arguments, as the arguments are different for all the subclasses's abstract method.</p> <p>Those of you who are experienced, how do I get around this?</p> <p>After some Googling, it seems to have to do with this: <a href="http://webcache.googleusercontent.co...s-constructor/" rel="nofollow">http://webcache.googleusercontent.co...s-constructor/</a></p> <p>But is still a novice, so find it hard to hang on to .. I feel I could've avoided all of this trouble if I could just use the super() after the subclasses constructors have been processed.</p> <p>Code on request:</p> <pre><code> abstract public class Prylar { abstract public Integer value(); private String itemName; private Person owner; public Prylar(Person thisOwner, String iN){ owner = thisOwner; itemName = iN; value = value(); } public class Smycken extends Prylar{ private int antalStenar; private boolean guldEllerSilver; private int value; public Smycken (Person who, String n, int aS, boolean material){ super(who,n); antalStenar = aS; guldEllerSilver = material; } public Integer value() { if (guldEllerSilver){ value = 2000; } else{ value= 700;} value += (500*antalStenar); return value; } </code></pre> <p>}</p> <p>And I just want to end this by saying thank you all for taking your time to read and help a guy out. I really appreciate it. Hopefully when someone else has a similar problem, they will stumble upon this.</p> <p>Thanks guys!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload