Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to prevent a Non-abstract class from instantiation?

Posted on 2000-05-15
13
Medium Priority
?
328 Views
Last Modified: 2010-04-01
Here I have two twisting questions in Java?
1. How can prevent my non-abstract class from instantiation. Mind that it is strictly a non - abstract class( Not an interface)?

2. I would like to create only three objects for a class . If more than that, It should give an error message. Please tell me the secret behind it.?
0
Comment
Question by:sjhyam
  • 4
  • 4
  • 2
  • +3
13 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 2810276
1.for ur first question u can use
public final class myClass
{

}
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2810288
1. Make all constructors private or if the class doesn't have a constructor provide a private one.

2. make the constructor private, provide a class factory for that object or a static function that keep in a static variable the number of all instances; when the value in that variable will exceed 3 then return null or throw an exception;
the number of instances can be managed from the constructor also.
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2810291
1. How can prevent my non-abstract class from instantiation. Mind that it is strictly a non -  abstract class( Not an interface)?

Answer: Make the constructor private or protected.  Then provide a method named something like: newInstance() that would create an object using the private constructor if needed.

2. I would like to create only three objects for a class . If more than that, It should give an error message. Please tell me the secret behind it.?

Answer: Using the same methodology as in the above example, provide only protected or private constructors.  Again, these will only be available to the class, and therefore you can provide methods that would control creation of objects of the class.  In the newInstance() method you can call the constructor the first three times requested (and keep a count of the number of objects created).  After the three are created, you can return a null handle, or a handle to one of the already created objects (a variation on the Singleton pattern.), or you can throw an exception.

Good luck

Laminamia :)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2810293
Oops, sorry ovidiucraciun.  

I'll make my answer a comment.

Laminamia :)
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2810305
for the first question:
you can put your class in a package and make it visible only for the classes inside the package; in that way when you'll deploy the binary files the user will can instantiate only the public classes of the package.
of course if the user is clever enough he can avoid this kind of protection ;)
0
 
LVL 2

Author Comment

by:sjhyam
ID: 2810391
This question has a deletion request Pending
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2810460
This question no longer is pending deletion
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2810461
hmmm ... ;)
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2811425
sjhyam:

If someone has given a comment that you feel deserves the points because it answers the question, then click on the button "Accept comment as answer" on the comment that answers your question.

Laminamia :)
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2812405
>>>>>Here I have two twisting questions in Java?

some people provide satisfactory answers ,those people don't get any money only satisfaction,so please give those deserving people points.

They both have already answered satisfactorily,pls don't take offence at this.

:)
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2812410
rjackman,
when u want to prevent subclassing  of your class you tag it final, but we can still make insatnces of that class...
0
 

Accepted Solution

by:
ninnu earned 100 total points
ID: 2826939
For your first question
 having a private constructor would be the better way.
for your second question
  you can have a global static variable intialized to zero and keep on increment it when a new object has been constructed and check for it in the constructor.If it is more than 3 ,display the error message.
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2827170
Ninnu.  I believe that the answer was already given in several above comments.

shyjam should accept one of those as the answer.

Would you please withdraw your answer due to the fact that other experts already left the answer in comments?

Laminamia :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question