Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super class constructors

 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
why do we have to call the super class constructors in order to use the instance variables and methods of sub class.Even if we want to use the instance variables inside the constructor of the sub class,we cannot do so unless we have the constructors of all the super classes run already.
Why is so?
Why is it necessary for the sub class to run the constructors of super classes to use its own instance variables inside its own constructor?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because a sub-class-object IS A super-class-object. that is the basis of inheritance.A car which is a green Ford also IS-A vehicle which is a green Ford. So when you create your object as a green Ford car, you have to create it as a green Ford vehicle first.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic