Java

  1. Home
  2. Computing & Technology
  3. Java
photo of Paul Leahy

Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Object-Oriented Programming: Preventing Inheritance

Thursday January 8, 2009

Most classes will never be extended by another class. After all, inheritance is only useful when the right relationship between two classes exists and extending the functionality of a class is beneficial.

However, if a class you design might be used by other programmers it's important to consider the implications of any subclasses they create. It might the case that you do not want your class to be extended by another. Find out how to prevent your classes from being inherited.

Comments

January 10, 2009 at 3:18 pm
(1) Kirk says:

Inheritance is a funny thing. On the one hand it is something to be avoided. On the other hand, it is very very useful.

Now, why this advice is questionable and why String shouldn’t have been declared final.

One should normally shouldn’t extend a class because most likely there isn’t a proper “is-a” or “kind-of” relationship. For example, most thread extensions that I’ve seen do not represent a specialization of a thread. They are better represented by Runnable, something you want a normal thread to do. However, when there is a specialization, preventing inheritance to express that relationship foces one to write ugly hacks. The biggest ugly hack that I often see is a static class called StringUtils.

This class is there because there are some useful extensions that are simply prevented by some developer that in the case of Strings, didn’t believe that other programmers would understand and properly be able to extend String. I say horse noggies to that! There are many useful extensions that could be applied to String. For example, UTF-8String or some other specialization.

Refactorings that result in an implosion of a code base is a clear sign that things are getting better (in terms of organization). Just think of all the code that goes away if we could only remove the final keyword from the String class declaration.

While I’m not against the use of final, it needs to be used sparingly IMHO. The consequences of adding final to a class declaration just to mother other developers can end up causing more harm then help.

Kirk

January 12, 2009 at 4:54 pm
(2) Paul Leahy says:

Interesting comment, Kirk. Yep, I agree inheritance is tricky and deciding when to prevent inheritance isn’t always clear. I can understand the desire to make String a final class as it is so fundamental to the Java language and is immutable. You would have preferred it so that the class wasn’t final but all the critical behaviors were?

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss

Community Forum

Explore Java

About.com Special Features

Java

  1. Home
  2. Computing & Technology
  3. Java

©2009 About.com, a part of The New York Times Company.

All rights reserved.