Showing posts with label super( ). Show all posts
Showing posts with label super( ). Show all posts

Monday, 31 October 2011

Sun Certified Java Programmer (SCJP)

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:

Sun Certified Java Programmer (SCJP)

Constructors


A constructor is used when creating an object from a class. The constructor name must match the name of the class and must not have a return type. They can be overloaded, but they are not inherited by subclasses.

Invoking constructors


A constructor can be invoked only from other constructors. To invoke a constructor in the same class, invoke the this() function with