How does Java restrict a class from being used to create objects?

  • By declaring it as an abstract class
  • By marking its constructor as private
  • By specifying it as a singleton class
  • By using the final keyword
In Java, when you mark a class constructor as private, it prevents the class from being instantiated from outside the class, effectively restricting the creation of objects. Abstract classes can't be instantiated directly, but this is not the primary means of restriction. The final keyword prevents subclassing but doesn't restrict object creation. A singleton pattern controls object creation, but it's not the typical way to restrict a class.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *