| Author |
Preventing instantion of base class object
|
Puneet Agarwal
Ranch Hand
Joined: Jul 15, 2003
Posts: 49
|
|
Lets say we have 2 classes A and B . B is derived from A . How can I achieve a situation where I should be able to create an object of B , but not of A . Is it possible.... P.S. - A cannot be made abstract , it is a proper class.
|
 |
Mona Gadkari
Ranch Hand
Joined: Jul 28, 2003
Posts: 64
|
|
Originally posted by Puneet Agarwal: Lets say we have 2 classes A and B . B is derived from A . How can I achieve a situation where I should be able to create an object of B , but not of A . Is it possible.... P.S. - A cannot be made abstract , it is a proper class.
Hello, I would suggest that u can declare the constructor of class A as private, then no one can create an object of calss A.
|
Mona(Varijasmom)
|
 |
Puneet Agarwal
Ranch Hand
Joined: Jul 15, 2003
Posts: 49
|
|
|
That wont work.....Declaring class A constructor as private will not allow me to create object of class B.
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
hi Puneet, Mona is right Check the following code
|
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
|
 |
Puneet Agarwal
Ranch Hand
Joined: Jul 15, 2003
Posts: 49
|
|
I think I am a bit confused here . How can you instantiate a child class object if you declre the base class constrctor as private . My question is to allow the creation of a derived class but not the Base class.
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
I think I am a bit confused here . How can you instantiate a child class object if you declre the base class constrctor as private .
You can't. My previous code doesn't compile.
My question is to allow the creation of a derived class but not the Base class.
I don't see the point of your question!!! if you don't want to use the base class, then why do you extended it at the first place!!!
|
 |
prasanthi alla
Greenhorn
Joined: Dec 02, 2003
Posts: 15
|
|
hi vicken! ur code gave errors when i tried to run it, Error #: 306 : constructor B() has private access in class untitled1.B at line 16, column 3 can u please clear this bye
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
posted by prasanthi,
ur code gave errors when i tried to run it, Error #: 306 : constructor B() has private access in class untitled1.B at line 16, column 3
Yes, i know. Its purpose is to show that you cannot instantiate a class with a private constructor.
|
 |
prasanthi alla
Greenhorn
Joined: Dec 02, 2003
Posts: 15
|
|
hi i know that we can't instantiate a class with private constructor, but is it also like we can't extend a class which has private constructor because i got that error at line of class definition of derived class then what's the use of having final modifier for a class thank u
|
 |
Puneet Agarwal
Ranch Hand
Joined: Jul 15, 2003
Posts: 49
|
|
|
I was asked this question in a job interview recently . Seems odd to me the question . I am unable to get a scenario where such a situation would arise .
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
Private constructors are usually used in singleton approaches, in which we only want to instantiate an object only once (No more than one instance of an object is allowed). Such classes has static method that allows the instantiation. For example: [ December 03, 2003: Message edited by: Vicken Karaoghlanian ]
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
|
|
Groovy
|
 |
prasanthi alla
Greenhorn
Joined: Dec 02, 2003
Posts: 15
|
|
hi pradeep , this is cool with inner classes, but how to code the same condition for derived classes (instantiate derived class but can't be able to instantiate base class) thank u
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
One thing you can do is to give A a protected constructor. If A is in a package, then code outside that package is prevented from constructing a bare instance of A; only instances of A's subclasses (like B) can be constructed. Inside A's package, A objects can be constructed, of course. The question setup is a bit flawed. If A "can't be abstract because it's a proper class", the implication is that instances can be created -- otherwise, it could be abstract. Perhaps, then, preventing construction outside the defining package is what the questioner meant.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
this is cool with inner classes, but how to code the same condition for derived classes
In the example subTest is a dervied class of ConTest, or did you mean that dervied class is a top-level class?
|
 |
prasanthi alla
Greenhorn
Joined: Dec 02, 2003
Posts: 15
|
|
oopssssss sorry pradeep i misunderstood it thank u
|
 |
prasanthi alla
Greenhorn
Joined: Dec 02, 2003
Posts: 15
|
|
hi pradeep sorry for again bothering u but iam not clear with that, subtest is a class which extends contest, but still its defined within the class contest , so is it not inner class of that? may be iam clear with nested classes topic thanks for ur time
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
|
Yes it is an static inner class. That is the reason I asked you whether the dervied class is supposed to be a top-level class (This is not the case in my example).It would be great if you could lemme know that.
|
 |
prasanthi alla
Greenhorn
Joined: Dec 02, 2003
Posts: 15
|
|
ya that's what i mean pradeep so please help me in knowing
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
|
If B is a dervied top level class then answer for your question - it cannot be done in Java. If it isn't a top level class then my code sample where subtest extends ConTest is a good example, here you cannot create an instance of superclass (ConTest)since the constructor is private but an instance of subclass (SubTest)can be created.
|
 |
 |
|
|
subject: Preventing instantion of base class object
|
|
|