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
Post a Comment