Yahoo Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

used for creating singletons

- vivek May 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

By keeping a constructor private, the class cannot be sub-classed.

- Badal Chowdhary September 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This forces the subclass to explicitly make a call to non private super class's constructor(if any,though the scope of the subclass matters),otherwise same as above(not allowing the parent class to subclass)

- Ashok October 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Object construction is entirely forbidden.
Object construction is private only, Objects can be constructed, but only internally.

- anonymouse October 20, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

By making Constructor private , the object creation is limited within the declared class. There is no way of creating object , using new operator in outside of the class.

- rk January 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keeping 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.

- MasterSolution June 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
}
}

- Anonymous December 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@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.

- master January 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you suck master. Read the sentence that you have written and see if it makes sense.

- Anonymous January 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried this, i see error

Implicit super constructor parent() is not visible for default constructor. Must define an explicit constructor

- Nilesh January 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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
}

- running July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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
}

- running July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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
}

- running July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- bicepjai September 11, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More