Share on Google+Share on Google+

SCJP Module-3 Question-10

Advertisement
The given example will test your understanding of Super class and Sub class in Java and their object initialization.

Given below the sample code :

class SuperClass {
public static void main(String[] args) {
new SubClass();
}

SuperClass() {
System.out.print("Inside SuperClass ");
}
}

class SubClass extends SuperClass {
SubClass() {
System.out.print("Inside SubClass");
}
}

What is the output of the following code ?

1. Inside SuperClass

2. Inside SubClass

3. It will give compile error

4. Inside SuperClass Inside SubClass.

Answer :

(4)

Explanation :

Because the 'SuperClass' constructor automatically run, therefore the first output would be "Inside SuperClass" & after it, the instance of the "SubClass" is called which in turn prints "Inside SubClass".

Advertisements

Posted on: July 12, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.