Reputation: 31
I've got a class SimpleBoard that is extended by Land, which is then extended by Units. Whenever I try to compile Units, I get this error:
cannot find symbol
symbol : constructor Land()
location: class Land
public Units(int x, int y){
Thoughts on what's wrong?
EDIT: sorry, I was in a hurry! Here's the full code:
public class Units extends Land{
private int attack;
public Units(int x, int y) {
if(x==1){
attack = 1;
}
else if(x==2)
attack = 2;
else if(x==3)
attack = 4;
ar[y].AddUnit(this);
}
public int getAttack(){
return attack;
}
}
and Land
public class Land extends SimpleBoard {
private boolean barrel = false;
private boolean crown = false;
private boolean castle = false;
private boolean stronghold = false;
private int num;
private int array = 0;
public int[] adjacent;
private Units [] Occupy = new Units [4];
public Land(int z, int x, int c, int v, int b, int[] array){
if(z == 1)
barrel = true;
if(x == 1)
crown = true;
if(c == 1)
castle = true;
if (v==1)
stronghold = true;
num = b;
adjacent = array;
}
public void addUnit(Units x){
Occupy[array] = x;
array++;
}
public boolean checkB(){
return barrel;
}
public int getAdj(int i){
return adjacent[i];
}
}
And board
public class SimpleBoard {
public static Land[] ar = new Land[3];
public static void main(String[] args){
Land One = new Land(1,0,0,0,1, new int[]{2, 3});
Land Two = new Land(0,1,0,0,2, new int[]{1, 3});
Land Three = new Land(0,0,1,0,3, new int[]{1, 2});
ar[0] = One;
ar[1] = Two;
ar[2] = Three;
Units Footman = new Units(1, 1);
Units Knight = new Units(2, 3);
Units Siege = new Units(3, 2);
}
}
Upvotes: 0
Views: 312
Reputation: 3456
You can change your Units
and Land
class to this one:
public class Units extends Land {
private int attack;
public Units(int z, int x, int c, int v, int b, int[] array) {
super(z, x, c, v, b, array);
}
public Units(int x, int y) {
if (x == 1) {
attack = 1;
} else if (x == 2) {
attack = 2;
} else if (x == 3) {
attack = 4;
}
ar[y].addUnit(this);
}
public int getAttack() {
return attack;
}
}
public class Land extends SimpleBoard {
// Declared variable
public Land(int x, int y) {
}
// Rest of the code...
}
Because of the inheritance, the constructor that you will state in Units
class must exist in the Land
class too.
Add a constructor in your Land
class called Land(int x, int y)
without any code inside (just a blank constructor) so that it will remove the error in your Units
class. It is not the best practice, since I don't know what are you trying to do. If you in hurry you can try this one, but if you have time please explain in brief what your application purpose.
SimpleBoardGame.java
public class SimpleBoardGame {
private static Land[] ar = new Land[3];
public static Land[] getAr() {
return ar;
}
public static void main(String[] args) {
Land One = new Land(1, 0, 0, 0, 1, new int[]{2, 3});
Land Two = new Land(0, 1, 0, 0, 2, new int[]{1, 3});
Land Three = new Land(0, 0, 1, 0, 3, new int[]{1, 2});
ar[0] = One;
ar[1] = Two;
ar[2] = Three;
// When you pass the parameter for 'y' please make sure it already minus by one.
// If not, will occur the 'java.lang.ArrayIndexOutOfBoundsException'
Unit Footman = new Unit(1, 0);
Unit Knight = new Unit(2, 2);
Unit Siege = new Unit(3, 1);
}
}
Land.java
public class Land {
// For boolean variable, no need to set the value as "false" since it default is "false".
private boolean barrel;
private boolean crown;
private boolean castle;
private boolean stronghold;
private int num;
private int array = 0;
public int[] adjacent;
private Unit[] Occupy = new Unit[4];
public Land(int x, int y) {
// Empty constructor...
}
public Land(int z, int x, int c, int v, int b, int[] array) {
if (z == 1) {
barrel = true;
}
if (x == 1) {
crown = true;
}
if (c == 1) {
castle = true;
}
if (v == 1) {
stronghold = true;
}
num = b;
adjacent = array;
}
public void addUnit(Unit x) {
Occupy[array] = x;
array++;
}
public boolean checkB() {
return barrel;
}
public int getAdj(int i) {
return adjacent[i];
}
}
Unit.java (Changed from Units.java
to Unit.java
)
public class Unit extends Land {
private int attack;
public Unit(int z, int x, int c, int v, int b, int[] array) {
super(z, x, c, v, b, array);
}
public Unit(int x, int y) {
super(x, y);
if (x == 1) {
attack = 1;
} else if (x == 2) {
attack = 2;
} else if (x == 3) {
attack = 4;
}
addUnit(y);
}
/**
* Overload method of the addUnit() of Land class.
* Better not use "this" inside the constructor.
*
* @param y
*/
public final void addUnit(int y) {
SimpleBoardGame.getAr()[y].addUnit(this);
}
public int getAttack() {
return attack;
}
}
Note: Please make sure you don't inherit the SimpleBoardGame
as the main class in Land
class, if you want to access the variable inside the main class just make setter and getter for that. And you can access like this one SimpleBoardGame.getAr()[y].addUnit(this);
(see inside method addUnit(int y)
in Unit
class.
Upvotes: 1
Reputation: 40036
The idea is simple:
Child inherits from Parent. In Child constructor, you need to first construct the "Parent" part of the object by invoking Parent's constructor, then construct your child part.
It is done by a super(...) statement:
class Animal {
String name;
public Animal(String name) {
this.name = name;
}
}
class Human extends Animal {
String familyName;
public Human(String name, String familyName) {
super(name); // HERE!!!! means calling Animal's Animal(String) ctor
this.familyName = familyName;
}
}
If you haven't explicitly added the super(...) statement, compiler will assume you are calling the no-argument constructor of Parent class.
For a class that has defined no constructor, compiler will add a default no-argument constructor for it. However, as your Land has a public Land(int z, int x, int c, int v, int b, int[] array) constructor defined, that means you will not have a no-argument constructor in Land.
In Unit's constructor, there is no super(...) statement, that means it will try to invoke Land's no-arg constructor (which does not exists), which caused the problem
Edit: Though a bit off-topic, it seems to me that your use of inheritance is not making any sense. Why "Land" is-a "SimpleBoard" ? Why "Unit" is-a "Land" ? They seems to be a "has-a" relationship instead of "is-a" to me. Consider redesign your app again or you will face even more strange problems
Upvotes: 0