java - Are there good reasons for a public constructor of an abstract class -


it not possible create object directly calling constructor of abstract class. constructor of abstract class can called derived class. therefore seems me constructors of abstract class must either protected or package-private (the latter unusual cases of restricting use of constructor derived classes within package). yet java allows constructor of abstract class public.

are there circumstances in useful declare constructor of abstract class public, rather protected or package-private?

this not quite duplicate of question "abstract class constructor access modifier": can declare constructor public; want know whether there ever good reason so. seems me there not. see c# has similar peculiarity.

the answer same java:

there's no reason public constructor abstract class. i'd assume reason compiler doesn't complain simple didn't spend time covering since doesn't matter if it's public or protected. (source)

you can't call constructor of abstract class other direct subclass.

so adding special rule access modifiers of constructors of abstract classes wouldn't add useful language.


one thing looks exception rule - if abstract class defines default constructor, subclass not have implement constructor: legal:

public abstract class {   public a() {} }  public class b extends {} 

so can create b calling new b() - note, still create b , not a. and, again, doesn't matter if constructor in a public or protected. shouldn't private, compiler notice , complain...

actually invoke "invisible" public default constructor on b simple super() call...


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -