Webmaster Forums - Website and SEO Help

Web Development and Design => Database, Server, Coding and Website Administration => Topic started by: sinelogixtech on November 21, 2017, 05:25:53 AM

Title: How can we restrict inheritance for a class?
Post by: sinelogixtech on November 21, 2017, 05:25:53 AM
How can we restrict inheritance for a class?
Title: Re: How can we restrict inheritance for a class?
Post by: 12Three on February 28, 2019, 11:16:33 PM
   

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"); }