• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to prevent base class constructor from executing  RSS feed

 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the help. ( )
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you don't have a base class constructor(no arg one) then your child object won't be created at all.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
salvin francis
Bartender
Posts: 1660
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!