This site requires JavaScript, please enable it in your browser!
Greenfoot back
Twentyonestreet
Twentyonestreet wrote ...

2015/2/23

Help with polymorphism

Twentyonestreet Twentyonestreet

2015/2/23

#
In my program I have super class, which inherits from Actor and it has three subclasses. I use polymorphism there (it works like I have an empty method in super class an methods with same name in subclasses, but I call the methods in subclasses from the super class depending on the object, that does this action. but is it possible to specify in the super class, method from which subclass I want to call?
Super_Hippo Super_Hippo

2015/2/23

#
I didn't really understand your problem (maybe because the last sentence is a bit messed up). If you call the method from the subclasses, then they will use the method which is written in their class code if there is one with this name (unless you use 'super().methodName()' explicitly). Did you try to code and notice the problem? If yes, please explain what happens and what you want to happen.
danpost danpost

2015/2/23

#
Maybe you are confused about how subclassing works. You cannot create an object from a superclass and then decide to make it a specific type (from one of its subclasses) at a later time. You can, however, assign a field (String name, or int id) to be used conditionally to determine what behavior or state it should have in the superclass and dispense with the subclasses. Otherwise, you create object from the subclasses and they are restricted to perform actions and have state specific to its class and superclass -- not from a different subclass of its superclass.
Twentyonestreet Twentyonestreet

2015/2/23

#
I meant, that in super class I have method movement() and methods with the same name in subclasses. After in super class I have another method ( identifyClass() ) that identifies the object (of subclass), which is located in given area, and then the method returns the class of that object. And from act() of super class I want to call method movement() of the class that was returned by method identifyClass(). is it somehow possible?
Super_Hippo Super_Hippo

2015/2/23

#
It is but wouldn't it be easier to call the movement for each class by their own act method?
danpost danpost

2015/2/23

#
The thing is ... you do not have to know which subclass the actor is of. For example, if you had an 'Orb' class with 'RedOrb', 'GreenOrb', and 'BlueOrb as subclasses of 'Orb', with methods as you have described, you can just use:
((Orb) actor).movement();
and the movement from the correct subclass will be executed. All you need to know is that is of Orb type, regardless of subclass it originates from.
Twentyonestreet Twentyonestreet

2015/2/23

#
danpost, should I then make method movement() static? and if I have an object of the class, but not class, should I use ((object.getClass) actor.movement() or object.movement()?
danpost danpost

2015/2/23

#
Twentyonestreet wrote...
danpost, should I then make method movement() static?
No. The method is an instance method as it deals with the movement of the objects of that type.
if I have an object of the class, but not class, should I use ((object.getClass) actor.movement() or object.movement()?
It would be best if you show the code around which you are trying to call the method from, indicating what class and type of class the code is in and how that class relates to the class that the method is in.
danpost danpost

2015/2/24

#
Just as an example, the following would work in the World subclass:
Object orb = new RedOrb(); // as an Object type, you cannot run any Actor, Orb or RedOrb class methods on 'orb'
addObject((Actor)orb, 100, 100); // 'addObject' requires an Actor type object; using just 'orb' would not work
((Orb)orb).movement(); // 'movement' cannot be executed on an Object type, nor an Actor type, as the method is declared in the Orb class
As an Orb type, the 'movement' method from the appropriate subclass (RedOrb in this case) will be executed because the 'movement' method in the Orb class was overridden there. Another prime example is the 'act' methods you write in your classes. The Greenfoot framework does not know what you are going to name your Actor subclasses -- but, it does not need to know. The following method is taken directly from the source of greenfoot (greenfoot.core.Simulation.JAVA):
private static void actActor(Actor actor)
{
    actor.act();
}
Notice that 'actor' is typed as an Actor object, not as any specific subclass of Actor; but, nonetheless, the correct act method will execute for the 'actor' given because if the empty 'act' method in the Actor class is overridden, the overriding method will execute.
Twentyonestreet Twentyonestreet

2015/2/24

#
danpost, thank you! Got it working.
You need to login to post a reply.