Click to See Complete Forum and Search --> : A Very Simple Conceptual Question .....


VipulPathak
September 21st, 2000, 01:54 PM
Hi All !

Below is a Simple Java Source Code. Please Analyse it and express ur view, that what is the flow of program ?

/* FileName: L.java implements Inheritance in Java */

import java.lang.* ;

class Base
{
Base(int XX)
{
System.out.println("BAS : "+XX) ;
}
Base(int XX, float sdf)
{
System.out.println("BAS2 :"+XX) ;
System.out.println("BAS2 :"+sdf) ;
}
}

class Der extends Base
{
Der(int XXX)
{
super(XXX) ;
System.out.println("DER : "+XXX) ;
}
}

public class L
{
public static void main(String[] SHE_SHE)
{
Der D = new Der(10) ;
return ;
}
}




We know that, If a No constructor is written by programmer then Java implicitly provides a default constructor. But, if any kind of construct is written, then the default constructor must also be explicitly provided by the Programmer.
The Base Class doesn’t contain the Default Constructor then how is the Instance of Base class is able to created ?

We know that whenever a Child class Object is being Created, the Parent Class constructor is first called then the Derived class object is created. In this case, how is the Base class object first created even when the base class constructor doesn’t exists ?

If we remove the statement “super(XXX) ;”, the javac report “Error”. What special treatment the “super()” is giving ?
e.g. What fine is this super is doing and how ?

Thanks.




-Vipul Pathak.
Pathak Developers Pvt. Ltd.
Indore, (MP) INDIA.
ICQ# 65423070

PS: If this helps you then Rate it, so let me know How much is this helpful to you.

poochi
September 21st, 2000, 04:09 PM
> Please Analyse it and express ur view, that what is the flow of program ?

The result will be

"BAS : 10"
"DER : 10"

> We know that, If a No constructor is written by programmer then Java implicitly provides a default constructor

That's true...

> But, if any kind of construct is written, then the default constructor must also be explicitly provided by the Programmer

No... I will explain the default constructor concept here below...


class Base{
}

class Der extends Base{
}

public class L{
public static void main( String[] s ){
Der d = new Der(); // right :
Der d2 = new Der(10); // Wrong : There is no such constructor defined in Der which takes int as an argument
}
}




In the above example , there is no constructor written explicity. To create an object ,
you need to provide atleast one constructor. If the developer doesnt provide any , the
java compiler created a constructor for you and insert's it in your code.
That dummy constructor is called "DEFAULT" ( Constructor with no arguments ). But it's not
compulsary that you HAVE to define the DEFAULT constructor all the time. But your class
has to have ATLEAST one constructor. And the developer can call only the constructors defined
in your class. In the above example , both the classes have default constructor only. So ,
when you create an object you can call the no argument constructor only.


class Base{
}

class Der extends Base{
public Der( int i ){}
}

public class L{
public static void main( String[] s ){
Der d = new Der(); // Wrong : No "no-arg" constructor defined in Der. Java compiler wont produce "default" constructor for you if there is a Constructor in that class.
Der d2 = new Der(10); // Right :
}
}






> We know that whenever a Child class Object is being Created, the Parent Class constructor is first called then the Derived class object is created

I dont think so... Whenever you create a Child <b>OBJECT</b>, the Parent <B>Object</b> will
be created and Whenever the child Constructor is called , parent constructor also will be
called.

> how is the Base class object first created even when the base class constructor doesnÂ’t exists

Object creation is nothing but creation of a chuck of memory. It's nothing to do with constructor.
constructor's job is to initialize the variables.

> If we remove the statement super(XXX) ;, the javac report “Error”. What special treatment the “super() is giving ?

The first call from the derive class constructor is making a call to base class constructor.
If you dont call the base class constructor explicitly , the default constructor of the base class will be called( no-arg constructor )implicity. If dont have no arg constructor in your base class and have other constructors , you will get compiler error.

Phill
September 21st, 2000, 04:38 PM
Hi there
It looks like you are studying for the
SCJP exam?
Anyway, if you didnt place the call to
super(int xxx) in the subclasses constructor
then the JVM would automatically place
a call to the super() no-args constructor on
the first line of the subclass constructor,
but the super() no-args constructor doesnt
exist so you receive an error.

But because you placed the super(int) call
the JVM doesnt call the no-args constructor
it calls the super(int) constructor.

The call to super is important so
that the subclass can inherit from
the superclass.
Its the same as if you dont provide a
constructor, the JVM provides a no-args
constructor for you.
Hope this is clear for you.
Good luck Phill.