user979616
user979616

Reputation: 273

How to call a certain classes methods (inheritance)

Im writing a small java game in which there are various types of monsters, a couple computer controlled players and a human controlled player. The game world is a fairly basic 2D grid. The grid is declared so that it expects Humanoid objects. That is, Humanoid [][] grid. However, various parts of the humanoid code need to be overridden for each type of character.

What im having some trouble with is how I can call the humans specific move method if the code expects Humanoids and also how to check bounds on the movement.

If anything is unclear, let me know.

Thanks

Note: My humanoid class doesn't have a move method and due to the fact that im doing this for class, it isn't allowed to either

Upvotes: 0

Views: 130

Answers (3)

Bill K
Bill K

Reputation: 62759

The point of inheriting is that you should not have to know which sub-class you are operating on. For instance, say that both humans and monsters shared a base class called "Mobile", and Mobile had an abstract method "move". Humans might override move to walk across the map, birds might override it to fly, and landsharks might swim under ground.

Thing is, your map just calls Mobile.move(), and the action will be delegated to the actual subclass to do the moving.

If you have to know which class you using, you're doing it wrong--however sometimes you need to know something ABOUT the class, for instance, if the terrain is impassable, then you might call Mobile.canFly() to see if the given critter supports flying.

Later, if you decide humans can fly via spells you simply modify the human class and everything else just works, but if you had tested the class and only permitted "Vulture", "Eagle" and "Dragon" to fly over an obstacle, then your code is a lot messier to fix up if you change it or add to it.

Upvotes: 0

Wyzard
Wyzard

Reputation: 34563

If you override the methods in subclasses, you don't have to do anything special to call them. Just call the method on your Humanoid variable, and whatever actual class the instance is, that's the version of the method that will run.

You'll probably want to make Humanoid an abstract class with abstract methods that must be defined in subclasses, or maybe even an interface. But it'll work even if you define a method, with code, in Humanoid and then override that same method with different code in a subclass. The subclass's version of the method will run when the object you call it on is an instance of the subclass, even if your variable is of type Humanoid, not the subclass.

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120168

You don't have to do anything. If you have a Humanoid class, with a move method, and a Human class that extends from Humanoid, with its own move method, when you call move Human's version will be used (if you call it on a Human instance).

So if you do

Humanoid human = new Human();
human.move();  // Human's move is invoked

Humanoid orc = new Orc();
orc.move(); // Orc's move is invoked

the runtime automagically invokes the appropriate move method.

Upvotes: 1

Related Questions