Yahoo Interview Question
Software Engineer / DevelopersKeeping the constructor private means that the class cannot be inherited by others.
The same can be achieved by final keyword on the class, but making constructor private puts more restriction as you can create class on heap from outside the class.
using this one can make class Singleton as well, by providing getInstance() like method.
All of the above comments are partially wrong. In such case, the parent class can indeed be inherited and can be instantiated too. Refer to the below example
package com;
class parent{
parent(){System.out.println("parent cons. called");}
parent(String msg)
{
System.out.println("non private constructior");
}
}
class child extends parent{
child(){super("v");}
}
public class General {
public static void main(String args[]){
parent c=new parent();
child d=new child();
}
}
@Anon above: Wrong . you can't create an object like this...parent c=new parent();
your constructor is not private...!!! . Make it private then you can't create an instance of the the parent class. Other things are right.
All the above answers are wrong.
Only final class cannot be extended.
Having private constructor means
1-We cannot create an object of that class outside.
2-We cannot class call super() from a subclass.
class A{
private A(){ //1-We cannot create an object of class A outside. 2-We cannot call super() in subclass
}
public void show(){
System.out.println("Hello");
}
}
//A is not final hence can be extended
public class B extends A{
public static void main(String args[])
B obj= new B();
obj.show();//NO PROBLEM
}
All the above answers are wrong.
Only final class cannot be extended.
Having private constructor means
1-We cannot create an object of that class outside.
2-We cannot class call super() from a subclass.
class A{
private A(){ //1-We cannot create an object of class A outside. 2-We cannot call super() in subclass
}
public void show(){
System.out.println("Hello");
}
}
//A is not final hence can be extended
public class B extends A{
public static void main(String args[])
B obj= new B();
obj.show();//NO PROBLEM
}
All the above answers are wrong.
Only final class cannot be extended.
Having private constructor means
1-We cannot create an object of that class outside.
2-We cannot class call super() from a subclass.
class A{
private A(){ //1-We cannot create an object of class A outside. 2-We cannot call super() in subclass
}
public void show(){
System.out.println("Hello");
}
}
//A is not final hence can be extended
public class B extends A{
public static void main(String args[])
B obj= new B();
obj.show();//NO PROBLEM
}
Declaring the constructor private will ensure that no one outside of the class can directly instantiate the class. In this case, the only way to create an instance of the class is by providing a static public method, as is done when using the Factory Method Pattern.
Additionally, because the constructor is private, the class also cannot be inherited.
used for creating singletons
- vivek May 04, 2009