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

How child class call super class constructor..

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i attended an interview they have given this question





what is out put for this question ,
i thought when we compile the child class , and decompile the child class by using javap,

,compiler will provide only provide only defaukt constructor if not provided one,
thats what i understand ed from javap, by seeing the byte code

but the output is different , it is calling super class constructor,
there is no signs of calling super class constructor in the byte code of Child class,

then how it is possible to call super class constructor
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

use javap -c YourClass

1: invokespecial #11; //Method java/lang/Object."<init>":()V


you will see the above statement in constructor. which is nothing
but call default constructor of your super class.

Shortly, compiler insert super() to first line of your every constructor, if you dont provide super(...) or this(...) explicitly
 
Girish K Gupta
Greenhorn
Posts: 19
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raj

These are standard questions with constructor-chaining concept.
In all constructors a call to super classes's constructor is implicitly made.

eg, If you write parent's constructor as following:


It would be interpreted like:


Please see the documentation http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8
You can refer to any Java text book as well.

Regards
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for , your replie

Then why cant we see ,this super class constructor,in byte code

by using javap conmmand because it is profiler right( decompiler),


is there any plugins for decompiler, in eclipse

or any software for decompiling, please suggest that software,

And last thing ,these are standard questions which i am facing in interviews,

can any one please suggest, how solve this type of questions,
or books are there to pratice this type of question,
i tried cathyseira scjp, but they written in highlevel..
 
Campbell Ritchie
Sheriff
Pie
Posts: 49391
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raj talatam wrote: . . . Then why cant we see ,this super class constructor,in byte code . . .
Yes, you can. Seetharaman Venkatasamy showed you it: Object."<init>".

When you are asked that sort of question, remember that such implementation details are not necessary for the programmer to know.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raj talatam wrote:i tried cathyseira scjp, but they written in highlevel..

It's Kathy Sierra, BTW
 
Girish K Gupta
Greenhorn
Posts: 19
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raj

I don't know what types of questions you are looking for but for the one you mentioned in this thread above the Kathy Siera book gives all the required information.

I feel whatever is provided in this book is more than sufficient for an application developer. [Chapter 2, Constructors and Instantiation (Exam Objectives 1.6, 5.3, and 5.4)]
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gupta,

They are asking questions on constructors, collections,

Exceptional handling

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic