Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Query on Q49, RHE Bonus 1 - final 'default' method overriding

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Supposed wrong answer:
A) void zzz() { }
My argument:
Given the child class is in a different package, it can't see the void zzz() method and so it can create all sorts of void zzz() doing whatever they like because it's not really overriding or overloading.
This not the case?
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Trevor Green:
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
Supposed wrong answer:
A) void zzz() { }

My argument:
Given the child class is in a different package, it can't see the void zzz() method and so it can create all sorts of void zzz() doing whatever they like because it's not really overriding or overloading.
This not the case?


No Trevor, I think subclass is in another package but its parent class is defined as public and so the child class can refer to its vaiables or can legally overload its methods also.
Jyotsna
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Trevor Green:
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
Supposed wrong answer:
A) void zzz() { }
My argument:
Given the child class is in a different package, it can't see the void zzz() method and so it can create all sorts of void zzz() doing whatever they like because it's not really overriding or overloading.
This not the case?


If you are subclassing the Parent class, you are still overriding the method even if the subclass is not in the same package as the parent.
Think about it: You can overide methods of classes in the standard java packages, i.e the equals() method the Object class, but the subclasses do not have to reside in the same package as the superclasses.
In this case, the reason the answer is incorrect is that you can not override a final method.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "being in a separate package" is a red herring and has no effect on the answer.
 
Trevor Green
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that if void zzz() was private, the child classes could have any method they like called zzz() (returning ints, throwing exceptions, taking arguments, etc) because the child cannot see the parent's void zzz() method - it's private.
In this case, the void zzz() method is default. This means that a child in a different package can't see it's parent's void zzz() method and so why wouldn't the above still hold true?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually what happens is that you get a "Note" from the compiler saying


Note: Method void zzz() in class Child does not override the corresponding method in class Package1.Parent. If you are trying to override this method, you cannot do so because it is private to a different package.
1 warning


Then it compiles and runs fine.
This is the test code that I used
 
Trevor Green
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy.
So as far as Certification Questions go, if you would get a warning, it still counts as having compiled? ie, if the question reads, will this code (above) compile, the answer would be yes?
In RHE, the answer implied that if you get a warning, it doesn't compile.
I take it that if the void zzz() in the child class said private int zzz() then this would be fine because, as the warning said, the child.zzz() is not overriding the parent.zzz().
 
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled a similar program as Cindy's with jdk 1.3.0_02 and did not get the warning she mentioned; it compiled and ran cleanly.
I don't know, from my testing, I tend to agree with your original reasoning and I think that RHE is wrong...
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested it in jdk1.2.1

I think that you would have to read the wording of the question VERY CAREFULLY. In this case it looks like RHE were wrong.
[This message has been edited by Cindy Glass (edited June 14, 2001).]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done Trevor for pointing that one out.
I wonder if RHE know?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They stop by here on a regular basis, especially Michael Ernest. As a matter of fact I think that Robert Simon had dinner with Paul Wheaton Tuesday. Anyway, one way or the other, we will try to get this to their errata list.
Or perhaps get clarification on what they intended.
OK, I e-mailed Michael Ernest. Maybe he can set us straight .
[This message has been edited by Cindy Glass (edited June 14, 2001).]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested it and I got the warning message but the program compiled. Which makes sense since the method isn't visible, the child class should be able to override it. So the rule is that you can't override a final method... if you can see it.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Trevor:
I think you are right,and I have tested the code it complied with a warning,but it did compile and I also run it,everything went smoothly.The answer should be wrong.

Originally posted by Trevor Green:
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
Supposed wrong answer:
A) void zzz() { }
My argument:
Given the child class is in a different package, it can't see the void zzz() method and so it can create all sorts of void zzz() doing whatever they like because it's not really overriding or overloading.
This not the case?



------------------
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Imagine my embarrassment at not having a copy of the book on hand!
I certainly agree in principle with the discussion below. But I'll have to go back to actual text to see the whole thing, and see why we're wrong.
Anyone have a page number to share?
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Junilu Lacar
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Michael Ernest:
> I'll have to go back to actual text to see the
> whole thing, and see why we're wrong.
> Anyone have a page number to share?
Michael, see the original post: Question 49 of the Bonus 1 test. Full text of the question was also given in the original post.
Long day, huh? Thanks for taking the time, though. We really appreciate it.
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaranch.com/ubb/Forum24/HTML/008973.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic