Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we override a constructor in Java and can a constructor be private?

Tags:

java

I would appreciate an explanation for these questions:

  1. Can we Override a constructor in Java?
  2. Can a Constructor be private?
like image 696
S.Ganesh Avatar asked Mar 25 '11 09:03

S.Ganesh


People also ask

Can a constructor be private Java?

A private constructor in Java is used in restricting object creation. It is a special instance constructor used in static member-only classes. If a constructor is declared as private, then its objects are only accessible from within the declared class. You cannot access its objects from outside the constructor class.

Can private constructor be overloaded in Java?

Yes! Java supports constructor overloading. In constructor loading, we create multiple constructors with the same name but with different parameters types or with different no of parameters.

How do you override a constructor in Java?

Constructor Overriding is never possible in Java. This is because, Constructor looks like a method but name should be as class name and no return value. Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding.

How do you make a constructor private in Java?

Java allows us to declare a constructor as private. We can declare a constructor private by using the private access specifier. Note that if a constructor is declared private, we are not able to create an object of the class. Instead, we can use this private constructor in Singleton Design Pattern.


2 Answers

No, you can't override a constructor. They're not inherited. However, each subclass constructor has to chain either to another constructor within the subclass or to a constructor in the superclass. So for example:

public class Superclass
{
    public Superclass(int x) {}

    public Superclass(String y) {}
}

public class Subclass extends Superclass
{
    public Subclass()
    {
        super(5); // chain to Superclass(int) constructor
    }
}

The implication of constructors not being inherited is that you can't do this:

// Invalid
Subclass x = new Subclass("hello");

As for your second question, yes, a constructor can be private. It can still be called within the class, or any enclosing class. This is common for things like singletons:

public class Singleton
{
    private static final Singleton instance = new Singleton();

    private Singleton()
    {
        // Prevent instantiation from the outside world (assuming this isn't
        // a nested class)
    }

    public static Singleton getInstance() {
        return instance;
    }
}

Private constructors are also used to prevent any instantiation, if you have a utility class which just has static methods.

like image 53
Jon Skeet Avatar answered Oct 27 '22 00:10

Jon Skeet


Constructor is meant for a class. It cant be overridden under any circumstances. Its like wanting to change Ferrari's factory from BMW's factory (which isn't practical). Surely you can overload to get the functionality you need.

Yes Constructor can be private. By making it private you are not letting the outside world to create an object of it directly through constructor, But singleton pattern uses a public static method to call the constructor of the class and object can be created.

like image 33
user2626445 Avatar answered Oct 27 '22 00:10

user2626445