This is from following mock exam:
Java Programmer Certification Mock Exam No 1
Copyright �Marcus Green 2002
Last Updated 17 July 2002
The original question:
Question 58)
You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java
//Base.java
package Base;
class Base{
protected void amethod(){
System.out.println("amethod");
}//End of amethod
}//End of class base
package Class1;
//Class1.java
public class Class1 extends Base{
public static void main(String argv[]){
Base b = new Base();
b.amethod();
}//End of main
}//End of Class1
I have three questions about this question. They are noted at appropriate places below:
Question 58)
You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled
// How can you have two packages in same dir. The package name has to match the dir name. Rigth?
Base.java
//Base.java
package Base;
public class Base{ // This class will have to be
// public
// so that it can be accessed
// from Class1
// package. Right?
protected void amethod(){
System.out.println("amethod");
}//End of amethod
}//End of class base
package Class1;
//Class1.java
import Base.*; // you have to import Base class
public class Class1 extends Base{
public static void main(String argv[]){
Base b = new Base(); // following line will
// give an error
// because you can
// access protected
// method via an
// object of sub-clas
//b.amethod();
Class1 c = new Class1(); // this will work.
c.amethod();
}//End of main
}//End of Class1
[ September 03, 2002: Message edited by: Barkat Mardhani ]
[ September 03, 2002: Message edited by: Barkat Mardhani ]