• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final being overridden??

 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic