Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About final class [duplicate]

Tags:

java

When we declare all variables and methods in a class as final, we are restricted the concept of override.

Then, "what is the reason we want to restrict IS-A relationship in an OOP language( even we restrict IS-A relationship, this language has Has-A relationship) by using final class?"

My point is we can not restrict the accessing the members of our class any way, then give an easy access by allowing IS-A relationship. Otherwise it is a headache to create objects and all? (in my view 'access' not includes modifications)

If I miss any important theme of programming , please excuse me. And tell me, what is it?

like image 610
M.V.S REDDY Avatar asked May 29 '26 03:05

M.V.S REDDY


1 Answers

Is there any necessity to declare a class as final class?

Necessity, no. Declare a class final if you want to forbid extending it. This can prevent programming errors. For example it's a recommended practice to make utility classes with only static methods to be final, as inheriting from such class wouldn't make sense, and be clearly a programming error.

like image 72
janos Avatar answered May 31 '26 15:05

janos