Java Challenge Answer

Yes Ademir, you’re completly right.

In Java, all classes extends Object, and every class, before execute its constructor’s code, makes a call to its superclass through the ‘super()’ assignment. So, if you don’t call ‘super()’ inside the subclass’s constructor, the compiler will do it for you implicitly, but it will always decide by using the ‘super()’ with no-args.

In the last post, TShirt class doesn’t declare any constructor, so the compiler defines a default constructor with default ‘super()’ with no-args. However, its superclass (Clothes) doesn’t have a default constructor. So we must declare ‘super(“string goes here”)’ in TShirt’s constructor explicitly or we will get a compiler error.

The right code would be:


class Clothes {
Clothes (String s) { }

}

class TShirt extends Clothes {
TShirt() {
super(“explicitly call to superclass”);
}

}

Later, I’ll post others Java Challenge.
Keep tuned.


Fernando

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s