> Programming Languages > Java
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Java



Java - "Java overriding bug?" in Programming Languages


Old 09-05-2004   #1
..k..
 
Default Java overriding bug?

The code below prints Good Bye and false. Why it does not print Mary and true?
(If you change the var declaration to B obj = new B() - it works fine. )


cl*** A {
public boolean b_flag=false;
public String str_Flag="Hello";
}

cl*** B extends A {
public boolean b_flag=false;
public String str_Flag="Good Bye";
public String toString(){
return "B.toString() String:" + str_Flag + "B: boolean: " + b_flag;
}

}

public cl*** VariableOverridingTest {
public static void main(String[] args) {
A obj = new B(); // Prints Good Bye and false. Why?
// B obj = new B(); // Prints Mary and true
obj.str_Flag="Mary";
obj.b_flag=true;

System.out.println(obj);

}
}
 
Old 09-05-2004   #2
..v.. ..ls..
 
Default Re: Java overriding bug?

"Yakov" <yfain@hotmail.com> wrote in message
news:b04e2170.0409050910.1517ba3@posting.google.co m...
> The code below prints Good Bye and false. Why it does not print Mary and

true?
> (If you change the var declaration to B obj = new B() - it works fine. )
>
>
> cl*** A {
> public boolean b_flag=false;
> public String str_Flag="Hello";
> }
>
> cl*** B extends A {
> public boolean b_flag=false;
> public String str_Flag="Good Bye";
> public String toString(){
> return "B.toString() String:" + str_Flag + "B: boolean: " + b_flag;
> }
>
> }
>
> public cl*** VariableOverridingTest {
> public static void main(String[] args) {
> A obj = new B(); // Prints Good Bye and false. Why?
> // B obj = new B(); // Prints Mary and true
> obj.str_Flag="Mary";
> obj.b_flag=true;
>
> System.out.println(obj);
>
> }
> }


There are two separate members named b_flag and two separate members named
str_Flag. The toString() method builds a String using the members stored in
the instance of B, not the base (A). If you modify the members in A, that
does not affect the String that is returned by toString(), because it is not
using those members of A. Were you expecting your modification of A's
b_flag to change the value of B's b_flag?

--
David Hilsee


 
Old 09-06-2004   #3
..k..
 
Default Re: Java overriding bug?

>
> There are two separate members named b_flag and two separate members named
> str_Flag. The toString() method builds a String using the members stored in
> the instance of B, not the base (A). If you modify the members in A, that
> does not affect the String that is returned by toString(), because it is not
> using those members of A. Were you expecting your modification of A's
> b_flag to change the value of B's b_flag?


When I run this program using a debugger, I see two separate members
of each variable. I'm just wondering if Java has such thing as
variables overriding?
Based on my example, there is not. I wonder if this is described
somewhere in Java doc?
 
Old 09-06-2004   #4
..v.. ..ls..
 
Default Re: Java overriding bug?

"Yakov" <yfain@hotmail.com> wrote in message
news:b04e2170.0409051838.2538dae0@posting.google.c om...
> >
> > There are two separate members named b_flag and two separate members

named
> > str_Flag. The toString() method builds a String using the members

stored in
> > the instance of B, not the base (A). If you modify the members in A,

that
> > does not affect the String that is returned by toString(), because it is

not
> > using those members of A. Were you expecting your modification of A's
> > b_flag to change the value of B's b_flag?

>
> When I run this program using a debugger, I see two separate members
> of each variable. I'm just wondering if Java has such thing as
> variables overriding?
> Based on my example, there is not. I wonder if this is described
> somewhere in Java doc?


No, there is no such thing. I don't know of any resource that explains why
this is not provided in Java. A basic tutorial might cover the behavior.

--
David Hilsee


 
Old 09-06-2004   #5
..a..
 
Default Re: Java overriding bug?

Yakov wrote:
> When I run this program using a debugger, I see two separate members
> of each variable. I'm just wondering if Java has such thing as
> variables overriding?
> Based on my example, there is not. I wonder if this is described
> somewhere in Java doc?


You should never make your attributes visible, but write
accessor/mutator methods, which can be overridden.

Redefining fields from a supercl*** is called variable hiding btw, in
case you want to look it up. Never used it for anything myself, but
figure it may be a powerful techique for something..
 
Old 09-06-2004   #6
..r.. ..p..
 
Default Re: Java overriding bug?

Frank wrote:

> Redefining fields from a supercl*** [...]
> Never used it for anything myself, but
> figure it may be a powerful techique for something..


Yes, creating hard-to-find bugs...

;-)

For Yakov, the real authority here is the Java Language Specification (version
2), normally abbreviated to JLS2 or JLS by posters in this newsgroup. You can
buy it as a book, or it is on Sun's website somewhere in both PDF and HTML
forms.

-- chris



 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0