1

I have one super class, which called game that. It looks like this:

import java.util.ArrayList;

public class Game {
    private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
    private ArrayList<Tower> towers = new ArrayList<Tower>();
    private int corridorLength;
    private int currentPosition = 0;
    public Game(int corridorLength){
        this.corridorLength = corridorLength;
    }

    public void addTower(int damage,int timeStep){
        this.towers.add(new Tower(damage,timeStep)); // Add tower with 
current position corrdor length

    }
    public void addEnemy(int health, int speed){
        this.enemies.add(new Enemy(health,speed));
    }
    public void advance(){
        this.currentPosition = this.currentPosition + 1;
        if(this.currentPosition == this.corridorLength){
            System.out.println("Game Over");
        }
    }
    public void printDamage(){
        System.out.println(this.towers.get(this.currentPosition));
    }

}

The main focus is on the public void addTower(int, int) So, I have a subclass called Tower:

public class Tower extends Game {

   public Tower(int damage, int timeStep){
       super.addTower(damage,timeStep);
   }
   public void getDamage(){
       super.printDamage();
   }
}

And subclass of the Tower subclass called Catapult:

public class Catapult extends Tower {
    public Catapult(){
        super(5,3);
    }

}

I am new to Java and can't see what am I doing wrong here. Why do I need a default constructor for the Tower in the Game?

  • you need to have an explicit default constructor if you have a `public Game(int corridorLength)` parameterized one – Suraj Rao Oct 07 '17 at 13:08
  • possible duplicate: https://stackoverflow.com/questions/1197634/java-error-implicit-super-constructor-is-undefined-for-default-constructor – Suraj Rao Oct 07 '17 at 13:10
  • The Tower constructor implicitly calls the Game default constructor, which doesn't exist. But does Tower really need to extend Game? I don't see any need. Can't it stand on its own? – DodgyCodeException Oct 07 '17 at 13:15

1 Answers1

1

You need to explicitly declare default constructor in Game class.

public Game (){} 

Since, Object instantiation chained to Object class during that, it will call its super class constructor. You have explicitly declared arg-constructor in Game, so default constructor won't be added automatically.

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • I have added a default constructor, but now, I get an error **Exception in thread "main" java.lang.StackOverflowError**. It says due to the line in **super.addTower(int,int)** in the Tower and **this.towers.add...** in the Game class –  Oct 07 '17 at 13:41
  • @AbylIkhsanov because you have doing it wrong. If you carefully look at your code. Then you will see when you invoke super.addTower. It will go into Infinite loop. And that is the reason you are getting that error – Ravi Oct 07 '17 at 13:45
  • Are you saying that, when I call super.addTower, this method in the Game class calls again the Tower(int, int) constructor, and it goes forever as they keep calling each other? –  Oct 07 '17 at 13:54
  • @AbylIkhsanov Correct. – Ravi Oct 07 '17 at 13:54