Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Top level class??

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abhi,

The explanation is very clear. I got the concept.

Swapna.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim,

Before the error was


Now that I changed the protected modifier the class is compling.
Swapna
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic