constructor doubt

In K&B it is stated that the constructor is invoked during runtime when you say new on some class type. My doubt is how come the constructor in the dog class is being invoked when the new is done on the Animal class. Can anyone throw some light on this. Thanks.

If it can't find the Animal() constructor it means the default no args dog constructor called super() and hence the error.

Rajshekhar Anand

Greenhorn

Posts: 14

posted 9 years ago

The compile time error is coming , not because you are making an object of type animal .Its because you don't have a no arg constructor defined in the parent class of dog , that is animal. Define a no arg constructor in Animal class , it will run properly then.

sridhar row

Ranch Hand

Posts: 162

posted 9 years ago

Define a no arg constructor in Animal class , it will run properly then

G.Sinha, the above code works even though i don't define a no-arg constructor in Animal. How come?

Rajshekhar Anand

Greenhorn

Posts: 14

posted 9 years ago

See its like , the derived class tries to call no argument constructor of base class if you don't call , explicitly any other constructor by using super.

here in the second program , you called explicitly a base class constructor with a String argument, so it won't try to call the base class no argument constructor.

Hope it clears the doubt.

-Gaurav

sridhar row

Ranch Hand

Posts: 162

posted 9 years ago

See its like , the derived class tries to call no argument constructor of base class if you don't call , explicitly any other constructor by using super.

Dude, that is my original question. You are going around in circles

In K&B it is stated that the constructor is invoked during runtime when you say new on some class type. My doubt is how come the constructor in the dog class is being invoked when the new is done on the Animal class.

The compiler is just trying to make sure your code will work if it's invoked.

The compiler sees that if Dog's default constructor were invoked (maybe by some other class the compiler knows nothing about), it would attempt to call Animal's no-args constructor. Because no such constructor exists for Animal, a compilation error results.

But this does not mean that your code would necessarily invoke Dog's constructor at runtime. In fact, it does not, and you can demonstrate this by adding some println statements in the constructors...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummersscce.org

To be clear: the problem is not in the execution of the main method in your class test.

It's in the compilation of your class dog.

The cause of that problem is that dog extends from Animal, which has declared a constructor. This means the compiler does NOT create the default, no argument constructor for Animal.

Then along comes sublcass dog. It has no declared constructors, so the compiler wants to declare a default, no argument constructor, which by default calls the no-argument constructor of its parent class, Animal.

But remember, Animal has no such default constructor because you chose to explicitly implement a constructor that takes an argument, but not a no-argument constructor.

So, you can

give dog a constructor that takes a String; it will have to explicitly call super(String)

give dog a no argument construct that calls super("Dog"); or some similar default value to the Animal(String) constructor

add a no-argument constructor to Animal.

Remember, constructors aren't inherited in the same way that methods are. They're special. The compiler is enforcing that.

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

In your example, the code doesn't compile because Dog(String h){} is implicitly trying to call Animal(), which doesn't exist.

To make it work, you need to add super(h) as the first line of Dog(), or add the no-arg constructor to Animal.

I realized that this wasn't stated in my list of solutions I gave above, so I edited the post to minimize confusion from anybody else who reads it.

One last thing: please put your code in the UBB Code tags to make it easier to read. [ May 23, 2008: Message edited by: Stevi Deter ]

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James