0

Consider the following code:

// ======== Abstract class ========

public abstract class Creatures {

     public abstract void loseEnergy();

     public void execute()
     {
          loseEnergy();
     }

}

// ======== Animals ========

public class Animals : Creatures 
{
     public override void loseEnergy(){}
}

public class Birds : Animals 
{
     public override void loseEnergy(){}
}

// ======== Human ========

public class Human : Creatures 
{
     public override void loseEnergy(){}
}

public class Male : Human 
{
     public override void loseEnergy(){}
}

public class Female : Human 
{
     public override void loseEnergy(){}
}

[ This code was based on the code by Jayson suggested here: "Base class methods calling derived class methods ?" ]

In the given code example, I would like to have the runtime executing EACH derived class object's certain method, in this case, which is 'loseEnergy()', however, I could not find the solution.

How do I approach this problem? What can be useful to know or to try.. in order to solve this issue?

Your help is very much appreciated! Thank you!

Kind regards, Segara

P.S. Some search I have done so far:

EDIT:

I decided to stick to the idea I had before which is to have some list that would contain the objects of the classes that have 'loseEnergy()' method. Having such list I will be able to call every object's method 'loseEnergy()', which is what I wanted.

Question can be closed.

Thank you.

Community
  • 1
  • 1
  • It's not clear what you are trying to do. The overriden method should only be called once (and the version that would be called is the one that belongs to the concrete class of the object that calls the method). – Eran Mar 28 '13 at 03:42

3 Answers3

0

I didn't really understand your problem but anyway i can try to give you some means to use abstract classes :

  • If you use a abstract method, you SHOULD override it in a subclasses (like a method declared in an interface)

  • If you want that all inherited class use a same method, you can implement it in the abstract class ; all subclasses will use the method you implements if you don't override it, you've have to not declare it in the subclasses (extends < ABS_CLASS > is good enough)

  • If you want use a method of the abstract class which is override in the sub class you can use the keyword super .

I hope it will help you.

Kraiss
  • 919
  • 7
  • 22
  • Thank you for your reply, user1254600. It certainly helped me to refresh my mind. :) I may have been a bit unclear about my problem...so my main goal behind this question is to implement alike feature that is built in Greenfoot - an environment for java coding. There, you can create a class that extends "Actor" class and it will grant newly created class a public method "act()", which gets called repeatedly at the runtime. I would like to master that technique to call all derived class objects' method. –  Mar 26 '13 at 09:16
0

if you mean that you want the calls: female.loseEnergy() -> human.loseEnergy() -> creature.loseEnergy(), call the base method in the first line of the overriden one

public class Female : Human 
{
    public override void loseEnergy()
    {
        base.loseEnergy();
        // do stuff
    }
}
Exceptyon
  • 1,584
  • 16
  • 23
  • Thank you for reply Exceptyon. :) I am trying to find a way to 'collect' all derived from 'Creatures' classes and subsequently attempting to 'collect' all created/relevant objects of those derived classes, so to be able to call their overriden methods 'loseEnergy()'. –  Mar 26 '13 at 09:42
0

In the Greenfoot environment that you mention in the post above, the act() method is called only on actors which have been added into the "world". Internally, this adds them into a list. The simulation process iterates through the list and calls act() on each object in turn. Objects that are not "in the world" are not known to the system and so do not have their act method called. There is no magic here going on here.

If you wanted similar behaviour but without manually adding objects into a list, you could possibly have the base class constructor add new objects into a global list. I don't know C# so I don't know precisely how to do this, but I cannot imagine it would be difficult.

davmac
  • 20,150
  • 1
  • 40
  • 68