How to avoid subclass without use of final Keyword in Java -


final class abstest {}  class btest extends abstest {} 

how prevent creation of subclass(es) without using final keyword?

here few options

  • create private constructor
  • make each method final, people can't override them. avoid accidental calling of methods subclass way. doesn't stop subclassing though.
  • put check constructor class:

    if (this.getclass() != abc.class) { throw new runtimeexception("subclasses not allowed"); }

but final is provided solve such problem must say, so better go with final!!!


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -