• 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

constructors

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the constructors of the base class gets invoked while creating objects for the sub class??pleas explain
 
Ranch Hand
Posts: 83
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A derived class is type of its super class. Therefore while creating an object of derived class details of constructing derived class are present in its own constructor but details specific to the super class are present in the super class only.

For example if you have a
Class MySuper {
int a;
public MySuper() {
this.a= 100;
}
}


Now if you write a new derived class for above class, you should always get the int a initialized to 100 for all objects. And that is possible only if the Super class constructor is called for each object of all subclasses. If this does not happen the purpose of inheritance is defeated.

This was a simple example but if you think about a super class which has many attributes and all of them to be inherited to its sub classes then it would make sense.
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sachin, but
'All variables / fields that are declared in the construcotr block or the method block or a static block is not allowed to be accessed outside.
Include Private members too in that list.
When you say extends we extend the access to the behavior's(methods) of the class and nothing else'.

are not the above statements correct? I guess if they are correct, whether the following statement justifies?
'if you write a new derived class for above class, you should always get the int a initialized to 100 for all objects. And that is possible only if the Super class constructor is called for each object of all subclasses'
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor has the very specific job of getting an object ready for use. There is no reason for its internal workings to be used in any other way.

Instantiation of an object starts at the top of the hierarchy (with java.lang.Object) and works its way down.

For example, we might say that a Volkswagen IS-A Car, and a Car IS-AN Object. So if we're going to create a Volkswagen object, we start by creating an Object first (calling Object's constructor). Next, we take our Object and "extend" it into a Car (calling Car's constructor). And finally, we take our Car and "extend" it into a Volkswagen (calling Volkswagen's constructor).
 
Sheriff
Posts: 22785
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"SachinJoshi Joshi",

I sent you a private message two days ago. I urge you to read it, and follow its instructions.

Thank you,

Rob
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic