• 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

A simple Q but still have doubt

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends.

When a subclass's constructor calls the super class's constructor...Why it always invokes the default constructor(i.e which takes no arguments.) of super class, Even if there is the constructor of super class with same arg.
of calling constructor ???

For detailed explanation of my Q see following code:

class Super
{
Super(int a){
System.out.println(a);
}

Super(){
System.out.println("default Super");
}

}

class Sub extends Super{

Sub (int w){
System.out.println("Sub w");
}

Sub(){
System.out.println("Sub default");
}

public static void main(String args[]){
Super t=new Super(3);
Sub t1=new Sub(5);
Sub t2=new Sub();

}
}


the o/p:
3
default Super
Sub w
default Super
Sub default

-----------
Shubha.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote
------------------------------------------------------------------------------
When a subclass's constructor calls the super class's constructor...Why it always invokes the default constructor(i.e which takes no arguments.) of super class, Even if there is the constructor of super class with same arg.
of calling constructor ???
------------------------------------------------------------------------------

Whenever you create an object of child class, compiler first try to initialize your parent class's object. If you dont call your parent class constructor explicitly which take some arguments then compiler call the default constructor to initialize the parent object and if you want to initialize your child Object with some special value then you have to create its object by passing the value according to the constructor you have defined in your child class and if you have defined any constructor other than the defualt constructor to inialize your parent class object with some specific state, then you have to call the parents class constructor in your child class constructor, if you dont call any of the parents class constructor in your child class constructor then compile calls the defualt constructor of parents class to initiaze its object.


Regards,
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Every constructor(argument or no argument) the first line is super().so it calls the no arg constructor of the super class.

read the following code.

class Super
{
Super(int a){
System.out.println(a);//1st call goes here and prints 3.
}

Super(){
System.out.println("default Super");
}

}

class Sub extends Super{

Sub (int w) //2nd call come here,but here 1st line is implicitly super().
{
//super(); //it calls the no argument constructor of the super class.
System.out.println("Sub w");
}

Sub() //3rd call come here,here the 1st line is implicitly super
{
//super(); //it calls no argument constructor of the super class.

System.out.println("Sub default");
}

public static void main(String args[]){
Super t=new Super(3);//1st call
Sub t1=new Sub(5);//2nd call
Sub t2=new Sub();

}
}


the o/p:
3
default Super
Sub w
default Super
Sub default


with regards,
P.Ramu
SCJP 1.4
 
Piyush Sam
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramu,


-------------------------------------------------------------------------------
Every constructor(argument or no argument) the first line is super().so it calls the no arg constructor of the super class.
-------------------------------------------------------------------------------

I dont think so, bcoz if u explicitly call any of super class's constructor other than the defualt one then its not always the defualt constructer super() gets called, but the one you are calling explictly will get called

consider the following piece of code

class Base {
public Base() {
System.out.println("base default constructor");
}

public Base(int i) {
System.out.println("base constructor:" + i);
}
}

class Derived extends Base {
Derived() {
this((int) 9);
System.out.println("derived default constructor");
}

Derived(int j) {
super(j);
System.out.println("derived constructor" + j);
}

}

public class Q01 {
public static void main(String args[]) {
Derived obj = new Derived();
}

}

O/P of the code is:

base constructor:9
derived constructor9
derived default constructor

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

Originally posted by shubha an.:
Hi friends.

When a subclass's constructor calls the super class's constructor...Why it always invokes the default constructor...

-----------
Shubha.



The only constructor that is ever implicitly called is the default constructor. All others require you to make the call explicitily.

Consider a constructor whose superclass contains a default constructor and one that accepts a File parameter. Clearly you need full control over what constructors are called. Even when you omit the call to super() it's really just a convenience; you're electing to call it at that point.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what K&B says:

Every constructor must have as its first statement either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()).

If you do type in a constructor (as opposed to relying on the compiler generated default constructor), and you do not type in the call to super(), the compiler will insert a no arg call to super() for you.

However, I added this as the first line in one of my overloaded constructors in my subclass but it still called the super class default constructor first and then the subclass constructors. So I guess the rule is that the super class default constructor gets called no matter what.

class SuperClass{
public SuperClass(){
System.out.println("Call to Super");
}
}

public class SubClass extends SuperClass{
public SubClass(){
System.out.println("Call to Sub");
}

public SubClass(int i){
this();
System.out.println("Call to overloaded SubClass Constructor");
}

public void subMethod(){
System.out.println("Call to subMethod");
}

public static void main(String r[]){
new SubClass(1);
}
}
 
Get meta with me! What pursues us is our own obsessions! But not 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