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

2014/7/6

Explanation of "super"

Amy Amy

2014/7/6

#
Hello *.*, i'm completely new to Java and Greenfoot, so please excuse my perhaps stupid question: I can not find any explanations to "super" in the Greenfoot documentation. Ok, i've already noticed that super declares the world environment with it's dimension and cell size, and it's the basis to run all Greenfoot programs(?) - but is there a reason that it isn't mentioned in the doc? Isn't it a method as other methods, too? Or is super a class? Thanks for clarification Amy
NikZ NikZ

2014/7/6

#
When opening a new world class, there will be a "super(600, 400);" already put in for you, but also a commented explanation of its purpose, which is in fact the dimension and cell size. Super is a method which creates the world.
NikZ NikZ

2014/7/6

#
I believe super() is not a Greenfoot method, therefore it will not be in the Greenfoot doc.
NikZ NikZ

2014/7/6

#
You can redirect here for more.
NikZ NikZ

2014/7/6

#
Super is part of Java, which will explain why it turns light blue in the source code. To clarify, super is not a class, nor is it part of Greenfoot.
danpost danpost

2014/7/6

#
'super' is a reference to the class that the current class extends. What you see in the World class with the dimensions of the world is a call to the constructor of the World class (the 'super-class' of the class you created that extends the World class). If you created a generic World object, you would use:
1
World world = new World(600, 400, 1){};
where 'new World(600, 400, 1)' is a call to the constructor of the World class. This is exactly what is called when you call:
1
super(600, 400, 1);
from your sub-class of World. 'super' is a java keyword and is explained here.
Amy Amy

2014/7/6

#
Ah, thanks to all of you. I see i have much to learn... :)
NikZ NikZ

2014/7/8

#
Just to clear things up, super() is used for a class to use a parent class constructer (can be used with sub/super classes). Click here for more on super().
trimil trimil

2014/7/8

#
The keyword
1
super
can also be used to access the super-class's methods like this:
1
2
3
4
5
6
7
8
9
public void doSomthing()
{
    super.doSomething();
    doSomthingElseTo();
}
 
private void doSomethingElseTo
{
}
where the super-class has a method called
1
doSomething();
and you want the method do do what the super-class's method does, but also something else.
trimil trimil

2014/7/8

#
The keyword super can also be used to access the super-class's methods like this:
1
2
3
4
5
6
7
8
9
public void doSomthing()
{
    super.doSomething();
    doSomthingElseTo();
}
 
private void doSomethingElseTo()
{
}
where the super-class has a method called doSomething() and you want the method do do what the super-class's method does, but also something else(method doSomethingElseTo).
NikZ NikZ

2014/7/8

#
trimil wrote...
The keyword super can also be used to access the super-class's methods like this:
As you can see "super" has access to the super-class it belongs to.
You need to login to post a reply.