aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor doubt" Watch "constructor doubt" New topic
Author

constructor doubt

sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
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.
Rajshekhar Anand
Greenhorn

Joined: May 22, 2008
Posts: 14
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

Joined: Jan 16, 2008
Posts: 162
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

Joined: May 22, 2008
Posts: 14
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

Joined: Jan 16, 2008
Posts: 162
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

Joined: May 22, 2008
Posts: 14
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

Joined: Aug 31, 2004
Posts: 11343

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...
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

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 ]

    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
    Rakesh Chaudhary
    Ranch Hand

    Joined: Feb 19, 2008
    Posts: 120
    Hope this helps






    Cya...


    Acquire the unexpected.......
    Preparing for SCJP 6
    sridhar row
    Ranch Hand

    Joined: Jan 16, 2008
    Posts: 162
    Thanks guys (Marc and Stevi nice explanation as usual )
    [ May 22, 2008: Message edited by: sridhar row ]
    Vijay Arora
    Greenhorn

    Joined: Jun 25, 2007
    Posts: 19
    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");
    }
    }


    vijay arora
    Stevi Deter
    Ranch Hand

    Joined: Mar 22, 2008
    Posts: 265

    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 ]
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    "Albert",
    Please check your private messages.
    -Ben


    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: constructor doubt