This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Package and Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Package and Overriding" Watch "Package and Overriding" New topic
Author

Package and Overriding

Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
Question:
Consider the following class definition:
public class Parent {
final void zzz() { }
}
Which of the following methods may appear in a subclass of Parent, when the subclass is in a different package from Parent? Choose all correct options.
A) void zzz() { }
B) void zzz(int j) { }
C) final void zzz(float f) { }
D) public final void zzz(double d) { }
I choose A,B,C,D
but the anwser said I am wrong.
Who can give correct anwser to this question?
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
BTW,the anwser to the above question is B,C,D.
Edy Yu
Ranch Hand

Joined: Nov 21, 2000
Posts: 264
Cus the subclass is in the different package and final void zzz() doesn't have any modifier(means it's only avaible in the same package)


<i><br />Sun Certified Programmer for Java 2 Platform (SCJP)<br />Sun Certified Developer for Java 2 Platform (SCJD)<br />Sun Certified Web Component Developer for Java2 Platform, Enterprise Edition (SCWCD)<br />Sun Certified Business Component Developer for Java2 Platform, Enterprise Edition (SCBCD)<br />Sun Certified Enterprise Architect for J2EE (SCEA)<br />IBM Certified Enterprise Developer, WebSphere Studio V5.0<br /></i>
Edy Yu
Ranch Hand

Joined: Nov 21, 2000
Posts: 264
and also since it's final, it can't be overiddend in the subclass
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
But I tried the following code and it compiles well
Sub.java
import abc.Parent;
public class Sub extends Parent {
void zzz() { }
// void zzz(int j) { }
//final void zzz(float f) { }
//public final void zzz(double d) { }
}
Parent.java in another package abc
package abc;
public class Parent {
final void zzz() { }
}
So I think anwser A is right since the zzz(0 method in Parent class has default access,it can not be seen in other packages.
So the Sub class can define zzz() method freely.
This is just like private method.The subclass can define a method with the same signature and return type as the private method in superclass.
Edy Yu
Ranch Hand

Joined: Nov 21, 2000
Posts: 264
Are you sure you testes right?
I tested your code which gave me a compilation error.
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
I am sure.
What error did you get?
you have to put Parent.java in a dirctory named abc.
For example:
Parent.java -->c:\java\abc
Sub.java -->c:\java
Then you can compile Sub.java without error.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Package and Overriding
 
Similar Threads
RHE mock question bonus1 q49
R&H bonus1 q49
from RHE Bonus Questions
RHE Bonus1 Q49
R&H bouns1-49