File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preventing instantion of base class object

 
Puneet Agarwal
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Puneet Agarwal
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That wont work.....Declaring class A constructor as private will not allow me to create object of class B.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Puneet, Mona is right
Check the following code
 
Puneet Agarwal
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
prasanthi alla
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oopssssss sorry pradeep i misunderstood it
thank u
 
prasanthi alla
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya that's what i mean pradeep
so please help me in knowing
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic