• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor inheritence

 
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you printing it?
 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the init() method do?
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic