Pages

Jun 20, 2015

3 ways to prevent a class from being subclassed in Java

  1. Access control: Even though a class can't be marked private, a class can be non-public (by not declaring it as public). A non-public class can be subclassed only by classes in the same package as the class.
  2. final keyword: A final class means that it's the end of the inheritance line.
  3. If a class has only private constructors. Ex. like built-in Math class in Java.

No comments:

Post a Comment