• 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

Constructor call

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class c
{
c(){
System.out.println("Instance intialization");
}

}


class E extends c{
E(int i,int y ){}
public static void main(String[] args){
E _e = new E(2,4);
}
}

//output "Instance intialization"
In class E ,there will be no default constructor as i have written two argument constructor then hw is the constructor of class c is called.
 
Ranch Hand
Posts: 51
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nish,

This is the way that Java operates.

When you do not call the super contructor explicitly, it calls the super class's default constructor implicitly as;

super();

Regards,
Kamal
 
nish vatsa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but where does the compiler put the call to super ...
u mean here
E(int i,int y ){
super();
}

but i dnt think so..
can u clarify
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, for each constructor you define, the first operation is to call a parent constructor. I you don't explicitly call a parent constructor using super(arguments), there's an implicit call to the parent default constructor.

So your constructor E(int i,int y ){} is equivalent to E(int i,int y ){super();}

Note that if you declare a class F which extends E, for each constructor of F, you'll need to call EXPLCITLY super(int x, int y) since E doesn't have a no-arg constructor.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


E(int i,int y ){
super();
//An implicit call is put in during runtime by the environment at this
//location
}



super() is the first statement in the constructor..

 
Kamal Mettananda
Ranch Hand
Posts: 51
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, The super() call become the first statement in your constructor call.

So before executing the content of your sub class constructor, super constructor will be called.

--------------------

Regards,
Kamal

SCJP 1.4
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic