miniwolf
miniwolf

Reputation: 339

Java inheritance from superclass fields

I have made these two classes in Java, and I need to define my structure like this:

class UnitType {
    Position myPos;
    public Position getPosition() {
        return myPos;
    }
}
class Unit extends UnitType {
    Position myPos;
    public Unit(Position myPos) {
        this.myPos = myPos;
    }
}

My question, can I do this following:

class UnitTest {
    private Unit testUnit;
    @Before
    public void setUp() {
        testUnit = new Unit(new Position(1,0));
    }
    @Test
    public void testPositionUnit() {
        assertEquals("Unit should be placed at (1,0)", new Position(1,0), testUnit.getPosition());
    }
}

And if I can do this, what would happen if I updated the position in my Unit class. Will I be looking at a wrong pointer when calling getPosition()?

Many is wondering about my .equals method for my Position class so here it is: https://pastebin.com/Tfer1kqR

Upvotes: 0

Views: 1222

Answers (3)

Mike Samuel
Mike Samuel

Reputation: 120506

To make sure you're looking at the right position, set the position via the super constructor

class UnitType {
    Position myPos;  // This could be private final.

    // Only needed for backwards compatibility if you do new UnitType().
    UnitType() { this(null); }  // this(...) delegates to another constructor.
    // sets the position field.
    UnitType(Position myPos) { this.myPos = myPos; }

    public void getPosition() {
        return myPos;
    }
}
class Unit extends UnitType {
    // No field definition here that masks the super-class one.
    public Unit(Position myPos) {
        super(myPos);  // Invoke UnitType(Position) to set the field
    }
}

then to get your unit-tests to work, make sure the Position class overrides equals(Object) so that assertEquals will test for equivalence instead of checking that they are the same object.

Upvotes: 1

João Silva
João Silva

Reputation: 91299

If you do so, you'll be hiding the myPos field of the superclass UnitType. For example:

Unit u = new Unit(new Position(1, 0));
System.out.println(u.myPos);         // (1, 0) assuming toString() was overriden
System.out.println(u.getPosition()); // null
UnitType ut = u;
System.out.println(ut.myPos);        // null

Then, even though you may update the myPos field of your Unit class, getPosition() will always return null, since it will return the myPos field from the superclass, which was never set.

Also, if you want to determine if two Position objects are the same, make sure you implement .equals() (and .hashCode()).

Upvotes: 4

Peter Ilfrich
Peter Ilfrich

Reputation: 3816

First of all, you method getPosition() has to return something. In your code it returns nothing, cause you've specified the method with a return type void

Secondly, depending on the Position class you're using (own class?, geometric position?, ...) you probably need to override/write the equals(Object o) method according to what you want to achieve.

In general testUnit.getPosition() and new Position(..) won't return the same object (although they might contain the same values), thus making the assertion false.

Upvotes: 0

Related Questions