Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package

 
Kaushik Badiyani
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
//File P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
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
Thanx
Kaushik
 
Metal Zhang
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 P1.java 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 P2.java 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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic