Share on Google+Share on Google+

SCJP Module-3 Question-5

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

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.