Home Tutorial Java Scjp Part3 SCJP Module-3 Question-10

 
 

SCJP Module-3 Question-10
Posted on: July 12, 2010 at 12:00 AM
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".

Related Tags for SCJP Module-3 Question-10:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.