| Author |
Top level class??
|
Swapna James
Ranch Hand
Joined: Jul 26, 2005
Posts: 56
|
|
Hi, This is a question from a mock exam. Given the following classes declaration in the same file MyClass1.java A.Two classes can never be declared in the same file. B.The code Does't compile as the top most class is protected. C.The code compiles & MyClass2 can only be instantiated in it's sub classes. D.The code compiles & MyClass2 can only be instantiated by the classes in the package 'mypackage'. B is the answer. I thought top most class the the one with the widest access modifer. Can anybody explain to me why MyClass2 is the top level class here and how to identify it?
|
SCJP 1.4
|
 |
Abhishek Jain
Greenhorn
Joined: Sep 02, 2005
Posts: 26
|
|
Hi Swapna Top level classes are those which are not nested in any other class.. Let me explain it to u with an example Class A{ Class B{ } } Class C{ } if code is like this then A: Top Level Class B: Not a Top level class <== C: Top Level Class and haa Top level classes can only be public or default else the code will not compile I think u can understand the meaning Regards Abhishek
|
SCJP <br />SCWCD <br />SCBCD <br />SCEA Preparing
|
 |
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
Am not sure myself how the protected class is topmost class.. but u cannot have a class with specifier protected.... only public final static Regards
|
 |
Swapna James
Ranch Hand
Joined: Jul 26, 2005
Posts: 56
|
|
Thanks Abhi, The explanation is very clear. I got the concept. Swapna.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
The given answer B is nonsense. If you've transcribed it correctly, then evidently this mock exam was written by an unreliable author. Do not spend time worryng about what the author is telling you - it's wrong; the author was confused. A top-level class is a class that is not nested in any way. In the code shown, there are two top-level classes, MyClass1 and MyClass2. These are considered top-level, regardless of the access modifier. You may encounter discussion of "top-level nested classes" or "static inner classes", which used to be considered top-level. This information is old (from before 2001), and should be ignored if you hear it now, as it will be unnecessarily confusing. Top-level classes are not nested. Period. The correct answer would be that the code does not compile because, if there is more than one top-level class in a file, only one may be public, and the rest must be package access (no access modifier).
|
"I'm not back." - Bill Harding, Twister
|
 |
Swapna James
Ranch Hand
Joined: Jul 26, 2005
Posts: 56
|
|
Thanks Jim, Before the error was Now that I changed the protected modifier the class is compling. Swapna
|
 |
 |
|
|
subject: Top level class??
|
|
|