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 › Extended class constructors
Page Index Toggle Pages: 1
Extended class constructors (Read 458 times)
Extended class constructors
Dec 20th, 2005, 3:30am
 
I'm not sure if this is a bug or part of java or what.

Code:

class rawr
{
rawr()
{}
}
class meow extends rawr
{
meow(int sss,int b)
{}
}


Will compile. However if the class meow is extending from has arguments in its constructor.. like so...

Code:

class rawr
{
rawr(int b)
{}
}
class meow extends rawr
{
meow(int sss,int b)
{}
}


This will not compile.


I don't understand this.

Check this out
http://www.javacoffeebreak.com/java104/java104.html

Looks like you should be able to do this...



*edit*
Finally.. you're able to compile this

Code:

class rawr
{
rawr()
{}
rawr(int sss)
{}
}
class meow extends rawr
{
meow(int sss,int b)
{}
}
Re: Extended class constructors
Reply #1 - Dec 20th, 2005, 6:10am
 
welcome to object oriented programming, i'll be your substitute teacher, mr. fry.

in the first case, you're not providing a default constructor for the base class. i think some compilers may create one for you, others are more specific and require that you make one.

classes that extend others will always call the constructor of their parent. so in your first example, there's basically an invisible line inside meow(int sss, int b) that calls "super()", which runs the code inside the rawr() initializer.

huh?

so this code:
Code:

void setup() {
new meow(45, 68);
}

class rawr
{
rawr()
{
println("rawr");
}
}
class meow extends rawr
{
meow(int sss,int b)
{
println("meow!");
}
}

will print
Code:
rawr
meow!

is that what you expected?

the constructor of the superclass is always called in a subclass.

the code in your problematic example basically says that the only way to create a class of type 'rawr' is to pass it an int to get it started. because you're not doing that, it's giving you an error. this helps you enforce things like a case where the base class *must* have a particular variable initialized.

in order to fix things, and tell the compiler that you know what you're doing, you need to call super() yourself:

Code:

void setup() {
new meow(45, 68);
}

class rawr
{
rawr(int k)
{
println("rawr");
}
}
class meow extends rawr
{
meow(int sss,int b)
{
super(4);
println("meow!");
}
}


by passing 4 to the superclass, everything is ducky, and class is done for the day.
Re: Extended class constructors
Reply #2 - Dec 20th, 2005, 8:00am
 
oi. Thanks for that explanation!

*takes notes*

Now that I'm thinking about it. That's rather sneaky of Java or Processing? to hide a super() in there. This is a very important concept that I somehow missed learning. Very helpful!
Page Index Toggle Pages: 1