What will be the result of an attempt to compile and run the following program?
class Box
{
int b,w;
void Box(int b,int w)
{
this.b = b;
this.w = w;
}
}
public class MyBox extends Box
{
MyBox()
{
super(10,15);
System.out.println(b + "," + w);
}
static public void main(String args[])
{
MyBox box = new MyBox();
}
}
Choices:
A. Does not compile; main method is not declared correctly
B. Prints 10,15
C. Prints 0,0
D. None of the above
Correct choice:
- D
Explanation:
The program does not compile because there is no matching constructor in the base class for the super(10,15) call from the subclass constructor. void Box(int b, int w) is not a constructor, because a return type is given. If it had been a constructor, the variables w and h would have been initialized to 10 and 15. The program would have compiled correctly and printed 10,15. There is no error in the declaration of the main() method; static and public can appear in any order.
2 comments: