Sorry but I'm kinda confused. I thought it should print A and then B. First the constructor of super class is executed before the consturctor of the child class. Correct? Thanks, deep
Joined: Mar 05, 2003
Sorry but I'm kinda confused.
Ah, the person I am looking for.
First the constructor of super class is executed before the consturctor of the child class. Correct?
Memory for the B object is allocated. Then the constructor B() is invoked. The first thing B() does is invoke A(). When the compiler is compiling the constructor A() for class A, how does it know what to assign to the variable a? I think, the constructor B() invokes A() and passes a reference to the B object under construction to the constructor A(). Or put another way, B() invokes A() and leaves a reference to the B object under construction hanging around on the stack for the constructor A() to use. [ April 10, 2003: Message edited by: Marlene Miller ]
Hi Deepak, What you said is correct. But you are suppose to get this output as: Class B Class B bcoz, when baseclass constructor is called it refers to the instance of child class(B), so while in SOP a.getClass() object prints Class B, it mean this refers to object B's Instance. This always refers to current instance, here the current instance is B. (A a = new B()); Correct me if I am wrong. Regards PREM
Yes Brian, you are right. This is also the reason why constructors should not make polymorphic calls: the overriding method could be accessing variables not initialized yet. ___________________________________________________ The declared (compile) type of this is the class containing the method in which this appears. this, within an instance method points the instance on which the method was invoked. this, within a constructor points the instance that is being constructed. In this way the parent constructor initializations are performed over the instance being created, as it should be this is called the hidden argument because you do not see it in the argument list, but it appears magically as the first local variable within the method or constructor. the hidden argument to a constructor is the reference to the object returned by new (just been allocated space)