d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Abstract Class
12Next
Add Reply New Topic New Poll
Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Nov 6 2012 03:37pm
JAVA

I have a parent ( and now abstract ) class called Player, which maintained a TreeSet of strings and an ArrayList of strings in the previous version of the assignment.

I need to make two classes, Human and Computer, which extend this player class.

The Human player has a list of strings that is typed in by the user, via the terminal, and every word is stored. However, for each word that is stored in the list, there is a % chance that it is also stored in the TreeSet, which will be used by the Computer player actively.

The question is, how do I make the TreeSet known to both the Human and the Computer so that I can add words and access them from the same list?

This post was edited by Cattotonic on Nov 6 2012 03:37pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 6 2012 03:45pm
i assume you declared it as private. change it to protected and your subclasses can access it just fine so should be good to go. alternatively, you can use a getter that's protected.

/edit: wait, so one player (human) and a separate player (computer) both need to access the same field? then you should stick it into the controller, or whatever class is managing both the human and player.

This post was edited by carteblanche on Nov 6 2012 03:47pm
Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Nov 6 2012 04:21pm
Quote (carteblanche @ Nov 6 2012 05:45pm)
i assume you declared it as private. change it to protected and your subclasses can access it just fine so should be good to go. alternatively, you can use a getter that's protected.

/edit: wait, so one player (human) and a separate player (computer) both need to access the same field? then you should stick it into the controller, or whatever class is managing both the human and player.


Okay, I've stuck it under the Player (parent) class and labeled it protected. But my question is would it be instantiated officially since abstract classes can't have constructors? The Computer (child) class will have it instantiated when a Computer is constructed, but not the Human.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 6 2012 04:29pm
Quote (Cattotonic @ Nov 6 2012 06:21pm)
Okay, I've stuck it under the Player (parent) class and labeled it protected. But my question is would it be instantiated officiallysince abstract classes can't have constructors? The Computer (child) class will have it instantiated when a Computer is constructed, but not the Human.


Bolded statement is false.

1. All classes in java have at least one constructor.
2. If you do not specify a constructor, a no args constructor is automatically generated for you.
3. if you do not specify which constructor to chain off from in the first line of your constructor (eg: this(...) or super(...)), then super() is automatically inserted for you, which calls your direct superclass's no-args constructor

You're confused since you cannot instantiate an abstract class. But rest assured, it has a constructor. Your Computer and Human class constructors are both invoking it.

This post was edited by carteblanche on Nov 6 2012 04:33pm
Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Nov 6 2012 05:11pm
Quote (carteblanche @ Nov 6 2012 06:29pm)
Bolded statement is false.

1. All classes in java have at least one constructor.
2. If you do not specify a constructor, a no args constructor is automatically generated for you.
3. if you do not specify which constructor to chain off from in the first line of your constructor (eg: this(...) or super(...)), then super() is automatically inserted for you, which calls your direct superclass's no-args constructor

You're confused since you cannot instantiate an abstract class. But rest assured, it has a constructor. Your Computer and Human class constructors are both invoking it.


Ohhh, thank you for clearing that up! So I can just construct the TreeSet in the parent and make changes from that!
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 6 2012 06:14pm
Don't confuse classes with class instances.


Assuming your code looks something like this --
Code
public class Player {

private TreeSet data;
public Player() {
 data = new TreeSet();
}

public void add(Foo f) {
 data.add(f);
}
}

public class Human extends Player {
}
public class Computer extends Player {
}


And then you call code that looks like this:
Code
Human p1 = new Human();
p1.add(new Foo());
Computer p2 = new Computer();


P1 and P2 will not be using the same TreeSet instance, so p2 will not have the Foo instance that p1 added. They will be using completely different data, even if they call the same methods from the parent class, because they are completely separate instances.

What you probably need is to take a TreeSet as an argument for your constructors, so that you can allocate a single TreeSet instance and pass it into the individual Computer/Human instances -- who will then treat the TreeSet either the same because of the code they share in Player, or differently because of any overridden/abstract methods.

This post was edited by irimi on Nov 6 2012 06:21pm
Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Nov 6 2012 07:21pm
Quote (irimi @ Nov 6 2012 08:14pm)


What you probably need is to take a TreeSet as an argument for your constructors, so that you can allocate a single TreeSet instance and pass it into the individual Computer/Human instances -- who will then treat the TreeSet either the same because of the code they share in Player, or differently because of any overridden/abstract methods.


So if I pass the TreeSet in the constructor of the child classes, they will refer to the same one?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 6 2012 07:26pm
assuming your constructor has the correct code to handle it, yes.
Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Nov 6 2012 07:30pm
Quote (irimi @ Nov 6 2012 09:26pm)
assuming your constructor has the correct code to handle it, yes.


Thanks! :)
Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Nov 7 2012 10: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) {}
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll