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

2017/6/22

Using method from subclass in superclass

Kevroa Kevroa

2017/6/22

#
How do I use a method from my subclass in the code for my superclass? Basically I have this code to create an options menu:
public void loadOptions()
    {
        Background Background = new Background();
        getWorld().addObject(Background,800,450);
        Title Title = new Title();
        getWorld().addObject(Title,800,175);
        Controls Controls = new Controls();
        getWorld().addObject(Controls,800,325);
        Control_List Control_List = new Control_List();
        getWorld().addObject(Control_List,800,550);
        Exit Exit = new Exit();
        getWorld().addObject(Exit,800,800);
        Selector Selector = new Selector();
        getWorld().addObject(Selector,800,800);
    }
And I need it to run in my main menu when I press the options button.
if (Greenfoot.isKeyDown("enter"))
{
    unloadHome();
    loadOptions();          
    selPos = 3;
}
I've done this before but I don't remember how.. I tried searching for it but didn't find what I was looking for.
danpost danpost

2017/6/22

#
You could just move the method from the subclass into the superclass. There is nothing in the method that would cause any issues in doing that.
Kevroa Kevroa

2017/6/22

#
Oh wow, simply fix haha.. Now that it's fixed, how would I actually go about using a method from a subclass? Might be useful later.
danpost danpost

2017/6/22

#
Kevroa wrote...
Oh wow, simply fix haha.. Now that it's fixed, how would I actually go about using a method from a subclass? Might be useful later.
You simply cannot. At least, not without first declaring the method in the superclass or one of its superclasses. The 'act' method of the Actor class is a prime example of this. In the Actor class, you have something like this:
public void act() {}
an act method without any code to implement -- which is called during an act step (or when manually executed) by code within the greenfoot framework. By including an act method in a subclass of Actor, it will execute instead of the one in the Actor class. This is an example of a method override.
You need to login to post a reply.