lwjgl - Java - How to properly use Extends/Implements -


okay kind of confused uses of "extends" , "implements". let me give scenario, let's making simple tower defense game. understanding if made class (towerbase.java), can fill variables , methods, such as; int hp, int damage, int attack(){ ... }, etc.... figured make many types of towers, lets 1 archer tower, , inside towerarcher.java want set towerbase's variables unique values said tower. understanding inside of towerarcher if extend towerbase have access of towerbase's variables , methods since towerbase parent. tried this:

public class towerbase {     //health (range: 1-100)     int hp;      public int attack(int blah){         //blah blah how tower goes attacking foe     } 

then in towerarcher.java

public class towerarcher extends towerbase{     //set towerarcher's unique values     hp = 10; } 

now somewhere in game...

towerarcher archer; archer.attack(blah); 

eclipse throws syntax error in towerarcher.java @ "hp = 10;": varabledeclaratorid expected after token. presume saying doesn't know hp is. confuses me since thought know hp since it's in parent class. have found work-arounds don't seem effective , if work still leave me confused extends , implements for. trying find out here is;
1: extends
2: how use extends in program intuitively
3: implements
4: how use implements in program intuitively
5: should use in scenario have many different objects (towers) doing same thing, perhaps towerarcher towerbunker towersnipernest towerbarricade etc.

know asking lot couldn't find post on others :)
p.s. if missing needed information please let me know. clueless @ point.

your problem has nothing differences between extends , implements. seem have okay understanding of how inheritance works, or @ least you've said isn't wrong. you're messing syntax. need set hp in constructor:

public class towerarcher extends towerbase {     // can't set hp here; syntax doesn't work way.     // hp = 10;      public towerarcher() {         // initialize here, instead.         // runs when construct towerarcher.         hp = 10;     } } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -