• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Doubt on constructors

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public Cert1
{
public Cert1()
{
System.out.print("1");
}
}

public Cert2 extends Cert1
{

public Cert2()
{
System.out.print("2");
}
}

public Cert3 extends Cert3
{

public Cert3()
{
System.out.print("3");
}
}

public class certdemo
{
public static void main(String args[])
{
new Cert3();
}
}

o/p -- 123

Why not 321 as Cert3 extends Cert2 which extends Cert1

Can anyone focus some light on this please

Source " My own code"
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because the flow of the constructor goes from the super class till the other sub classes who extend that super class.

Here when c3 is called it looks up to find its immediate superclass i.e c2...c2 extends the class c1....So from there the flow of the constructor starts. Here the flow starts from C1 because it is the constructor of the superclass..Hence the output 123.

Hope that helps.
[ March 13, 2008: Message edited by: ansuman mohapatra ]
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure your code will even compile?
 
ansuman mohapatra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Cert1
{
public Cert1()
{
System.out.print("1");
}
}

class Cert2 extends Cert1
{
public Cert2()
{
System.out.print("2");
}
}

class Cert3 extends Cert2
{
public Cert3()
{
System.out.print("3");
}
}

public class Constructor
{
public static void main(String args[])
{
new Cert3();
}
}

You can try with this code.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why what happen
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the order make sense, because if we print 321, then we have to first execute all the statement in the class 3, this make us into trouble because without knowing what your parent variables are doing you cant execute you class variables.

do you think it make sense?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting can you illustrate your words with example which prints 321.

Please
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dinesh,

Here's the explanation

First whenever a constructor runs there is an implicit call to its super constructor in the first line itself if there is no call to the other overloaded constructors.

In your example, cert3 calls cert2 which calls cert1 which calls the Object constructor.

All the super constructor runs and then the subclass constructor constructor runs!

For a more detailed explanation checkout K & B.

Hope that helps.


Uttara Rishi.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dinesh,

Whenever any of the construtor get called weather its subclass constructor or superclass the execution always start from the default constructor of the superclass.
That is why the o/p is 123.
Your code is not OK.I did changes.

Execution goes like this

In main() method the Cert3() get called.
class hierarchy goes like this
cert 1
|
cert 2
|
cert 3

Now as the cert3()default constructor is at the last in the hierarchy so the call goes automatically to cert1() default constructor that is super class.
so now the output will be like
cert1() ->cert2()->cert3()

o/p 123

Hope this help


Sandhya
[ March 13, 2008: Message edited by: sandhi mridul ]
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic