For example, there is a car class (sample code below), I can not figure out how to make a class that cannot be instantiated (directly), but you can use its functionality for the hierarchy.

public abstract class Car { protected String brand; protected String model; protected int price; protected int maxPassanger; protected int capacity; protected int velocity; protected int maxCarrying; protected double fuelConsumption; public abstract void startEngine(); public abstract void stopEngine(); } 
  • 3
    How will you use private constructor as a successor? - etki
  • I'm accustomed, so sorry for the stupid questions. - Shirak Arshakyan
  • lest I use privat, it seems I understood so - Shirak Arshakyan
  • Show example code where you want to use "private or abstract class"! - Mikhailov Valentine
  • public abstract class Car {protected String brand; protected String model; protected int price; protected int maxPassanger; protected int capacity; protected int velocity; protected int maxCarrying; protected double fuelConsumption; - Shirak Arshakyan

2 answers 2

For hierarchy - an abstract class. If the constructor is private, then it is impossible to make an instance of this class outside, including among the heirs.

    If the constructor is private, then it is impossible to make an instance of this class OUTSIDE. But it is possible from the inside, through the available static method. You can even inherit from this class using a static nested class (and then the enclosing class can be abstract). In short, if the constructor is private, then this class fully controls the creation of instances of this class and its heirs.