File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a problem about overriden Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a problem about overriden" Watch "a problem about overriden" New topic
Author

a problem about overriden

HiBob Chu
Ranch Hand

Joined: Aug 12, 2002
Posts: 86
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.
The answer doesn't include :
A. void zzz(){}
I think A is right, for the subclass couldn't
see the zzz in it's superclass, so the subclass
can define the function zzz freely, isn't it?
Charu Murali
Ranch Hand

Joined: Jul 12, 2002
Posts: 37

I think A is right, for the subclass couldn't
see the zzz in it's superclass, so the subclass
can define the function zzz freely, isn't it?


if final void zzz() in baseclass is private then we can include void zzz() in subclass for the same reason you have given. Since it is final A could not be the answer

HTH
Charu
[ August 22, 2002: Message edited by: Charu Murali ]
HiBob Chu
Ranch Hand

Joined: Aug 12, 2002
Posts: 86
I see, the "default" virable instead of methods
in father class couldn't be seen in his subclass
local in different package.
so A is incorrect!
Thank you!
Charu Murali
Ranch Hand

Joined: Jul 12, 2002
Posts: 37
oh I am sorry. I missed the point that the subclass is in different package.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I don't see how it matters whether the subclass is in the same package or not. You can't override a non-private final method. That's what "final" means.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
HiBob Chu
Ranch Hand

Joined: Aug 12, 2002
Posts: 86
To Newman :
If you couldn't see the function(of couse it's not true in the question) zzz (even though it's final )from the subclass for that the subclass is in different pakage,you could override it freely, I thought.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
No, that would defeat the whole purpose of declaring a final method.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
So in final analysis, this sub-class
a. can not call this method (different package)
b. can not over-ride it (because it is final)
c. can not shadow it (because it not private)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
So in final analysis, this sub-class
a. can not call this method (different package)
b. can not over-ride it (because it is final)
c. can not shadow it (because it not private)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I do not think there is any viable combination:
private final amethod() { }
because privates are automatically final. Right?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I think "final" is allowed, but basically ignored by the compiler, on a private method.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You think correctly, Ron.
From the JLS:
A private method and all methods declared in a final class (�8.1.1.2) are implicitly final, because it is impossible to override them. It is permitted but not required for the declarations of such methods to redundantly include the final keyword.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
A is also a valid answer. Though, it won't exhibit polymorphic behaviour.

it will print In Parent


SCJP2. Please Indent your code using UBB Code
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Jose:
I think you changed some assumptions in the
original question. The sub class is in different
package then the base class. And base class has
default access specifier. And in the package of
sub-class, the base class package is NOT imported.
In your example you are importing base class
package. Hence efectively base and sub class are
in same package.
[ August 22, 2002: Message edited by: Barkat Mardhani ]
Alvin Lee
Greenhorn

Joined: Aug 22, 2002
Posts: 3
Hi Barkat:
I'm not quite agree with this. Though the sub class import the package of the parent class. But the fact that they are in different package is unchanged.
So i think the Answer A could be the solution.
Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
from JLS

A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.
A private method and all methods declared in a final class (�8.1.1.2) are implicitly final, because it is impossible to override them. It is permitted but not required for the declarations of such methods to redundantly include the final keyword.
It is a compile-time error for a final method to be declared abstract.

The concept of hiding is to be clear.Unaccessible field or method method can be declared with same name in the subclass . It is not considered as hiding or overriding. That is identified as a function which belong to the subclass only. It has nothing to do with the parent function.
Is this explanation correct ???
[ August 23, 2002: Message edited by: Binu K Idicula ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi Barkat.
The only facts I can see in the question are that parent is public and its method is friendly and final. Thus I made the assumptions needed for the code to work.
I think Binu has been happily verbose:

The concept of hiding is to be clear.Unaccessible field or method method can be declared with same name in the subclass . It is not considered as hiding or overriding. That is identified as a function which belong to the subclass only. It has nothing to do with the parent function.

[ August 23, 2002: Message edited by: Jose Botella ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Ron:
Your quote:

I don't see how it matters whether the subclass is in the same package or not. You can't override a non-private final method. That's what "final" means.

I think Jose has made a point that subclass can
over-ride a final method as long as parent and
subclasses are in different packages and method
is not declared public in parent.
Thanks
Barkat
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
with a caveat: Polymorphic behaivor will not
manifest.
Hi HiBob:
Seems like your guess was right. Do think the
creator of this question will agree with you?
Thanks
Barkat
HiBob Chu
Ranch Hand

Joined: Aug 12, 2002
Posts: 86
Hi thanks all above ! A good news : Today noon,
I passed the SCJP1.4 with 90% score, I am happy.
Hi Mardhani :
I don't know who is the author, . I don't
remember where I downloaded it, and it was created
in 1999.
I think it's simple to validate my guess: write
a sample code, but sorry now I never writed code in different packge(I don't know how to do it yet),so I will study it then try! hehehe
HiBob Chu
Ranch Hand

Joined: Aug 12, 2002
Posts: 86
Hi sorry I readed Jos's code just now
clearly,it's seems like what I want to do, if it
work rightly, the fact is showed:
subclass couldn't see any "defualt" method
in it's superclass which is in different package,
it means that the "default" asscessible modifier
acts the same action with both virable and methods.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
from Barkat:

I think Jose has made a point that subclass can
over-ride a final method as long as parent and
subclasses are in different packages and method
is not declared public in parent.

Well my intention was the other way round.
Please reread the quote from Binu I gave in my last post.
HiBob congrats for your result.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi HiBob:
Congrats..
Hi Jose:
Whether you (and Binu) call it declaring a brand new method in sub-class or something else, fact remains
that your code proved that it can be done when
base and sub-class are in different packages for a final method with default access. However, there is no polymorphic behavior. That is
contrary to what I and Ron were initially assuming.
Thanks
Barkat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a problem about overriden
 
Similar Threads
Query on Q49, RHE Bonus 1 - final 'default' method overriding
R&H bonus1 q49
Package and Overriding
from RHE Bonus Questions
R&H bouns1-49