Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

call super class constructor

 
Greenhorn
Posts: 19
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First we create object of class like,

A a = new A();

when this line of code will execute,

constructor of class A will call. ok

Now class A's constructor is like this,

A(){
super();
}

when this constructor will call,

A's superclass constructor will also called,

And If I don't write super(); in constructor, then also call to super class constructor,

so my question is How Long the super class' constructor will call ?

because in java every class create default constructor automatically(if not initialized).
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Krunal wrote: so my question is How Long the super class' constructor will call ?



Hello Krunal,

The super class constructor will be invoked according to the regular execution time of the program, it does NOT have specific timing nor specific timing calculation (to the best of my knowledge).

Regards

Ikpefua
 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will call all the super classes of class A
for example
class A extends B{}
then the default constructor of class A will call default constructor of class B and which will call Object's constructor.As all classes are subclass of Object ,therefore your last call to super will always be Object

I hoped i Helped You
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sagar I was NOT so suire of the question, now I got what he meant...
 
krunal prajapati
Greenhorn
Posts: 19
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you sagar,
It helped me a lot.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krunal prajapati wrote:Thank you sagar,
It helped me a lot.




Read K & B book on page 170 about Constructor Chaining for more details. Below is a recap from the book:

Constructor Chaining

4. Object()
3. Animal() calls super()
2. Horse() calls super()
1. main() calls new Horse()


We know that constructors are invoked at runtime when you say new on some class
type as follows:

Horse h = new Horse();

But what really happens when you say new Horse() ?
(Assume Horse extends Animal and Animal extends Object.)

1. Horse constructor is invoked. Every constructor invokes the constructor
of its superclass with an (implicit) call to super(), unless the constructor
invokes an overloaded constructor of the same class (more on that in a
minute).

(This is a tricky ONE if you have overloaded constructor, see in coding for more insight).

2. Animal constructor is invoked (Animal is the superclass of Horse).

3. Object constructor is invoked (Object is the ultimate superclass of all
classes, so class Animal extends Object even though you don't actually
type "extends Object" into the Animal class declaration. It's implicit.) At
this point we're on the top of the stack.

4. Object instance variables are given their explicit values. By explicit values,
we mean values that are assigned at the time the variables are declared,
like "int x = 27", where "27" is the explicit value (as opposed to the
default value) of the instance variable.

5. Object constructor completes.

6. Animal instance variables are given their explicit values (if any).

7. Animal constructor completes.

8. Horse instance variables are given their explicit values (if any).

9. Horse constructor completes.



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

Ikpefua Jacob-Obinyan wrote:

Krunal wrote: so my question is How Long the super class' constructor will call ?



Hello Krunal,

The super class constructor will be invoked according to the regular execution time of the program, it does NOT have specific timing nor specific timing calculation (to the best of my knowledge).

Regards

Ikpefua

at what specific time a constructor will invoke?
 
justin ford
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[zulfy]
my question is : At what specific time a constructor will invoke?
reply
    Bookmark Topic Watch Topic
  • New Topic