GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 467 of 502    Print  
Restrict a Class
How can we restrict a class so that no instance creation of class possible without declaring the class as Abstract ?


  
Total Answers and Comments: 10 Last Update: August 18, 2009     Asked by: jas_vineet 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: jas_vineet
 
Can you explain a bit more on this with example ?

Above answer was rated as good by the following members:
getravi2k, swpnl_ptl, bino75
March 15, 2008 01:18:27   #1  
aditimohan Member Since: March 2008   Contribution: 1    

RE: Restrict a Class
We can acheive that by declaring the default constructor as private.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
March 17, 2008 04:56:51   #2  
jas_vineet Member Since: March 2008   Contribution: 4    

RE: Restrict a Class
Can you explain a bit more on this with example ?
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
April 17, 2008 07:30:47   #3  
trcdutt Member Since: February 2008   Contribution: 3    

RE: Restrict a Class
public class Test {

private Test() {
// private constructor
}
}

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
May 06, 2008 16:20:53   #4  
santoshbwn Member Since: May 2008   Contribution: 2    

RE: Restrict a Class
Eg inbuilt class: Math
The above class has a private constructor

 
Is this answer useful? Yes | No
June 15, 2008 17:21:17   #5  
interviewprep9 Member Since: June 2008   Contribution: 10    

RE: Restrict a Class
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.


 
Is this answer useful? Yes | No
August 03, 2008 02:21:13   #6  
er.mukesh_208 Member Since: May 2008   Contribution: 3    

RE: Restrict a Class
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.

 
Is this answer useful? Yes | No
August 03, 2008 23:57:03   #7  
yatitandon Member Since: September 2006   Contribution: 1    

RE: Restrict a Class
Making the constructor as private is the right answer.
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
August 04, 2008 00:12:57   #8  
aleemkh Member Since: July 2008   Contribution: 17    

RE: Restrict a Class

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.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 21, 2009 16:28:08   #9  
sriragv Member Since: June 2009   Contribution: 1    

RE: Restrict a Class
1. By creating a private constructor
2. By giving abstract key word to the class

 
Is this answer useful? Yes | No
August 18, 2009 06:51:14   #10  
Sarje Member Since: August 2009   Contribution: 62    

RE: Restrict a Class
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.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape