| Author |
Constructor inheritence
|
jeff mutonho
Ranch Hand
Joined: Apr 30, 2003
Posts: 271
|
|
Consider the two classes: public abstract class A { private boolean booleanVal = false; public A() { super(); } public A(boolean val) { this(); booleanVal = val; } } public class B extends A { public B() throws Exception{ super(true);//set booleanVal in superclass to true init(); } } Calling super(true) in B should set booleanVal to true.When I run this , and booleanVal is never set yo "true".Why is this?
|
 |
Stuart Ash
Ranch Hand
Joined: Oct 07, 2005
Posts: 637
|
|
|
How are you printing it?
|
ASCII silly question, Get a silly ANSI.
|
 |
jeff mutonho
Ranch Hand
Joined: Apr 30, 2003
Posts: 271
|
|
I try to use it in test , later in B as shown below : if(booleanVal){ System.out.println("booleanVal value set to true"); }else{ System.out.println("booleanVal still false"); } and "booleanVal still false" is printed.
|
 |
k Oyedeji
Ranch Hand
Joined: Jul 07, 2002
Posts: 96
|
|
|
What does the init() method do?
|
 |
Reghu Ram Thanumalayan
Ranch Hand
Joined: Oct 21, 2003
Posts: 193
|
|
Originally posted by jeff mutonho: I try to use it in test , later in B as shown below : if(booleanVal){ System.out.println("booleanVal value set to true"); }else{ System.out.println("booleanVal still false"); } and "booleanVal still false" is printed.
if booleanVal is private in A, how can you access it in B ? Most probably, I think you have defined another instance variable in B called 'booleanVal' which is why it is being printed as false.  [ November 28, 2005: Message edited by: Reghu Ram T ]
|
Cheers,<br />Reghu Ram T<br /> <br />SCJP 1.4 - 98 %, SCBCD 1.3 - 94 %, SCMAD 1.0 - 92 %
|
 |
Ramender Mall
Ranch Hand
Joined: Sep 08, 2005
Posts: 311
|
|
Buddy, You posted it in both the forums(intemediate and beginner).. Whatever.. I tried your program( in simplest of the forms)and it ran fine... It ran fine....See if there is any big difference between both the programs... In my little program depending on the boolean value passed to super,
public showFlaw() { super(true); }
, the println was responding accordingly... And after seeing the other part of your code it seems to be the same issue as pointed out by Raghu Ram.. Ramy.. [ November 28, 2005: Message edited by: Ramender Mall ]
|
 |
 |
|
|
subject: Constructor inheritence
|
|
|