Goldman Sachs Interview Question for Software Engineer / Developers






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

In Java, constructors can be declared as private or protected, depending on the intended usage of the class.
If a constructor is declared as 'private' - then the class will prevent object creation. This could be required in a scenario where the class only wants static members available to any other class accessing it - you explicitly want to disallow object creation of that particular class.
Another scenario is - if you want to have only one object of that class present at any time - a singleton class.
Another scenario is - type-safe enums. All enums are implicitly public static final. Declaring a constructor for an enum class is meaningless in any case.
If a constructor is declared as protected, an object of that class can only be created by another class in the same package. This prevents instantiation outside the package.We can have protected constructors in abstract classes. The intention is to allow all the functionality of the base class to be inherited by the child/derived class - and prevent direct instantiation of the base class.

- Mallika Iyer January 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If a constructor is declared as protected, an object of that class can only be created by another class in the same package. This prevents instantiation outside the package.

This is wrong, protected is used for being accessed by classes in other packages, but only if they are sublcass.
Otherwise protected behave same as default.

- duskandawn February 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ duskandawn your theory applies to only accessing protected members but Mallika is right about object creation when constructor is protected.

- Survivor January 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can but you wont be able to initialize any of the objects

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

Its called singleton

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

You sure can have private constructor. By Making a constructor as private, you cannot instantiate objects from outside the class using new. But you can always provide a factory to create objects ( a method ) which can use new inside the class.

Also, by making the constructor as private, you cannot inherit this class as doing so would require the derived class constructor to call base class constructor which it cannot in this case

About protected constructors....am just guessing...i think it can also be done.

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

It sure can. The constructor can be either package (default), public, private or protected. Just like any other method.

- Abdul Malik Mansoor February 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If constructor is declared as public, you cannot create an object out of that class with that specific parameters.

class const1{
public static void main(String [] args)
{
const2 co = new const2();
}
}
class const2{
private const2(String str)
{
System.out.println("constructor is private");
}

public const2()
{
System.out.println("public constructor");
}
}

This will give output as public constructor. But if you try to use

const2 co = new const2("Try this");

It will give compiler error.

- Nishant February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private constructor - yes
protected constructor - no - how can you override a constructor? logical?

- dhrubo August 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry what is logical is not obvious always. just checked coding on JDK 1.6, protected is also allowed

- dhrubo August 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nunca Baixo App Daki Lixos

- Anonymous July 10, 2014 | 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