• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Top level class??

 
Swapna James
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Abhishek Jain
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abhi,

The explanation is very clear. I got the concept.

Swapna.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Swapna James
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim,

Before the error was


Now that I changed the protected modifier the class is compling.
Swapna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic