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.
Loading...
Related Quiz
- What is the role of FileWriter and FileReader in character streams when dealing with file I/O?
- What will happen if the superclass method does not exist in the subclass while trying to override it?
- What will happen if the DriverManager is unable to connect to the database using the provided URL?
- Can an interface contain static methods in Java?
- Which sorting algorithm uses a divide-and-conquer strategy to sort data?