• Post Reply Bookmark Topic Watch Topic
  • New Topic

java- constructor chaining  RSS feed

 
nur waheeda
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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]
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Javin Paul
Ranch Hand
Posts: 297
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to add if you don't provide any constructor compiler will add no args constructor automatically.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Rsudha Ramasamy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That’s correct. And welcome to the Ranch
 
Rsudha Ramasamy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!