• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor inheritence

 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you printing it?
 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the init() method do?
 
Reghu Ram Thanumalayan
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ramender Mall
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic