-3

Does it make sense to use two constructors with different access modifiers. What is the exact use? For Eg :

public class Apple {

    int count;

    public Apple()
    {

    }

    private Apple(int count)
    {
        this.count = count;
    }

    public void count()
    {
        System.out.println("Apple count is" + count); 
    }
}

With either one of constructor we can access everything from the class right

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • From the class yes, but access modifiers are all about controlling what the users of the class see. – Tunaki Jun 20 '16 at 10:05
  • This can very well make sense, but it makes no difference from *within* the class. See the first column in [this table](http://stackoverflow.com/a/33627846/276052). If want to prevent other classes from using the `Apple(int count)` constructor and instead make sure they use the no-arg constructor, this approach would make perfect sense. – aioobe Jun 20 '16 at 10:09

3 Answers3

2

Not really.

For instance in this case, you can't control what an Apple instance's count is (from anywhere beyond the class itself), because the constructor injecting a count value is private, and the count field itself has default access.

Mena
  • 47,782
  • 11
  • 87
  • 106
0

What is the exact use?

Java as many others OOP languages allows you the overloading, this is not other than giving you the freedom to define many Methods/Constructors using different parameters, so you can in a very flexible way construct or prepare the objects depending of what the user gives for input/ what the user needs...

sometime it will be just calling internally methods inside, hiding to the user how the magic inside the objects works...

Example

check how a use can construct an Apple by doing something like

Apple ap = new Apple(1);

but maybe the user doesnt need/want to pass the count immediately

so he can use

Apple ap2 = new Apple();

the trick is inside in the default constructor(constructor with no params) because as you see that constructor is calling himself and initializing the apple, but with count=0;

int count;

public Apple()
{
    this(0);
}

private Apple(int count)
{
    this.count = count;
}
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

The private constructor can be call by the public constructor. It's useful if you want to do the same treatment in each of your constructor, but no want to allow to construct with the treatment only. By ex:

class Vehicule{

  private Vehicule(String name){
   this.name=name;
  }

  public Vehicule(String name, Motor motor, GazType gazType){
    this(name);
    this.motor=motor;
    this.gazType=gazType;
  }
  public Vehicule(String name,SolarPanel solarPanel){
    this(name);
    this.solarPanel = solarPanel;
  }
 public Vehicule(String name, int numberOfCyclist){
    this(name);
    this.numberOfCyclist=numberOfCyclist;
  }

{

      Vehicule car = new Vehicule("ford", engine, gaz);//OK
      Vehicule tandem = new Vehicule("2wheel2people", 2);//OK
      Vehicule sailboard = new Vehicule("blueWind", blueSail);//OK
      Vehicule madMaxCar = new Vehicule("Interceptor", v8Engine, nitroglicerine);//OK

      Vehicule vehicule=new Vehicule("justeAname")//Compilation Error
    }
    }

You can use the private constructor in static factory too.

sab
  • 4,352
  • 7
  • 36
  • 60