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

a problem about overriden

 
HiBob Chu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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.
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh I am sorry. I missed the point that the subclass is in different package.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
HiBob Chu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that would defeat the whole purpose of declaring a final method.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think there is any viable combination:
private final amethod() { }
because privates are automatically final. Right?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think "final" is allowed, but basically ignored by the compiler, on a private method.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A is also a valid answer. Though, it won't exhibit polymorphic behaviour.

it will print In Parent
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic