• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package and Overriding

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW,the anwser to the above question is B,C,D.
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Edy Yu
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and also since it's final, it can't be overiddend in the subclass
 
Bin Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you testes right?
I tested your code which gave me a compilation error.
 
Bin Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic