Exam 1
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
1) Compile Error: Methods in Base not found
2) Compile Error: Unable to access protected method in base class
3) Compilation followed by the output "amethod"
4)Compile error: Superclass Class1.Base of class Class1.Class1 not
found
Ans: 4)Compile error: Superclass Class1.Base of class Class1.Class1
not found
Using the package statement has an effect similar to placing a source
file into a different directory.
Because the files are in different packages they cannot see each
other. The stuff about File1 not
having been compiled was just to mislead,
java has the equivalent of
an "automake", whereby if it
was not for the package statements the other file would have been
automatically compiled.
/**************************************************
According to RHE (Java 2) Page No.183 last two lines "It is illegal for a package and a class to have the same name."
So IMO there is no question to check about " Superclass Class1.Base of class Class1.Class1 not found". because compiler will check first about same name of package and class and will
show compiler error.what would be better than this if Marcus read this and explain.because i think the answer " compile
error" is ok but this is not for "Superclass Class1.Base of class
Class1.Class1 not found".
------------------
Praveen Kumar
Mumbai ,India
email:
[email protected]