I’ve encontered the following block of code while studying for Java Certification:
—
class Clothes {
Clothes (String s) { }
}
class TShirt extends Clothes { }
—
However, this code have a mistake. Anyone could tell what is it??
Next post I’ll tell the answer… think about it.
—
Fernando
Hey Sr.
Would The right code be this? :
class Clothes {
private String s;
Clothes (String s) {
this.s = s;
}
}
class TShirt extends Clothes {
TShirt(String s){
super(s);
}
}
The reason of the mistake, is that the superclass does not defines a default constructor (without parameters), thus, all subclasses are forced to define a constructor, making a call to the superclass constructor, supplying the desired parameters.
The TShirt class is one possibility. Another is:
class JavaTShirt extends Clothes {
JavaTShirt() {
super(“Java”);
}
}