Constructors

1. Can Constructors be declared private?

You could declare the class as final. This means that the class cannot be sub-classed and therefore no additional constructors could be defined.

A great feature of Java is that a constructor must be called for every class. This is a good thing because it insures that everything gets initialized. There is no way to stop it. A constructor for each class must be called. If you disallow a constructor to be defined in a sub-class, you could not create a subclass, therefore this would be a final class.

Reference: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=33&t=023671

2. How about having 'protected' Constructors?

Question from JavaRanch:
my doubt is if constructors cannt be inherited why the protected access modifier allowed for constructors

Answer from Jesper Young is :

protected is used to specify from where you can access a constructor, method, variable etc. - it doesn't directly have anything to do with inheritance.

Making the constructor protected makes it accessible only by subclasses and classes in the same package. If all the constructors in a class are protected, that means that you can only create instances of the class from a subclass or from a class in the same package.

Reference : http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=042950

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.