What are good reasons to prohibit inheritance in Java, for example by using final classes or classes using a single, private parameterless constructor? What are good reasons of making a method final?
-
1Pedantry: The default constructor is the one generated for you by the Java compiler if you don't explicitly write one in your code. You mean the no argument constructor.– John TopleyOct 20, 2008 at 15:21
-
Isn't that the "implicit default constructor"?– cretzelOct 20, 2008 at 15:28
-
2"You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors." java.sun.com/docs/books/tutorial/java/javaOO/constructors.html – John Topley– John TopleyOct 20, 2008 at 15:37
11 Answers
Your best reference here is Item 19 of Joshua Bloch's excellent book "Effective Java", called "Design and document for inheritance or else prohibit it". (It's item 17 in the second edition and item 15 in the first edition.) You should really read it, but I'll summarize.
The interaction of inherited classes with their parents can be surprising and unpredictable if the ancestor wasn't designed to be inherited from.
Classes should therefore come in two kinds:
Classes designed to be **extended**, and with enough documentation to describe how it should be done
Classes marked **final**
If you are writing purely internal code, this may be a bit of overkill. However, the extra effort involved in adding five characters to a class file is very small. If you are writing only for internal consumption, then a future coder can always remove the 'final' - you can think of it as a warning saying "this class was not designed with inheritance in mind".
-
6In my experience this is not overkill if anyone besides me will be using the code. I have never understood why Java has methods overridable by default. Oct 20, 2008 at 15:36
-
11To understand some of Effective Java, you need to understand Josh's take on design. He says [something like] you should always design class interfaces as if they were a public API. I think the majority opinion is that this approach is usually too heavy and inflexible. (YAGNI, etc.) Oct 20, 2008 at 15:44
-
10Good answer. (I can't resist pointing out that it's six characters, as there's also a space... ;-) Feb 8, 2009 at 13:18
-
46Please note, that making classes final might make testing harder (harder to stub or mock)– notnoopDec 16, 2009 at 22:59
-
11If the final class is writing to an interface, then mocking should not be a problem. May 18, 2010 at 16:29
You might want to make a method final so that overriding classes can't change behavior that is counted on in other methods. Methods called in constructors are often declared final so you don't get any unpleasant surprises when creating objects.
-
Didn't quite understand this answer. If you don't mind, could you explain what you mean by "overriding classes can't change behaviour that is counted on in other methods"? Am asking firstly because I didn't understand the sentence, and secondly because Netbeans is recommending that one of my functions I'm calling from the constructor, should be
final
.– NavFeb 25, 2016 at 8:11 -
1@Nav If you make a method final, then an overriding class won't be able to have its own version of that method (or it will be a compile error). That way you know that the behavior you implement in that method won't change later when someone else extends the class. Feb 25, 2016 at 12:11
-
C# will warn you if you call virtual methods from a constructor. Methods called in constructors sort of mirrors this. Nov 7, 2021 at 19:23
One reason to make a class final would be if you wanted to force composition over inheritance. This is generally desirable in avoiding tight coupling between classes.
There are 3 use cases where you can go for final methods.
- To avoid derived class from overriding a particular base class functionality.
- This is for security purpose, where base class is giving some important core functionality of the framework where derived class is not supposed to change it.
- Final methods are faster than instance methods, as there is no use of virtual table concept for final and private methods. So where ever there is a possibility, try to use final methods.
Purpose for making a class final:
So that no body can extend those classes and change their behavior.
Eg: Wrapper class Integer is a final class. If that class is not final, then any one can extend Integer into his own class and change the basic behavior of integer class. To avoid this, java made all wrapper classes as final classes.
-
Not very much convinced with your example. If that is the case, then why not make the methods and variables final instead of making the whole class final. Can you please edit your answer with the details.– RajkiranJan 20, 2015 at 18:10
-
4Even if you make all the variables and methods as final, still others can inherit your class and add extra functionality and change the basic behavior of your class. May 7, 2015 at 9:18
-
7> If that class is not final, then any one can extend Integer into his own class and change the basic behavior of integer class. Let's say this was allowed and this new class was called
DerivedInteger
, it still doesn't change the original Integer class, and whoever usesDerivedInteger
does so at their own risk, so I still don't get why it's a problem. Jul 5, 2017 at 22:03
you might want to make immutable objects (http://en.wikipedia.org/wiki/Immutable_object), you might want to create a singleton (http://en.wikipedia.org/wiki/Singleton_pattern), or you might want to prevent someone from overriding the method for reasons of efficiency, safety, or security.
Inheritance is like a chainsaw - very powerful, but awful in the wrong hands. Either you design a class to be inherited from (which can limit flexibility and take a lot longer) or you should prohibit it.
See Effective Java 2nd edition items 16 and 17, or my blog post "Inheritance Tax".
-
1The link to the blog post is broken. Also, do you think you could elaborate a bit more on this?– user289086Sep 22, 2014 at 5:27
-
@MichaelT: Fixed the link, thanks - follow it for more elaboration :) (I don't have the time to add to this right now, I'm afraid.) Sep 22, 2014 at 5:46
-
-
@Kedarnath: It works for me... it was broken for a while, but it's now pointing to the correct page, on codeblog.jonskeet.uk... Mar 27, 2015 at 6:44
Hmmm... I can think of two things:
You might have a class that deals with certain security issues. By subclassing it and feeding your system the subclassed version of it, an attacker can circumvent security restrictions. E.g. your application might support plugins and if a plugin can just subclass your security relevant classes, it can use this trick to somehow smuggle a subclassed version of it into place. However, this is rather something Sun has to deal with regarding applets and the like, maybe not such a realistic case.
A much more realistic one is to avoid an object becomes mutable. E.g. since Strings are immutable, your code can safely keep references to it
String blah = someOtherString;
instead of copying the string first. However, if you can subclass String, you can add methods to it that allow the string value to be modified, now no code can rely anymore that the string will stay the same if it just copies the string as above, instead it must duplicate the string.
To stop people from doing things that could confuse themselves and others. Imagine a physics library where you have some defined constants or calculations. Without using the final keyword, someone could come along and redefine basic calculations or constants that should NEVER change.
-
4There's something I don't understand about that argument - if someone did change those calculations/constants that shouldn't change - aren't any failures due to that 100% their fault? In other words, how does the change benefit anything?– matt bOct 20, 2008 at 15:18
-
Yes, it's technically "their" fault but imagine someone else using the library that's not been made aware of the changes, could lead to confusion. I have always thought that that was the reason many of the Java Base classes were marked final, to stop people form altering basic functionality. Oct 20, 2008 at 15:28
-
@matt b - You seem to be assuming that the developer who made the change did so knowingly or that they would care that it was their fault. If someone besides me will be using my code, I will mark it final unless it is meant to be changed. Oct 20, 2008 at 15:33
-
This is actually a different usage of 'final' from the one asked about. Oct 20, 2008 at 17:53
-
This answer seems to confuse inheritance and mutability of individual fields with the ability to write a subclass. You can mark individual fields and methods as final (preventing their redefinition) without prohibiting inheritance. Feb 8, 2009 at 13:25
Also, if you are writing a commercial closed source class, you might not want people to be able to change the functionality down the line, especially if u need to give support for it and people have overridden your method and are complaining that calling it gives unexpected results.
If you mark classes and methods as final, you may notice a small performance gain, since the runtime doesn't have to look up the right class method to invoke for a given object. Non-final methods are marked as virtual so that they can be properly extended if needed, final methods can be directly linked or compiled inline in the class.
-
2I believe this is a myth, since current generation VMs should be able to optimize and deoptimize as needed. I remember seeing some bencharmking this and classifying it as a myth. Oct 20, 2008 at 17:53
-
1The difference in code generation is not a myth, but maybe the performance gains are. As I said its a small gain, one site mentioned a 3.5% performance gain, some higher, which in most cases is not worth going all Nazi on your code. Oct 21, 2008 at 1:07
-
1It is not a myth. In fact, the compiler can only inline V final methods. I am not sure if any JITs "inline" things art runtime but I doubt that it can since you may be loading a derived class later.– UriNov 1, 2008 at 17:51
-
1Microbenchmark==grain-of-salt. That said, after 10B cycle warmup, avg over 10B calls shows essentially no difference under Eclipse on my laptop. Two methods returning String, final was 6.9643us, non-final was 6.9641us. That delta is background noise, IMHO. Feb 8, 2009 at 14:17
You want to make a method final so that overriding classes does not change its behavior. When you want to be able to change the behavior make the method public. When you override a public method it can be changed.