SCJP Module-3 Question-5


 

SCJP Module-3 Question-5

The class given below tests your understanding of classes and their objects initialization.

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();

Ads