wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final being overridden?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final being overridden??" Watch "final being overridden??" New topic
Author

final being overridden??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

Ans: It will compile and give the output "amethod" when run
I feel the answer is wronf becauase once a method declared final cannot be used in the subclass..am i right?
By the way, can private methods be called in the sub classes??
Sonir
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
The b.amethod() call is to the method in Base. Even if the code was:
Fin f = new Fin();
f.amethod();
It would be to the inherited method from Base. What you can't do is override amethod with a new definition in Fin.
Bill
[ January 10, 2002: Message edited by: William Brogden ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The keyword final, when used with a method, simply means that the method cannot be over-ridden. In this case, the method is being inherited, not over-ridden. The following code, however, would be illegal:

However, the following code is perfectly legal because it utilized an inherited method (which can be final).

I hope this makes sense, but the key is that the keyword final means that you can't over-ride a method, not that you can't use it in a child class (that's what the private access modifier is for).
Corey


SCJP Tipline, etc.
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Hi Corey..
I guess both the methods example which u gave are very similar
I cannot understand the differnce between the 2.
Still u have said that one of them is legal while the other one is not.
Can u give me some explainations?
Sonir
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Sonir,
Notice that in his second example he did not try to define amethod in the subclass as this is what is illegal. You cannot override(or redefine) a final method that was defined in the super class.

HTH,


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jason Kretzer:
Sonir,
Notice that in his second example he did not try to define amethod in the subclass as this is what is illegal. You cannot override(or redefine) a final method that was defined in the super class.

HTH,

Jason is right - it's the declaration of amethod in the subclass that is illegal - not the call to that method. If this is still confusing, please let me know and I'll try to elaborate.
Corey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final being overridden??
 
Similar Threads
How is this??
Modifiers Help?
final class
Doubt in Mock Question
Private modifier and classes