Charles Cai
Charles Cai

Reputation: 2791

I have a problem at child class about super()

This is my child class:

public class User extends Alien
{

protected XYCoordination currentLocation;
protected int energyCanister;
protected int lifePoints;

public User (XYCoordination currentLocation, int energyCanister, int lifePoints)
{
    super(currentLocation);
}
public int collectCanister()
{
    super.collectCanister();
    return energyCanister;
}
public int caculateLifePoints(int lifePoints)
{
    super.caculateLifePoints(lifePoints);
    return lifePoints;
}
}

This is my parent class:

public class Alien
{
protected XYCoordination currentLocation;
protected Planet currentPlanet;
protected int energyCanister;
protected int lifePoints;
protected int n;

public Alien(XYCoordination currentLocation, int energyCanister, int lifePoints)
{
    this.currentLocation = currentLocation;
    this.energyCanister = energyCanister;
    this.lifePoints = lifePoints;
}

public XYCoordination moveRight(XYCoordination toRight)
{
    currentLocation = currentLocation.addXToRight(toRight);
    return currentLocation;
}
public XYCoordination moveUp(XYCoordination toUp)
{
    currentLocation = currentLocation.addYToUp(toUp);
    return currentLocation;
}
public XYCoordination moveLeft(XYCoordination toLeft)
{
    currentLocation = currentLocation.addXToLeft(toLeft);
    return currentLocation;
}
public XYCoordination moveDown(XYCoordination toDown)
{
    currentLocation = currentLocation.addYToDown(toDown);
    return currentLocation;
}
public int collectCanister()
{
    energyCanister = energyCanister + (int)(n*currentPlanet.getRemainingCanister());
    return energyCanister;
}
public int caculateLifePoints(int lifePoints)
{
    lifePoints = (int)((2/3)*lifePoints);
    return lifePoints;
}
public void displayInfo()
{
    System.out.print("Currently you are in:" + currentLocation + "\t Currently you have" + energyCanister + "canisters" + "\tCurrently you have" + lifePoints + "lifepoints");
}
}

It is always says "can't find the symble 'super'."

Upvotes: 0

Views: 209

Answers (2)

Nivas
Nivas

Reputation: 18334

When you say
super(currentLocation); You call the super class (parent class) constructor with the single argument currentLocation. But your super class Alien does not have a constructor that accepts a single argument currentLocation.

Cannot find symbol super in this context means: You are trying to call a super class constructor with a single parameter, but I am unable to find a constructor in the super class with a single currentLocation argument.

You probably wanted to call super(currentLocation, energyCanister, lifePoints)

Upvotes: 0

Steve B.
Steve B.

Reputation: 57284

Your superclass needs to have a constructor that matches the signature you're calling. Since you have

public User (XYCoordination currentLocation, ....) {
    super(currentLocation);
}

the Alien class you're extending needs to have a constructor with a matching signature-

public Alien(YCoordination currentLocation)

Upvotes: 4

Related Questions