Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: When I Instantiate a Subclass of an Abstract Class It Doesn't Recognize the Constructor of its Superclass
    primarykey
    data
    text
    <p>I do not have very much Java experience but I see codes where there is an abstract class with a certain constructor and then a subclass of that abstract class without a constructor. Then when the subclass is instantiated it is constructed with its superclass constructor. Is that right?</p> <p>I have this abstract class:</p> <pre><code>public abstract class Tile{ public int x; public int y; public int z; protected Color color; protected float friction; protected float bounce; protected boolean liquid; public void Tile(int x, int y, int z){ this.x = x; this.y = y; this.z = z; init(); } abstract protected void init(); </code></pre> <p>And this subclass:</p> <pre><code>public class TestTile extends Tile{ protected void init(){ color = Color.RED; friction = 0.1f; bounce = 0.2f; liquid = false; } } </code></pre> <p>But when I instantiate a TestTile with this:</p> <pre><code>Tile tile = new TestTile(0, 0, 0); </code></pre> <p>the init() method never runs. All of the values defined inside it are null. I tried making what I though might be a redundant constructor in the subclass which just called super with the exact same parameters, but when I did that, even with super(x, y, z) the only statement inside it, it said this:</p> <blockquote> <p>TestTile.java:27: call to super must be first statement in constructor</p> </blockquote> <p>I want to make a bunch of subclasses of Tile which implement the properties of a Tile. If this is not the correct way to do that, what is a better way?</p> <p>I am using 32-bit Ubuntu Linux 11.04 if it has to do with anything.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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