About: Google Summer of Code Intern @ TEAMMATES, National University of Singapore. Open Source Community Member at DuckDuckGo.
Thanks, @bertilmuth .
As you said final keyword in java means "can‘t be modified", but still we should be allowed to extend it from another class so that we can use its defined method.
I totally agree that if we try to modify any of its methods then it should give an error, but otherwise we should be allowed to extend that final class and reuse its method in another class.
Please correct me if I am wrong anywhere.
As Kasey Speakman has pointed out, you can extend final classes by composition. Just not inheritance. A class can offer all visible methods of the final class, plus further methods.
Applied to a field/variable it means: it‘s a constant (the value can’t be modified.)
To be more correct: the variable assignment cannot be modified. Only in case of primitives and immutable objects the value cannot be modified.
final String[] foo = String[] { "bar" };
foo[0] = "quux"; // this is legal.
The creator of the class explicitly marked it that way because they did not want you to extend it. Probably to avoid having access to its protected members. But I can only guess as to their intentions. Generally, you have to use such a class with composition (create one and put it in a private field in your own class) rather than inheritance.
First of all, a final class can make sense. Imagine what would happen if somebody were to extend String, Integer or any of the other builtin classes - the JVM would be unable to perform a lot of optimizations. Sometimes, the programmer just didn't want to allow inheritance for architectural reasons. Granted, it's a rare occasion, but the possibility is there if you need it.
WARNING: VERY ADVANCED STUFF AHEAD
That being said, there actually is a possibility to inherit from a final class. This is needed mostly for internal purposes in frameworks like Spring of Hibernate. Using reflection, the final modifier of a class can be disabled at runtime, and the byte code of a class can be generated which extends the formerly final class. Don't try this at home.
I wrote a post with code examples that will hopefully explain this a bit more concretely.
The final
keyword is much more related to the intent behind that class than any physical restriction from Java or the JVM. As others have mentioned, it is technically possible to extend a final class, but it is difficult and dangerous to do so.
In Java, a final class is an opt-in feature, meaning the developer chose to make this class final. They looked at the class and the way it is intended to be used, and decided it makes more sense for it to not be extended. There are several reasons why they might have made this choice:
Fragment
instance, but our application logic requires an instance of FragmentProvider
(a custom class), not Fragment
. This FragmentProvider
class is final, and contains methods for loading/unloading fragments and tying them into our custom application lifecycle, which is functionality built on top of the normal ViewPager. Our ViewPager adapter only accepts a list of FragmentProvider
as input, so there is only one way to add a new Fragment to do it, and that one way is perfectly in-line with the original developer's intention.Context
base class in Android is a god-class that knows entirely too much, and is fragmented across many different implementations through a deep and confusing hierarchy. An Application
is a Context
. An Activity
is a ContextThemeWrapper
which is a ContextWrapper
which is a Context
. And supporting older versions of android, an AppCompatActivity
has 9 extended classes before reaching Context
. This makes it confusing to reason about which code is actually being run, and in what order, and it would have probably been a better idea to have made just a few Context
-type classes (just Context, Application, and Activity) and have the rest of the needed behavior implemented through composition. Because it's final.
Use composition instead of inheritance if you want to "extend" a final class.
See the Decorator Pattern
The final keyword in Java means: „can‘t be modified“. Applied to a field/variable it means: it‘s a constant (the value can’t be modified.) Applied to a class, it means: class can’t be extended (and thus, modified).