patternjavaCritical
What is the point of "final class" in Java?
Viewed 0 times
finaljavaclassthepointwhat
Problem
I am reading a book about Java and it says that you can declare the whole class as
I am just new to programming and I am wondering if programmers actually use this on their programs. If they do, when do they use it so I can understand it better and know when to use it.
If Java is object oriented, and you declare a class
final. I cannot think of anything where I'd use this. I am just new to programming and I am wondering if programmers actually use this on their programs. If they do, when do they use it so I can understand it better and know when to use it.
If Java is object oriented, and you declare a class
final, doesn't it stop the idea of class having the characteristics of objects?Solution
If they do, when do they use it so I can understand it better and know when to use it.
A
(It does not mean that all references to objects of the class would act as if they were declared as
When it's useful to declare a class as final is covered in the answers of this question:
If Java is object oriented, and you declare a class
In some sense yes.
By marking a class as final you disable a powerful and flexible feature of the language for that part of the code. Some classes however, should not (and in certain cases can not) be designed to take subclassing into account in a good way. In these cases it makes sense to mark the class as final, even though it limits OOP. (Remember however that a final class can still extend another non-final class.)
A
final class is simply a class that can't be extended.(It does not mean that all references to objects of the class would act as if they were declared as
final.)When it's useful to declare a class as final is covered in the answers of this question:
- Good reasons to prohibit inheritance in Java?
If Java is object oriented, and you declare a class
final, doesn't it stop the idea of class having the characteristics of objects?In some sense yes.
By marking a class as final you disable a powerful and flexible feature of the language for that part of the code. Some classes however, should not (and in certain cases can not) be designed to take subclassing into account in a good way. In these cases it makes sense to mark the class as final, even though it limits OOP. (Remember however that a final class can still extend another non-final class.)
Context
Stack Overflow Q#5181578, score: 652
Revisions (0)
No revisions yet.