What will happen if you try to compile and run this 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();
     }
}
Explanation
The program does not compile because there is no matching constructor in the base class to call super(10,15) of the subclass constructor.
void Box (int b, int w) is a method and not a constructor because of specified return type.
In the description of the method main() there is no error: static and public can appear in any order.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes