Originally posted by Daniel Liu:
Question: [Check all the correct answers]
Given the following calss definition:
1. public class DervedDemo extend Demo {
2. int M, N, L;
3. public DerivedDemo (int x, int y) {
4. M = x; N = y;
5. }
6. public DerivedDemo ( int x) {
7. super(x);
8. }
9. }
Which of the follwing constructor signatures MUST exist
in the Demo class for DerivedDemo to complie correctly?
a public Demo(int a, int b)
b public Demo(int a)
c public Demo()
The answer are b and c.
Could you explain why c is necessary ?
Because unless you make a specific call to a constrictor in a super class, the default constrictor is called. In the constrictor
public DerivedDemo (int x, int y) {
M = x; N = y;
}
no called is made to super so, Demo() is called.
Now since there is a call to super in the other constructor to
super(int) you have to have a constrictor Demo(int) defined.
When you define a constrictor, a default one isn't automatically generated and must be explicatly defined.