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 › Constructors and Inheritance
Page Index Toggle Pages: 1
Constructors and Inheritance (Read 772 times)
Constructors and Inheritance
Mar 3rd, 2010, 7:29am
 
I've got a superclass that defines a dozen variables. Each subclass adds at most, one or two additional variables. How can I avoid copying and pasting what is essentially the same constructor in to each subclass? Is there some syntactic shortcut that will let me tack a few extra fields on the input variable list?
Re: Constructors and Inheritance
Reply #1 - Mar 3rd, 2010, 7:50am
 
I think the Class default values thread can bring you some elements of answer.
Re: Constructors and Inheritance
Reply #2 - Mar 3rd, 2010, 8:02am
 
Hmm... I'd seen that thread, and unless I'm misunderstanding it doesn't really address my problem.

Maybe a concrete example will help. Let's say I have a superclass like this:

Code:

class Sprite {
 float x;
 float y;
 String name;

 Sprite(float _x, float _y, String _name) {
   x = _x;
   y = _y;
   name = _name;
 }
}


Now I want to have a subclass, which will have all of those variables plus a few more...

Code:

class Agent extends Sprite {
 color c;

 Agent(float _x, float _y, String _name, color _c) {
   x = _x;
   y = _y;
   name = _name;
   c = _c;
 }
}


It seems silly to write out all of those variable assignments again for the second class. Do I really have to do this? If I change the number or names of the variables in the superclass, I'd have to make changes to the constructor in every subclass.
Re: Constructors and Inheritance
Reply #3 - Mar 3rd, 2010, 8:15am
 
you can call the constructor for the class you're extending using super()

super(_x, _y, _name);
c = _c;

(i think, haven't tested it)

http://java.sun.com/docs/books/tutorial/java/IandI/super.html

Re: Constructors and Inheritance
Reply #4 - Mar 3rd, 2010, 8:22am
 
That's helpful (and it does work, I've used it before), but I still have to write out all the temporary variables in the constructor definition for the subclass.
Re: Constructors and Inheritance
Reply #5 - Mar 3rd, 2010, 8:50am
 
Yes, the super() wasn't mentioned in this thread, but I was more thinking of setters, with the chaining calls trick. Ie. you don't necessarily have to set all the variables at once in the constructor itself.

Java is a quite verbose language, it doesn't have syntax sugar to say something like Agent(<Sprite.parameters>, color _c) for example. Anyway, which Sprite constructor to take, if there are several?

The idea is to add setters in a fluid API way:
Agent a = (new Agent(7, 20, "Arthur")).setFrontColor(#55FF77).setBackColor(#110033).setSpeed(55).isMoving(tr
ue);

With an additional advantage of being slightly more explicit than a Agent a = new Agent(7, 20, "Arthur", #55FF77, #110033, 55, true); call, no? Closer of named parameters...
Page Index Toggle Pages: 1