• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList of unknown types of Objects calling methods  RSS feed

 
Jesse Matty
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array list holding unknown object types I declared like and then set equal to other arraylists from the master class LandSquare based on  the enemy number




so, how do I call the class methods on the objects in the arraylist,  each class of enemies has different attack methods and method names?
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesse Matty wrote:I have an array list holding unknown object types I declared like and then set equal to other arraylists from the master class LandSquare based on  the enemy number... so, how do I call the class methods on the objects in the arraylist,  each class of enemies has different attack methods and method names?


If you've got an object which you know is a Hobgoblin and you want to call Hobgoblin methods on it, then you cast it to a Hobgoblin variable and call the appropriate methods on that variable. Since each of your classes has different methods then you're pretty much forced into the casting idea.

Whereas, if the various classes all had the same methods, only they did different things for the different classes, then you would go the route of having all of those classes implement some top-level interface which defined the methods. This is preferable to the casting idea, from a design point of view, so when you get finished with your implementation which uses casting you might consider trying to abstract the various classes so that their methods (which would still do different things for each class) have the same name.
 
Jesse Matty
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I won't know what type of object the array will hold until runtime as the enemies are randomly created in the game.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the back of mind, something is telling me that the Visitor Pattern and double dispatch technique might be helpful but I'd have to see more of your code to say for sure. When you're blocked with issues like this, it also could be a sign that your design smells and it's worth thinking about how you could simplify things.  Another thing that comes to mind is an interface that captures what an "enemy" essentially can do so that you can adapt the specific methods to a common interface, say something called a Combatant or Opponent or BadGuy.  The Adaptor and Bridge patterns might come in handy.

But then again, these ideas may be way above your current level of skill and sophistication so again, I'd go back to trying to simplify things.
 
Jesse Matty
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved it I think. I made all of my enemies hobgoblins brigs, dragons ECT.  child classes of  a parent abstract class that extends LandSquare called Enemies. By doing that I can call the methods I need for each enemy in my array list of objects of  the abstract  parent class enemies I create for battle  and depending on the type of enemy I added to my new Enemies array list it will invoke the method for the child class of the correct enemy type brig goblin dragon ECT. Right?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If someone describes how they made a plane fly, you might say "right" but to be really convinced, you'd probably want to see the plane actually flying. Take your code/design out for a spin and see how it works out for you. If at some point you find it's difficult to work with, then you'll have your answer. If you can continue to keep it running without much problem, then still you'll have your answer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!