Question

Okay so I am kind of confused about the uses of "extends" and "implements". Let me give a scenario, let's say I am making a simple tower defense game. So it was my understanding that if I made a class (TowerBase.java), I can fill it with variables and methods, such as; int hp, int damage, int attack(){ ... }, etc.... I figured I could then make many types of towers, lets say one is an archer tower, and inside TowerArcher.java I want to set TowerBase's variables to unique values for said tower. My understanding was that inside of TowerArcher if I extend TowerBase I have access to all of TowerBase's variables and methods since TowerBase is now the parent. So I tried this:

public class TowerBase {
    //Health (Range: 1-100)
    int hp;

    public int attack(int blah){
        //Blah blah how the tower goes about attacking foe
    }

Then in the TowerArcher.java

public class TowerArcher extends TowerBase{
    //Set TowerArcher's unique values
    hp = 10;
}

Now somewhere in the game...

TowerArcher archer;
archer.attack(blah);

Eclipse throws this syntax error in TowerArcher.java at "hp = 10;": VarableDeclaratorId expected after this token. Which I presume that it was saying it doesn't know what hp is. This confuses me since I thought it would know what hp was since it's in the parent class. I have found work-arounds but they don't seem as effective and even if they work they still leave me confused as to what extends and implements are really for. Basically what I am trying to find out here is;
1: What does extends do
2: How could I use extends in my program intuitively
3: What does implements do
4: How could I use implements in my program intuitively
5: What I should use in this scenario where I have many different objects (towers) doing nearly the same thing, perhaps TowerArcher TowerBunker TowerSnipernest TowerBarricade etc.

I know I am asking for a lot but I couldn't find a good post on it so this could help others too :)
P.S. If I am missing any needed information please let me know. I am so clueless at this point.

Was it helpful?

Solution

Your problem has nothing to do with the differences between extends and implements. You seem to have an okay understanding of how inheritance works, or at least what you've said about it isn't wrong. You're just messing up the syntax. You need to set hp in a constructor:

public class TowerArcher extends TowerBase {
    // Can't set hp here; the syntax just doesn't work that way.
    // hp = 10;

    public TowerArcher() {
        // Initialize it here, instead.
        // This runs when you construct a TowerArcher.
        hp = 10;
    }
}

OTHER TIPS

public class TowerArcher extends TowerBase{
//Set TowerArcher's unique values
hp = 10;  //define your data type here..
}

Answers for your questions.

  1. extends is for extending a class or extends interface to another interface. You can extends only one class in java. When A extends B, A inherited B's behavior.

  2. TowerArcher extends TowerBase.

  3. implements is for implementing an interface. you can implements multiple interfaces for one class in java. You have to override all method signatures in interface in your class.

  4. Put abstract type of methods in a interface and implements it to your class.

  5. Create abstract class or interface as base class and put common behaviors there.

In your example code you're trying to set variable value in class body - what's invalid since direct content of class body never gets executed (except field initializations). You have to put your code to some method (for example to constructor).

public class TowerArcher extends TowerBase{
    public TowerArcher() {
        super(); //set values as in TowerBase constructor
        hp = 10; //archers don't wear armor, so they have low HP.
    }
}

extends simply uses form of specified parent class for fields of descendants and serves as base of methods which can, but don't have to be overridden. Fields set as public or protected (but not private) in super class (that's the official name of class which stands after extends keyword) are accessible in extended object.

In your program, you're using extends correctly. New types of towers will bring new classes, which will extends TowerBase.

Note that there can be only one class used as parent by extends keyword.

implements implements interfaces (interfaces, because you can implement more interfaces). In theory base, interfaces are similar to super classes, but they usually hold only one property and they manipulate with it. In your program, you can create IFlameThrowing or IStoneThrowing interfaces which would manage just one property of concrete tower (for example archers can't throw stones, but they can shoot fire arrows).

You have many different towers, so create structure which will divide all the things mentioned to towers, and properties. Then implement it. If you want to allow player to add upgrades to his tower during game (for example to learn his archers how to throw stones), use decorator pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top