1

I had a question in one of my interview:

Let's say I have a classA and I should create object of classA in only classA not in any other class, for example:

classA{
// some instructions

ClassA a = new ClassA();
}

ClassB{
//Some instructions

ClassA a1 = new ClassA(); // Here I should throw a compilation or runtime error.
}

I told by adding private constructor, but he told that we should have a capability of creating more than once object with in the same class. Can I make this?

user8579908
  • 23
  • 1
  • 7
  • 3
    Make the constructor private. – OH GOD SPIDERS Oct 24 '17 at 13:30
  • 2
    Note that compilation errors are *vastly* preferable to runtime errors. – Andy Turner Oct 24 '17 at 13:33
  • I told the same, but the interviewer told that, we should have ability to create more than one object in same class. – user8579908 Oct 24 '17 at 13:33
  • @user8579908 then your interviewer doesn't know what he is talking about. Making a constructor private has nothing to do with the number of instances that can be created. – Andy Turner Oct 24 '17 at 13:34
  • @AndyTurner Either that or it is a trick question to see if OP knows what he is talking about and "private constructor" is not just a wild guess :D. – Tom Oct 24 '17 at 13:35
  • 2
    @user8579908 "I told like make the class as a singleton class by adding private constructor" Ah, well, there's the source of the confusion: *you* introduced the word singleton here; whilst private constructors are necessary for (non-enum) singletons, they are used in other circumstances. – Andy Turner Oct 24 '17 at 13:38

3 Answers3

6

Make its constructor private:

private classA() {}

This forbids to invoke it anywhere else.
E.g. private constructors are used for Utility classes or Static factory
UPDATE: As @Arkadiy noticed it doesn't limit amount of instances you can create.

Russiancold
  • 952
  • 1
  • 7
  • 20
  • I told the same, but the interviewer told that, we should have ability to create more than one object in same class. – user8579908 Oct 24 '17 at 13:33
  • So edit your question properly. – Russiancold Oct 24 '17 at 13:34
  • 3
    private constructor does not preclude more than one instance. The interviewer was mixing up private constructor with the singleton pattern. Private constructor is used to implement singleton pattern, but can be used in other circumstances. –  Oct 24 '17 at 13:36
  • Maybe it was the Static Factory pattern required by interviewer? That pretty much only allows creation of the object inside the class, BUT it also allows access to the method that does the task from outside. Not the easiest question for a junior programmer I guess. – Vlasec Oct 24 '17 at 13:42
  • 1
    @Vlasec I guess we shouldn't divine what interviewer said, since OP more likely has misunderstood him. – Russiancold Oct 24 '17 at 13:45
  • Yep, if what OP said was true and exact, the interviewer would be an incompetent person. It's more likely that OP is just a bit too green to get it. – Vlasec Oct 24 '17 at 13:52
  • By the way, private constructors are used in more cases than you mentioned, and in utility classes it is only to prevent instantiation completely :) – Vlasec Oct 24 '17 at 13:56
  • @Vlasec unfortunately nobody will know. btw It's far not the first question like this, where everebody are so interested what interviewer has asked, but OP just intrigued everybody and can't explain correctly. – Russiancold Oct 24 '17 at 13:57
  • @Vlasec right. Obviously I just meant that in these examples private constructors are used as a part of entire realization. – Russiancold Oct 24 '17 at 14:02
0

You can make it happen by using private access modifier to constructor of the class,

private access modifier only let private things access with in it own class.

Following is table of access modifiers and their access scopes enter image description here

Nisal Edu
  • 5,371
  • 3
  • 22
  • 31
0
  1. make the constructor private
  2. follow a factory method or factory pattern
Ashok Kumar N
  • 523
  • 6
  • 21