7

I would appreciate an explanation for these questions:

  1. Can we Override a constructor in Java?
  2. Can a Constructor be private?
Gowtham Kumar
  • 534
  • 8
  • 22
S.Ganesh
  • 81
  • 1
  • 2
  • 9

11 Answers11

26

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • but One friend Said lik:"when you write your own constructor you overide the default one given by Java" It's correct or not – S.Ganesh Mar 25 '11 at 09:31
  • If your utility class had only static methods, why would you need a constructor (private or otherwise)? Would declaring it `final` not prevent anyone from extending it using the implicit no-arg constructor? – Scott Mar 25 '11 at 09:33
  • Ganesh, in your question in the comment, that's a different meaning of the word "override" – Yoni Mar 25 '11 at 09:34
  • 3
    @Scott: If you don't provide a private constructor, the compiler will create a parameterless one for you, and other people could call that. It's not just about preventing *derivation* - it's about preventing *instantiation*. – Jon Skeet Mar 25 '11 at 09:37
  • 2
    @Ganesh, It's actually the other way around: The compiler automatically provides a no-argument, default constructor for any class without constructors. Reference: http://download.oracle.com/javase/tutorial/java/javaOO/constructors.html – bdhar Mar 25 '11 at 09:38
4

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.

user2626445
  • 1,031
  • 12
  • 27
3
  1. No we can't use a constructor out of class because sub class is treat constructor as a method.. without return type.
  2. You can use it as a private but if a constructor of a class is private then you cannot make the obj of the respected class into another class.
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
2

You can use callback:

In parent class:

protected void onCreate(){}

SomeClass(){
//Constructor code
onCreate()
}

in child:

@Override
protected void onCreate(){
//Child constructor code
}
user1319976
  • 21
  • 1
  • 1
  • 3
2

1) NO! A constructor belongs to the class in which it is declared. A sub class is a different class and must have its own constructor. So, constructors simply can't be overridden.

2) Yes, that's done usually in case of singletons.

adarshr
  • 61,315
  • 23
  • 138
  • 167
1

no we cannt override an construtor, For implementing Singleton pattren we should have a private construtor.

developer
  • 9,116
  • 29
  • 91
  • 150
1

try this : http://www.javabeginner.com/learn-java/java-constructors 1 -> No 2 -> yes

hkairi
  • 385
  • 2
  • 9
1

1) Is this just homework question, or do you try to reach something? Can you show what you try to reach with an overriding constructor?

Since the parent constructor is called first, you may modify the base class to your needs in your constructor. Of course, just as far as the access to base attributes isn't private. If you extend a class but don't like their might-be-private attributes, deriving from it was an error.

2) Can a constructor be private?

Yes, but do you know what it is good for?

user unknown
  • 35,537
  • 11
  • 75
  • 121
1
  1. In a derived class you can create a new constructor with the same signature but that is not really overriding since, when initializing the class, the parent class's constructor is still called before the new one.

  2. a class's constructor can be private or protected and of course be public. but if it is protected or private how would you initiate the class? ( actually you could with a static function in that class...)

kroiz
  • 1,722
  • 1
  • 27
  • 43
0

Both are not true ,as when a class inherit other class it automatically calls its parents class ,so it makes sense that we can't override and make them final .

saurabh kumar
  • 155
  • 5
  • 26
0

You can not override a constructor, however you can make them any access level modifier such as public, private or default. You would want a private constructor for things like a singleton or for a class that's mostly made of static methods and such (i.e Java's Math class)

Nelle
  • 11
  • 1