• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Constructot Doubt

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is compilation error in the Code.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");
}
}
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is compilation error in the Code.Why???



Can you please tell us the exact error which you got when you try to compile the the above code and your interpretation of the error.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you just change your code like this...

Dog(String h)
{
super(h);
}


now your code will run.
 
Vijay Arora
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this compilation error:
can not find symbol:constructor Animal()
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Albert:
I got this compilation error:
can not find symbol:constructor Animal()



Please quote your source when you display code. This appears to be from my exam. Please also change your name to reflect javaranch naming standards. You need a first and last name

Any class that extends from another will try to run its default constructor. If you dont have that it will throw a compile time error.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi tushar,
Super class dont know about child class.There is a simple class Animal and having one constructor and we are calling constructor in main()function with proper argument.Then why the code is giving comile time error that can't find symbol.

thanks,
priya.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dog extends Animal, and before a Dog can be constructed, its parent must be. In your code even though it is not typed, the compiler puts in a super() call to a default Animal constructor which doesn't exist (hence the error).

 
priya shende
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mustafa.
-----------------
Studying for SCJP.
 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,
You are right.
I had also thought when i first saw the question but after contemplating over the question i found the problem.
we know that ,Compiler checks grammare of language.
So even we are not bothering with any other class except Animal in main,it will give error because we are not calling super class constructor.
Whatever mustafa is saying its true.


Tushar
SCJP5.0
Pune
India.
 
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
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your Animal class there is only one parameterized constructor .
Just modify your Dog class
class Dog{
Dog(String h){
super(h);
}
}

or modify your Animal class
just add default constructor to it .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic