This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt over final overriding

 
shilpa gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

i have two classes a super class and a subclass.
if i declare a method in super class and override it in subclass by a final method. i do not get any compile error.
but if i declare a final method in super class and try to override it by a method in the subclass i get the error message that final methods can not be over ridden
is it because the superclasses are compiled first and the subclasses next .
but then again i thought that classes were compiled in the same order as they were written
SOMEBODY PLEASE CLARIFY MY DOUBTS
thanx
shilpa
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
As far as I know, the keyword final makes a method NON- overridable (I don't know whether it's the correct word or not). Meaning that in a subclass you cannot override it but you can use the version provided by the super class. However, if your super class method is not declared with the keyword 'final', then your subclass override this method and alter it's functionality accordingly.
Finally, if you have a subclass of this subclass (in which you have overriden a super class method with a method declared as 'final'), then the lower most subclass in the heirarchy will be restricted from overriding the final method of this subclass.
Hope I did not confuse you!!
Bye,
Tualha Khan
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi shilpa,

The word(keyword) FINAL means
" Enough is Enough "
if any method,variable or class says that it is
final then they mean to say "Dont tuch me"
i.e u can use them but cant change there values
so if u defn a final method in super class can't override
it but if a non_final maethod is overriden & declare as final
then it is possible It's some thing like
Suppose u borrow a book from u r firend to read.
Then that book is final for u mean u cant Underline
the imp's u want (most of guys & girls throw an exception if we do so) so u can use it but cant change it.
Now suppose u got an Xerox copy of that book
so now u got the same book & now u can underline it or do what ever u want.But now if i ask u for that Xerox copy then
it will be final for me cause u wont allow me to do the same.
hope u get it
about the compilation of class
suppose i write
class sup extends super1
{
}
class super1
{
}
in this case if i compile the TrustMe.java file
containing above code. Then compiler will start
compiling sub class but as it extends super1 compiler should
know all details about super1 so it will first compile super1
& then return to sub
got it right...?
coolpraful@rediffmail.com


 
shashank hiwarkar1
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi prafful,

Thanx for the nice explaination.
 
shilpa gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praful & Tualha
thanx a lot forthe trouble you took
that was a beautiful exp praful
shilpa
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic