d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Abstract Class
Prev12
Add Reply New Topic New Poll
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 7 2012 12:00pm
Yeah that doesn't work. Reread the above.
Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Nov 7 2012 12:15pm
Quote (irimi @ Nov 7 2012 01:00pm)
Yeah that doesn't work.  Reread the above.


Instead of Dictionary, I meant TreeSet<String> as a parameter in the Player constructor.

Found a solution, removed the constructor from the parent.

This post was edited by Cattotonic on Nov 7 2012 12:18pm
Member
Posts: 9
Joined: Nov 7 2012
Gold: 0.00
Nov 7 2012 09:29pm
Quote (Cattotonic @ Nov 7 2012 11:56am)
I'm getting the error: Implicit super constructor Player() is undefined. Must explicitly invoke another constructor.

I'm trying to use the following constructors in the child classes:

Code
public Computer(TreeSet<String> t) {}

public Human(TreeSet<String> t) {}


While the parent (Player) class has the following constructor:
Code
public Player(Dictionary d) {}


adding the code "super(t)" as the fist line in the construct should fix your issue. This tells the jvm you want to pass the object through the constructor defined in the parent class.

Code
final abstract class Player {

protected final Dictionary dictionary;

Player(Dictionary<?> dictionary){
this.dictionary = dictionary;
}

/**getter*/
}

Code
class HumanPlayer extends Player {

HumanPlayer(TreeSet<?> dictionary) {
super(dictionary);
}
}

This is basically what you would do, but the most optimal way(depending on how you plan to use the code) would be to simply have something like the following:

Code
final abstract class Player {

public abstract Dictionary<?> getDictionary();

}


This way you can simple inherit the method in the child class and return the dictionary object of your choce.

all this was written in this chatbox so there is no formatting etc..

If you want to get even more advanced you can get into generics but it doesn't seem you're there yet.

This post was edited by Saxic on Nov 7 2012 09:37pm
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll