• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor doubt

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: John Meyers SCJP 5 Mock Exam



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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sridhar row:
... My doubt is how come the constructor in the dog class is being invoked when the new is done on the Animal class...


What makes you say the Dog constructor is running?
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc, When i tried to compile i got the following error:

test1.java:12: cannot find symbol
symbol : constructor Animal()
location: class Animal
class dog extends Animal

If it can't find the Animal() constructor it means the default no args dog constructor called super() and hence the error.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rajshekhar Anand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I explained you back again , bcz you changed the program.

and creating an object of base class doesn't invokes derived class constructor

-Gaurav
[ May 22, 2008: Message edited by: G Sinha ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sridhar,

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.

    [ Edited to be clearer per question and discussion below. ]
    [ May 23, 2008: Message edited by: Stevi Deter ]
     
    Ranch Hand
    Posts: 120
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hope this helps






    Cya...
     
    sridhar row
    Ranch Hand
    Posts: 162
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks guys (Marc and Stevi nice explanation as usual )
    [ May 22, 2008: Message edited by: sridhar row ]
     
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In this code compiler will not create default constructor for Dog
    Class.But still there is compilation error.Why???
    class Animal
    {
    Animal(String name)
    {
    System.out.println("ANIMAL NAME="+name);
    }
    }
    class Dog extends Animal
    {
    Dog(String h)
    {
    }

    }
    public class Three{
    public static void main (String [] args)
    {
    new Animal("Giraffe");
    }
    }
     
    Stevi Deter
    Ranch Hand
    Posts: 265
    Hibernate Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Vijay,

    Any constructor will implicitly call the no-args super() of the parent class unless you explicitly call a different version of super(args...).

    This is defined in the Java Language Specification:


    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 ]
     
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "Albert",
    Please check your private messages.
    -Ben
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic