Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Green's Exam #1 Question 59

 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember that each source file must have a public top level class yet the below example works. How is this possible?
class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver){
System.out.println("Over.amethod");
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Your memory is failing you ... The correct quote is:

A Java source file can contain AT MOST one public top level class.

Therefore even an empty file is a valid Java source file.
Regards,
Manfred.
 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help Manfred. This certainly clears my confusion.
Kevin
Originally posted by Manfred Leonhardt:
Hi,
Your memory is failing you ... The correct quote is:

A Java source file can contain AT MOST one public top level class.

Therefore even an empty file is a valid Java source file.
Regards,
Manfred.

 
sandeep bagati
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kevin
The question is perfectly right and answer too.
The Top-Level classes can be public or can have a default modifier.
Originally posted by Kevin Cary:
I remember that each source file must have a public top level class yet the below example works. How is this possible?
class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver){
System.out.println("Over.amethod");
}
}

 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandeep,
I your message seems to say a top level class can be public or default. For clarification, I think a top level class can have the attributes, public, protected, or default but not private. Is this correct?
Kevin
propoise95117@yahoo.com
Originally posted by sandeep bagati:
hi kevin
The question is perfectly right and answer too.
The Top-Level classes can be public or can have a default modifier.

 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about this last post. The mistake is mine. A top level class can either public or default.
Kevin
Originally posted by Kevin Cary:
Hi Sandeep,
I your message seems to say a top level class can be public or default. For clarification, I think a top level class can have the attributes, public, protected, or default but not private. Is this correct?
Kevin
propoise95117@yahoo.com

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic