We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › super() in a constructor
Page Index Toggle Pages: 1
super() in a constructor (Read 789 times)
super() in a constructor
May 19th, 2009, 2:12pm
 
Hello,

The following code fragment is from code 48-05 on pg 457 of the book "Processing":

Quote:
class SpinArm extends Spin {
  
  SpinArm(float x, float y, float s) {
    super(x, y, s);
  }



I understand that here super is calling the constructor of the superclass. How does the program know that it's calling for the constructor of Spin? How come it's not super.Spin(x, y, s)?
Re: super() in a constructor
Reply #1 - May 19th, 2009, 2:29pm
 
The call to super() is always to the class that your class extends.

It works as if the compiler fills in the proper values for this:
Code:
this = new super();//Don't try this at home, kids 



Then tacks on the new stuff that your class contains.
Re: super() in a constructor
Reply #2 - May 19th, 2009, 11:05pm
 
Phattee wrote on May 19th, 2009, 2:12pm:
How does the program know that it's calling for the constructor of Spin How come it's not super.Spin(x, y, s)

1) A class can have only one parent (can be Object class).
2) super() is implicitly a call to the constructor of the parent, no need to give it by name.
3) If the parent has several constructors, they are distinguished by the number and kind (type) of the parameters of the call.
Page Index Toggle Pages: 1