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

from RHE Bonus Questions

 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from Rhe book's bonus question set...
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 thought all are correct because since the subclass is out side the package so the zzz() method of parent is not accessible from the subclass.
but the answer is given B,C,D which are very obvious ,but i have a doubt that A also is correct...can anybody explain me the right thing
thank u
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should think it would be that final does not change what is returned or what is passed to method.
Dan
 
mousami bhattacharya
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sathi,
I agree with you , even I thought it to be all options .
I checked out by writing the code.and we are correct.
If a subclass is in a different package ,then the default method in parent is not accessible to the subclass and we can provide the same method signature with a more restricting accesibility .It just gives a warnign during compile time.
So the answer should be all 4
Mousami
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sathi,
I entered the below code and it ran fine. You are right about
option A.
<CODE>
package mypack1;
public class Parent {
final void zzz() {
System.out.println("zzz");
}
}
//File: Child.java
package mypackage;
import mypack1.*;
public class Child extends Parent {
void zzz() {
System.out.println("child zzz");
}
public static void main(String args []) {
new Child().zzz();
}
}
</CODE>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic