• 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 confusion

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

Hi
Can anyone please explain this to me
Here when I compile this,I get the results :
Base Class Constructor called with Hello argument
SubClass Constructor called
As far as I know , whenever a Sub class is instanciated,
always its Base class constructor is called first
and then the Sub Class constructor.Here also
the same is happening but how does the Base
Class Constructor receives the "Hello" argument
through super keyword which is placed in Subclass
contructor.It can only happen if the sub class constructor
is called first and then the Base class constructor.
If I am wrong please correct me


class BaseClass
{BaseClass(String str)
{
System.out.println("Base Class Constructor called with " + str + " argument");
}}
public class SubClass extends BaseClass
{
SubClass()
{
super("Hello"); //1
System.out.println("SubClass Constructor called");
}
public static void main(String args[])
{
SubClass sc = new SubClass();
}}
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is why the call to super("Hello") must be the first thing in the constructor. Java tests to see if you call anything other than the default constructor first.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add what Paul said...
Don't be confused with order of initialization, when you construct an instance of SubClass class using code :
... = new SubClass();
the SubClass constructor will always be execute before the BaseClass, to see if there is any explicit call to BaseClass (using super()).
Note : if there is NO explicit call, java will IMPLICITLY call super(), to access/call the BaseClass default constructor.
It will look's like BaseClass Constructor is executed first..!
so that's why super() must be the first thing in the constructor.
stevie
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by aurangzeb akhtar:

Hi
Can anyone please explain this to me
Here when I compile this,I get the results :
Base Class Constructor called with Hello argument
SubClass Constructor called
As far as I know , whenever a Sub class is instanciated,
always its Base class constructor is called first
and then the Sub Class constructor.Here also
the same is happening but how does the Base
Class Constructor receives the "Hello" argument
through super keyword which is placed in Subclass
contructor.It can only happen if the sub class constructor
is called first and then the Base class constructor.
If I am wrong please correct me


class BaseClass
{BaseClass(String str)
{
System.out.println("Base Class Constructor called with " + str + " argument");
}}
public class SubClass extends BaseClass
{
SubClass()
{
super("Hello"); //1
System.out.println("SubClass Constructor called");
}
public static void main(String args[])
{
SubClass sc = new SubClass();
}}


hi aurangzeb
Please read 'Inheritance' topic by Kavita which deals with somewhat same type of thing.
sandeep
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a 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