class a
{
----
}
class b extends a
{
}
But my class a not inherited into b how
class a
{
----
}
class b extends a
{
}
But my class a not inherited into b how
What exactly is your question? How to prevent class a
from being subclassed, but without making class a
final
?
Make all constructors of class a
private
. Provide a factory method for creating instances of a
.
class a {
// Private constructor
private a() {
}
// Factory method
public static a createA() {
return new a();
}
}
Use following in your class constructor :
if (this.getClass() != A.class) {
throw new RuntimeException("Subclassing not allowed");
}
But using this way, you will restrict inheritance at run time.