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

 
 

SCJP Module-3 Question-5
Posted on: July 12, 2010 at 12:00 AM
The class given below tests your understanding of classes and their objects initialization.

Given below the sample code :

class SuperClass {
SuperClass() {
}

public class SubClass {
SubClass() { }

}

}

Which is the correct way to declare the object of public "Subclass" , which can be use outside the "Superclass" ?

1. SubClass Sub = new SuperClass().new SubClass();

2. SubClass Sub = new SuperClass.SubClass();

3. SuperClass.SubClass Sub = new SuperClass.SubClass();

4. SuperClass.SubClass nbc = new SuperClass().new SubClass();

Answer :

(4)

Explanation :

If the "Superclass" is declared as "static" it will be declared like this :

   SuperClass.SubClass Sub = new SuperClass.SubClass();

But if the "Superclass" is declared as "public" it will be declared like this :

   SuperClass.SubClass nbc = new SuperClass().new SubClass();

Related Tags for SCJP Module-3 Question-5:


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.