Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question from Marcus Green's mock exam

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there (hope Marcus will read this):
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat, it's Base.java and Class1.java, the files that are in the same directory. A package is represented by a directory.
The question said
You have these files in the same directory.
and you said
How can you have two packages in same dir.

It does not say which directory, so I am going to assume it is .../Class1.
So when Class1.java is compiled the compiler will try to find the file Base.java.
When you compile Class1.java you get:
So it fails to compile.
What was the answer to the original question?
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.

That's near enough to what the compiler says.
-Barry
Sorry about editing freakouts.
[ September 03, 2002: Message edited by: Barry Gaunt ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry:
But these two files belong to two distinct packagees i.e Base and Class1. And these two packages are in same directory as per punch line of the question...
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice question Barkat, package questions do come up on the exam and once you get your head around the concepts they are fairly easy points. You can compile a file with a package direction in a directory that doesn't match the package name but you cannot execute it or see it from another class. Thus taking the package line out of class1 will not help because Base (or Base.Base to be precise) will not be visible, plus amethod is protected anyway. Basically it is very broken code.
Hmm I like this question, I think I'll write some more like it.
Marcus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic