I have an abstract class, Entity.java. I also have a class TransportUnit.java that extends the Entity class. Entities are to be constructed with a String name and int lifePoints. Transport Units are to be constructed with the same and in addition an empty Set. My question is how to write the constructor for the TransportUnit class and why? Thanks in advance.
Asked
Active
Viewed 69 times
1 Answers
2
You would write it more or less the same way as a normal constructor, just calling super()
Set s;
public TransportUnit(String name, int lifePoints){
super(name, lifePoints);
s = new HashSet(); //or other type of set
}

Chris Thompson
- 35,167
- 12
- 80
- 109
-
Thanks for the reply. This sort of makes sense to me and I can see that it will work. However, it seems a little strange to construct a TransportUnit with a specific Set, s, only to then call 's.clear();'. So every TransportUnit will start with the same empty set so why do we have to construct each one with a Set. Is there a way around this? Thanks. – j x Apr 24 '14 at 14:24
-
1If you don't need an instance specific set, then just provide a 2 arg constructor and instantiate an empty set in the body. No need for a 3rd argument – Jeremy Apr 24 '14 at 14:35
-
@Jimmy Ah, that was my mistake, I didn't fully understand the specification. Check out my edit above. – Chris Thompson Apr 24 '14 at 15:01
-
OK, that makes great sense thank you both for your help. – j x Apr 24 '14 at 15:22