This site requires JavaScript, please enable it in your browser!
Greenfoot back
DogeOverlord
DogeOverlord wrote ...

2015/9/30

Subclasses and passing variables

DogeOverlord DogeOverlord

2015/9/30

#
So I have this actor class called PathMaker, and here is it's constuctor:
public PathMaker(int face, Labyrinth wrld)
    {
        setRotation(face);
        world = wrld;
    }
and I am trying to give it a subclass, PathFollower
public PathFollower(int face, Labyrinth wrld)
    {
        setRotation(face);
        world = wrld;
    }
And I try to compile and it highlights the first { of PathFollower and says "Constructor PathMaker in class PathMaker cannot be applied to given types; required:int,Labyrinth; found: no arguments. Pathmaker has some methods I want to use in PathFollower.
danpost danpost

2015/9/30

#
One main issue is that a PathFollower object is not a PathMaker object -- have both classes extends the same class (the Actor class, presumably). The error message says you are trying to create a PathMaker object without passing the appropriate values to the constructor. You are probably using
PathMaker pathMaker = new PathMaker();
where the constructor, as written (line 1 of the second snippet in your post), requires something like this:
PathMaker pathMaker = new PathMaker(getRotation(), world);
I am not saying that this last line is what you actually need -- just that, as is, it probably would compile with that line.
davmac davmac

2015/10/1

#
The error in the constructor of PathFollower is because you extend PathMaker but do not pass arguments to its constructor. You could change the constructor of PathFollower above to:
public PathFollower(int face, Labyrinth wrld)
    {
        super(face, wrld);
        // setRotation(face); // don't need this line - it's done by the PathMaker constructor
        world = wrld;   // have you a private 'world' variable in both classes? otherwise you
            // can remove this line too
    }
As DanPost says above, however, it might be better not to make PathFollower a subclass of PathMaker.
DogeOverlord DogeOverlord

2015/10/1

#
Yea that's what I ended up doing, but thanks anyway.
You need to login to post a reply.