Welcome Guest | Sign in | Register
Inner Classes - Discussion Page For Q.3026 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Inner Classes > Questions and Answers

Q: 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" ?
A. SubClass Sub = new SuperClass().new SubClass(); B. SubClass Sub = new SuperClass.SubClass();
C. SuperClass.SubClass Sub = new SuperClass.SubClass(); D. SuperClass.SubClass nbc = new SuperClass().new SubClass();

Answer and Explanation

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

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

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.