permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt over final overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt over final overriding" Watch "doubt over final overriding" New topic

doubt over final overriding

shilpa gupta

Joined: Feb 09, 2001
Posts: 26
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
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
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!!
Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 642
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 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...?

All desirable things in life are either illegal, banned, expensive or married to someone else !!!
shashank hiwarkar1

Joined: Feb 11, 2001
Posts: 25
hi prafful,

Thanx for the nice explaination.

shilpa gupta

Joined: Feb 09, 2001
Posts: 26
Praful & Tualha
thanx a lot forthe trouble you took
that was a beautiful exp praful
I agree. Here's the link:
subject: doubt over final overriding
It's not a secret anymore!