-4

What is the best practice on creating new non abstract classes?

In addition to that should all variables be final unless you need to change them?

Does it make sense to mark methods final in final classes?

Andreas
  • 154,647
  • 11
  • 152
  • 247
Someone
  • 560
  • 9
  • 21
  • 1
    I think it should not. You can read more detail on [this link](http://stackoverflow.com/questions/5181578/use-of-final-class-in-java) – Hoai-Thu Vuong Feb 24 '17 at 08:26
  • 3
    "Does it make sense to mark methods final in final classes?" Not really, as the class itself can't be extended. [This related question](http://stackoverflow.com/questions/3182440/benefits-of-using-an-abstract-classes-vs-regular-class) might be of some use. – Salem Feb 24 '17 at 08:26
  • 3
    @ruakh You totally changed the question to something entirely different. Undo edit!! – Andreas Feb 24 '17 at 08:36
  • 2
    A better [link](http://stackoverflow.com/a/8766485/5221149) for answering the question *"Does it make sense to mark methods final in final classes?"*. Basically, answer is "no", since they are implicitly `final` anyway. – Andreas Feb 24 '17 at 08:39
  • @Andreas: I added the question from the title into the question body; I deleted the second paragraph because it was a completely different question; and I didn't actually explicitly delete the third paragraph, but since it didn't exist when I started editing, my edit implicitly rolled back its addition. Can you clarify which of those changes you are objecting to? – ruakh Feb 24 '17 at 09:46
  • @ruakh The original question is asking about three things: It's asking about final *classes*, final *variables*, and final *methods*. You removed two of the three questions, i.e. change the question text fundamentally. Given that there were already comments/answers for the parts you removed, that is wrong, in my opinion (and others it seems). If you want to address the situation that a question should only ask one thing, do that as a comment. Don't mess up the question. I'm rejecting your edit as *"clearly conflicts with author's intent"*. – Andreas Feb 24 '17 at 18:57
  • @Andreas: The original question actually *didn't* ask about final methods; the OP added that during the five-minute grace period. Both answers are only about final classes, and the OP accepted one of them, so you are clearly mistaken about the "author's intent". And lastly -- I think it's foolish to edit a question in such a way that makes it inappropriate for this site. If you really think that this question is "intended" to include three unrelated questions, then you should just downvote it and move on. – ruakh Feb 24 '17 at 21:11

1 Answers1

5

Very good article of @yegor256: Seven Virtues of a Good Object

Point 7. His Class Is Either Final or Abstract

A good object comes from either a final or abstract class. A final class is one that can't be extended via inheritance. An abstract class is one that can't have instances. Simply put, a class should either say, "You can never break me; I'm a black box for you" or "I'm broken already; fix me first and then use."

I find the Yegor's approach is very useful in my daily work.

Skeets
  • 4,476
  • 2
  • 41
  • 67
eugene-nikolaev
  • 1,290
  • 1
  • 13
  • 21