Restrict a Class

How can we restrict a class so that no instance creation of class possible without declaring the class as Abstract ?

Questions by jas_vineet   answers by jas_vineet

Showing Answers 1 - 30 of 30 Answers

If you have a private constructor in your class, you can still instatiate the class in that particular class. You cannnot however instantiate it in any other class.

  Was this answer useful?  Yes

We can do this by putting  an abstract method in that class.
This is because if their is any abstract method in the class then that class must be declared abstract.

  Was this answer useful?  Yes

aleemkh

  • Aug 4th, 2008
 

yes by making the constructor of a class private we can achieve this.
Since the private modifier is accessible withing the class, when a class outside this class tries to instantiate this class it gives error as the class cannot be instantiated.

Sarje

  • Aug 18th, 2009
 

Only abstract classes can not be instantiated. If you make the constructor private then you can not instantiate it outside of the class but you can still instantiate it within the same class. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions