• 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

Question in RHE CD

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Question 49 in exam Bonus 1 says:


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?


The option A) void zzz() { } is given as wrong.
I know final methods cannot be overriden, but these two source files compiled successfully:
<pre>
package p1;
public class X{
final void aMethod(){}
}
</pre>
<pre>
package p2;
import p1.X;
public class Y extends X{
void aMethod(){}
}
</pre>
I supose it compiled because class Y cannot see aMethod() in X. It is the same as when I can declare a method with the same name and argument list that another final and private one in a superclas. Please correct me if I am wrong.
And shouldn't answer A be given as correct then?
Thank you
[This message has been edited by Mafalda Alabort (edited March 25, 2001).]
[This message has been edited by Mafalda Alabort (edited March 25, 2001).]
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried the following code:
package p1;
public class xp1
{
final void aMethod()
{
System.out.println("p1 aMathod");
}
}
package p2;
import p1.xp1;
public class yp2 extends xp1
{
void aMethod()
{System.out.println("p2 aMathod");}
public static void main(String args[])
{
yp2 obj= new yp2();
obj.aMethod();
}
}
on compiling yp2 the following WARNING results:
"Method void aMethod() in class p2.yp2 does not override the
corresponding method in class p1.xp1. If you are trying to override this method, you cannot do so because it is private to a different package."
final methods cannot be overriden anyway, so it is valid for void aMethod(){} to appear in the subclass in a different package, as, it is being declared again and not overridden.
i would say that's an error in the RHE exam answer, unless somebody has a better reason to say it is correct.
 
reply
    Bookmark Topic Watch Topic
  • New Topic