It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Package" Watch "Package" New topic


Kaushik Badiyani

Joined: Jun 07, 2001
Posts: 27
Could somebody explain me the exact reason for this Question..

What happens when you attempt to compile and run these two files in the
same directory?
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
public class P2 extends P1{
void afancymethod()
a. Both compile and P2 outputs "What a fancy method" when run
b. Neither will compile
c. Both compile but P2 has an error at run time
d. P1 compiles cleanly but P2 has an error at compile time
Plz explain me in detail as i am very weak in Packages as i havent done anything practically
Metal Zhang
Ranch Hand

Joined: Aug 05, 2001
Posts: 52
I think the answer is D.
You can compile them by yourself.If you want to compile and run them successfully.You must do like this.
1,You must declare class P1 as public class;

2,Compile with the command line list below:

3,You must import class MyPackage.P1 before you define class P2;

or declare class P2 like this:

4,You must define a method named main in file if you want to run it.

5,You must make sure the current directory "." in your system's CLASSPATH variable.
Java lover from hell!
[This message has been edited by Metal Zhang (edited August 17, 2001).]

Java lover from hell!
subject: Package
It's not a secret anymore!