How can we restrict inheritance for a class?

Started by sinelogixtech, November 21, 2017, 05:25:53 AM


12Three

   

1. Use final.
    2. Use private constructors.
    3. Use a comment: // do not inherit.
    4. Use a javadoc comment.
    5. Make every method final, so people can't override them.
    6. Use a runtime check in the class constructor: if (this. getClass() != MyClass. class) { throw new RuntimeException("Subclasses not allowed"); }