• 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

java- constructor chaining

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Employee.java


Faculty.java


Person.java


for Person2 folder
my question is, can you explain about the output? i had try run this program. the output is
(1) Person's no-args constructor is invoked
Faculty
(4) Faculty's no argument constructor is invoked

why it only display no 1 n no4? how about no2 n 3? why no2 n no3 doesn't been display?

[Update: Added code tags, read UseCodeTags]
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say new Faculty() it invokes the constructor defined in the Faculty class. This in turn invokes super("Faculty") which invokes the overloaded constructor of Employee class and not the default constructor, hence you dont see (2) and (3) as the output.

Try this: remove super("Faculty"); from the public Faculty(){} method and see how the output changes.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just to add if you don't provide any constructor compiler will add no args constructor automatically.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javin Paul wrote:just to add if you don't provide any constructor compiler will add no args constructor automatically.

That is called a default constructor.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

your code works like this
1.new Faculty() calls - > public Faculty() constructor in the same class
2.public Faculty() calls super("Faculty") - > which calls the method public Employee(String s){ ..} in the Employee class
3.Since each constructor has super() as its first line (compiler insert super() call if you dont call super() from your constructor explicitly) public Employee(String s){..} calls -> super() which is public Person(){..}
4. public Person() prints "(1) Person's no-args constructor is invoked"
5.control then comes to the caller method public Employee(String s){..} which prints "Faculty"
6.control then comes to the caller method of public Employee(String s){..} which is public Faculty(){ ...} which prints "(4) Faculty's no argument constructor is invoked"

Please refer :
http://docs.oracle.com/javase/tutorial/java/IandI/super.html

for further clarifications
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That’s correct. And welcome to the Ranch
 
Rsudha Ramasamy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic