Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When would you use Final with Protected?

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
From what I think I can see, javax.swing.text.DefaultStyledDocument inherits two methods:

final protected writeLock()
final protected writeUnlock()

I would like to make use of these methods. I cannot call them from another class because they are protected, and I cannot override them because they are final.

When would you use the same combination of final and protected?

I am also curious to know how one can gain control of the writeLock [bugeyed]

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
protected final means subclasses can call it but not modify, ie, call it "as is".

Cheers,
Dian
 
I did realise this after posting (-:

Isn't it very anti-OOP to force uses "as is" and also very anti-iterative development processes?

Now my original post is redundant, but for arguments sake, lets assume one of us really did want to override that method in that class... how far up the tree would we need to go and how many classes would we need to replace?

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Then use composition instead of inheritence.

I think if you use final protected (public, private, whatever) it allows the compiler to make certain optimizations that it wouldn't be able to make otherwise.
 
Only JIT compilers can take some advantage of final methods, I don't think it's a good idea to use final for perfomance reasons.

Btw, private methods cannot be overriden and they're a good OO practice.

Cheers,
Dian
 
> You can't override that, if you actually need to replace
> that method, you should use another one with another
> name.

Other inherited methods will call the originally named method, so wouldn't you end up replacing many methods?

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
It it's final and protected, that means the original developer wanted to be sure that all the methods called the original one.

The OOP power is not only in what you show but also in what you hide.

Imagine for example a Java program for parental control. If you have a method control where age check is performed, you don't want that method to be overriden, changed or extended in any way, and you don't want other class to call any other method but the one you implemented.

Cheers,
Dian
 
I feel there is a difference in an application and an API. I would like the API to empower me. So we can agree to disagree :)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top