Why should a class be a final class? can some one please give real life example where a class needs to be final to serve it's purpose in object oriented design.
Thank you.
Printable View
Why should a class be a final class? can some one please give real life example where a class needs to be final to serve it's purpose in object oriented design.
Thank you.
Hello, roots.
I think you add the final modifier when you completed a class and you know that there will be no need for it to be the super class of some other class. For example: Let's say Planet is a subclass of Body and Earth is a subclass of Planet, but you do not get different types if Earths as you get different types of planets. Thus, the Earth class must be declared final. I think the purpose of this is to improve performance.
Hope this helps. :)
Or if you want to create a class that no one can extends due to the critical functionality in the class. A String class is an example of final class in the Java API.
I had a long standing doubt on final classes. May be more people can talk about their views on using final classes for themselves. Thank you tim again "you do not get different types if Earths" this is the kind of view i was talking about. Thank you wsaryada for your response.