| Author |
How to prevent base class constructor from executing
|
Nitin Menon
Ranch Hand
Joined: Jun 13, 2007
Posts: 79
|
|
Hi,
can anyone please help me...?
writing the sample code...
This code will output the contents of the child class constructor and parent class constructor. How do i prevent the parent class constructor from executing...?
Thanks in advance...!
(Christophe:I have edited your post by adding the code tag. Write source code inside the code tag to make it more readable.)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16809
|
|
Simply... You can't. One of the base class constructors must execute. The only way to avoid it, is to not inherit from the base class.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Nitin Menon
Ranch Hand
Joined: Jun 13, 2007
Posts: 79
|
|
Thank you very much for the help. ( )
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
Well if you don't have a base class constructor(no arg one) then your child object won't be created at all.
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
The reason why you cannot prevent a base class constructor from running is because of how objects and inheritance work.
Inheritance establishes an is-a relationship between the subclass and the superclass: an instance of the subclass is an instance of the superclass. In fact, it's an instance of the superclass with the specific stuff from the subclass added on top of it.
When an object of class B that extends class A is created, the following happens (conceptually): First, an instance of class A is created. To initialize this instance, one of the constructors of class A is called. Then, the extra stuff that class B adds to class A is added to the new object - this makes the object an instance of class B. A constructor of class B is called to initialize the object.
There is no way to make a B object without first making an A object, so there is no way to prevent the constructor of class A from being executed.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Nitin Menon
Ranch Hand
Joined: Jun 13, 2007
Posts: 79
|
|
Thank You Nitish and Jesper for the valuable information. This is a completely new information you have given me...!
Thanks a lot...!
|
 |
Himanshu Kansal
Ranch Hand
Joined: Jul 05, 2009
Posts: 257
|
|
Nitish Bangera wrote:Well if you don't have a base class constructor(no arg one) then your child object won't be created at all.
can call base class constructor required.
|
Experience and talent are independent of age
|
 |
salvin francis
Ranch Hand
Joined: Jan 12, 2009
Posts: 915
|
|
You need to think in this manner:
how can a child be born if his parent isnt ?
Too non technical
"be born" : constructor being invoked.
a bit off the topic:
A child can actually choose how his parent should be born (now thats vague) by a call to super.
but then it makes sense to have super as the first call in constructor compulsarily.
Back to java ....
The others are right you cannot prevent a parent's constructor from being called, you can divert it to another constructor if you need to,
but one constructor has to be called.
note that java provides a default empty constructor if none is defined.
|
My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
|
 |
Nitin Menon
Ranch Hand
Joined: Jun 13, 2007
Posts: 79
|
|
salvin francis wrote:You need to think in this manner:
how can a child be born if his parent isnt ?
Too non technical
"be born" : constructor being invoked.
a bit off the topic:
A child can actually choose how his parent should be born (now thats vague) by a call to super.
but then it makes sense to have super as the first call in constructor compulsarily.
Back to java ....
The others are right you cannot prevent a parent's constructor from being called, you can divert it to another constructor if you need to,
but one constructor has to be called.
note that java provides a default empty constructor if none is defined.
Can You please tell me why the below code brings error?
class Base
{
Base(int k)
{
System.out.println("parent"+k);
}
}
class Child1 extends Base
{
}
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
|
Because you are not invoking the superclass constructor. If you don't write a super(something); call the compiler will (attempt to) introduce a plain simple super(); but that doesn't match the available constructors.
|
 |
Nitin Menon
Ranch Hand
Joined: Jun 13, 2007
Posts: 79
|
|
Campbell Ritchie wrote:Because you are not invoking the superclass constructor. If you don't write a super(something); call the compiler will (attempt to) introduce a plain simple super(); but that doesn't match the available constructors.
Thanks for the reply. I got your point.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
Well done
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
salvin francis wrote:You need to think in this manner:
how can a child be born if his parent isnt ?
Ok, in this context, that analogy kind of works. But in general, I'm always wary of people who name their superclass "Parent" and their subclass "Child", because they carry over the biological meaning of "inheritance" to literally to object-oriented programming.
The most important thing to remember with regard to inheritance is that it indicates an "is-a" relationship: the subclass is a (specialized version of) the superclass.
In the case of Parent and Child, this doesn't hold: a Child is not a Parent. A better example which is also used very often is the Animal / Dog / Cat hierarchy: Animal is the superclass of Dog and of Cat, because dogs and cats are certain kinds of animals.
|
 |
Nitin Menon
Ranch Hand
Joined: Jun 13, 2007
Posts: 79
|
|
Jesper Young wrote:
salvin francis wrote:You need to think in this manner:
how can a child be born if his parent isnt ?
Ok, in this context, that analogy kind of works. But in general, I'm always wary of people who name their superclass "Parent" and their subclass "Child", because they carry over the biological meaning of "inheritance" to literally to object-oriented programming.
The most important thing to remember with regard to inheritance is that it indicates an "is-a" relationship: the subclass is a (specialized version of) the superclass.
In the case of Parent and Child, this doesn't hold: a Child is not a Parent. A better example which is also used very often is the Animal / Dog / Cat hierarchy: Animal is the superclass of Dog and of Cat, because dogs and cats are certain kinds of animals.
Thanks Jesper. That was very helpful. Actually, i was getting kind of mislead comparing inheritance with the parent child context. Anyway, thanks for the valuable suggestion.
|
 |
 |
|
|
subject: How to prevent base class constructor from executing
|
|
|