File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Preventing instantion of base class object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Preventing instantion of base class object" Watch "Preventing instantion of base class object" New topic
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: 8903



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: 24183
    
  34

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: 8903

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: 8903

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: 8903

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preventing instantion of base class object
 
Similar Threads
What class must an inner class extend ?
availability of ActionForm object in two action classes.
Javaranch Tips/Tricks question
interesting question
Mock question on polymorphism