aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A simple Q but still have doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A simple Q but still have doubt" Watch "A simple Q but still have doubt" New topic
Author

A simple Q but still have doubt

Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
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.


Shubhada
Piyush Sam
Greenhorn

Joined: Jun 02, 2005
Posts: 24
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,


Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
paramasivam Ramu
Ranch Hand

Joined: Mar 30, 2005
Posts: 39
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

Joined: Jun 02, 2005
Posts: 24
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,
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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.
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
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);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple Q but still have doubt