Inheretance creating object unncessarily?
I have a doubt that the below program creates 3 objects unncessarily
class A
{
System.out.println("AAAAAAAAAAA");
};
class B extends A
{
B()
{
System.out.println("BBBBBBB");
}
};
class C extends B
{
C()
{
System.out.println("CCCCCCCC");
}
};
class Test
{
public static void main(String args[])
{
C c = new C();
}
}
When i m creating object of class C , the program calling 3 constructors i.e A(),B(),C()
which is unnecessarily creating 3 objects. Does it need to create 3 Objects?
thanks in advance
>> class C extends B
> {
> C()
> {
> System.out.println("CCCCCCCC");
> }
> };
>
>
> C c = new C();
Constructor for class C is called for new C()
as you can see it does not create any other object besides that of class C
Inheritance does not create objects, it specifies relationships
I think ur class A is error :P.As default, if you create an object, it will call the parent constructor.
Constructors don't create objects. new creates objects.
hi
only object of class C will be created ,but all the constructors up the heirarchy will be executed, extreme super class first i.e. A() then B() and thenC().
Object of A ,B will not be created can be proven
by declaring a private func in A and invoke it using object of C u will get compile time error which in case would be false if it had been object of A.
This example proves nothing except a lack of comprehension of (a) Java (b) inheritance and (c) the actual subject of this thread.
The example would always give a compile error. A private method is private.
The real point is that an object of class C is built up from an object of class B which in turn includes an object of class A.. A and B are not separate objects, they are part of the object C.
ejpa at 2007-7-14 21:27:05 >

which school did u go to?
thanks for ur reply.is there anyway so that I can check the number of objects created by the program. Does it need to call the super class constructor?What is the purpose of calling the superclass costructor.?Could you please clarify my doubts.thanks
> thanks for ur reply.
>
> is there anyway so that I can check the number of
> objects created by the program.
Google for java profiler.
> Does it need to call the super class constructor?
Yes. If you don't call it explicitly, the compiler inserts a call for you.
> What is the purpose of calling the superclass
> costructor.?
To make sure the superclass is properly initialized.
jverda at 2007-7-14 21:27:05 >

When the super class constructor is being called, can we say that super class object is created?
> When the super class constructor is being called, can> we say that super class object is created?No, you can say that basically the fields inherited from the super class are initialized.
> When the super class constructor is being called, can
> we say that super class object is created?
No. Constructors do not create objects. They initialize their state after they've already been created (usually by the new operator).
Exactly one object is created. It is an object of the subclass. It is also an object of the superclass.
If I say "build me a house," you haven't created a house and also a building--i.e., you haven't created two object. You've created one object-a house, and all houses are buildings, so that one house is both a house and a building.
The superclass' constructor is for initializing the state (the member varaibles) of the superclass "part" of the overall object.
jverda at 2007-7-14 21:27:05 >

thanks sir, I ve got my answer.thank u very much for clarify my doubt