• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classes in source file

 
JL Valor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying for the Sun Java Cert exam and this problem appeared on a mock exam.
What will happen when you attempt to compile the following in a Test.java file?
class Base{
public static void main(String[] args) {
System.out.println("Hello");
}
}
public class Test extends Base{}
Well, I got it wrong but when i ran it myself it outputs "Hello." My question is...I though there could only be one default or public class in a source file. If that is true, then why is this code successful?
Thanks
JL
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could only be one public top level class (ie. not an inner class) The public class should have the same name as the filename. i.e yourPublicClassName.java
Bosun
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember class test "is a" base and therefore inherits the methods of base, including main.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one public class per source file is the rule I heard and I only see one here.
[This message has been edited by herb slocomb (edited March 22, 2001).]
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JL,
What were the choices for an answer?
What did you pick out of those choices, and what was given as the correct answer?
Thanks,
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JL,
I think where you got confused is the other rule is that any top level class must be either public or default
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic