Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Question on Constructor

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like:
class par{
par(int i){}
}
class chi extends par{
chi(){}
}
the program will fail when compiling and says a "cannot resolve symbol constructor par()".
i know that there won't be a default non arugement constructor created for chi in this case. however, why such a strange thing will happen even i created one constrcutor for the chi?
besides, no matter the function chi() has argument or not, the compiler all works like that.
.........who could tell me why?
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to define a default constructor in the parent class.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Shishio San has said you must explicitly declare a no-arguments constructor for class par.
When an instance of class chi is created its constructor chi() is called. But class chi is a subclass of class par so a constructor of class par must be called. Because you have not called the class par constructor yourself the compiler tries to call the par() (it does this by doing super())constructor for you. But there isn't one, because you only provided par(int). Remember: if you provide a constructor for a class the compiler will not provide a no-argument constructor automatically. If you do not provide any constructors the compiler will supply a default no-argument constructor.
-Barry
[ December 22, 2002: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rui,
Remember the super() construct leads to chaining of subclass constructors to superclass constructors. This means that a subclass must call the constructor of its superclass. This chaining behavior guarantees that all superclass constructors are called starting from the Object class, followed by the constructors in the other classes down to the class being instantiated. This is called (subclass-superclass) constructor chaining.
If a constructor does not have an explicit call to super(), then the call super() (without the parameters) is implicitly inserted to invoke the default constructor of the superclass.
If a class only defines non-default constructors (i.e. only constructors with parameters) then its subclasses cannot rely on the implicit behavior of a super() call being inserted. Subclasses must then explicitly call a superclass constructor, using the super() construct with the right arguments. In your case:
class par{
par(int i){}
}
class chi extends par{
chi(){
super(5) //insert this code
}
Hope that makes sense
}
 
RUI CHEN
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, Shishio,Barry and Roan!!!
I will remember it, "constructor chaining".
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- just wanted to clarify a couple of little terminology things
For constructor chaining, we actually say that constructors are CALLED from the bottom up, but COMPLETE from the top-down. So constructor chaining doesn't exactly start from the top, at Object, although Object is the first to actually complete. Constructor chaining puts the constructors on the stack starting with the class you said "new" on. So if you say new Dog(), the Dog constructor first goes on the stack. Then, the immediate superclass constructor (Animal()) is invoked (using the call to super()) and then the next superclass, etc. all the way up to Object. So Object's constructor is the LAST to be invoked (it's at the top of the stack), but it is the FIRST to actually complete. Then once it completes it is popped off the stack and the next highest class in the inheritance tree runs IT'S constructor, and so on until you get to the bottom -- the constructor that was first invoked.
Here's one way to remember it...
Your parents have to be born FIRST, before YOU can be born. Java doesn't really violate the fundamentals of biology. Well, not completely. Then again, you can only have one parent... oh well, we won't go there ; )
And one other thing:
We make a distinction between the "default" constructor and a "no-arg" constructor. The default constructor is the one the compiler puts in, which happens to be a no-arg constructor. But if you write one yourself, we say that you wrote a no-arg constructor, not a default constructor. The default constructor is the one you get if you don't write any constructors at all.
Cheers,
Kathy
Author, "Cowgirl's guide to no-arg relationships"
 
It's never done THAT before. Explain it to me tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic