1

I wanted to know why do we need final class in java... what could be the best answer for it...

I know we use final so that our class can't be inherited, but why we don't want our class to be inherited. Inheritence is used to increase code reusability... but we aren't implementing it in Final Class.

please somebody explain?

user2747831
  • 53
  • 1
  • 1
  • 6
  • The use of this is answered very well elsewhere, but consider what you would have to do if you needed to ensure a particular class wasn't extended if you *didn't* have final. For instance, if you needed to make sure that your `logPasswordUpdate()` function was never overridden to accidentally not log. Without `final` this would be virtually impossible to implement by hand. – Nathaniel Ford Sep 20 '13 at 16:29
  • `final class` will make it so no one can `extend` your class and then subsequently `override` your methods. The fact that it prevents `extends` from being used is enough to ensure no one can "mess" with your class. It can be used as a security measure (least privileged approach) or simply to make sure a process remains intact without outside intervention (someone subclassing your class and doing it wrong). – SnakeDoc Sep 20 '13 at 16:31
  • someone needs to ask OP why we have google... its for answering questions exactly like this. – user1231232141214124 Sep 20 '13 at 16:35
  • @redFIVE technically, google doesn't answer this kind of questions. You can search an answer out there, but then you can probably find a wrong explanation and then come to sites like SO to ask, then somebody will provide an accurate answer or what can be better, mark the post as a duplicate pointing to another Q/A in this site that covers this topic. – Luiggi Mendoza Sep 20 '13 at 16:45
  • I literally just googled "Why do we need final class in java?". Guess what came up?! Tons of sites answering this question. People need to learn to do a bit of research on their own instead of face rolling the most basic questions here with zero effort on their part. Hell, it even had links to the question already answered on this site. – user1231232141214124 Sep 20 '13 at 16:50

0 Answers0