Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18238 > unrolled thread

Re: Need help with a program.

Started by"Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this>
First post2012-08-20 18:58 +0000
Last post2012-08-20 18:58 +0000
Articles 7 — 3 participants

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Need help with a program. "Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
    Re: Need help with a program. "yanamandra" <yanamandra@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
      Re: Need help with a program. "Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
        Re: Need help with a program. "yanamandra" <yanamandra@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
          Re: Need help with a program. "Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
            Re: Need help with a program. "yanamandra" <yanamandra@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
          Re: Need help with a program. "Lew" <lew@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000

#18238 — Re: Need help with a program.

From"Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this>
Date2012-08-20 18:58 +0000
SubjectRe: Need help with a program.
Message-ID<50327C3C.57164.calajapr@time.synchro.net>
  To: yanamandra
From: Leif Roar Moldskred <leifm@dimnakorr.com>

yanamandra <venu.yanamandra@gmail.com> wrote:
> Why does the function printall() print the base class (c1) member
> values? If that is reasonable, what is the method to print the
> extended class (cls) values using the base class function printall().

I take it school's back in session? The issue you're running into is called 
variable shadowing. Reading up on that should give you the answer to your 
problem.

--
Leif Roar Moldskred

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#18240

From"yanamandra" <yanamandra@1:261/38.remove-fzq-this>
Date2012-08-20 18:58 +0000
Message-ID<50327C3D.57165.calajapr@time.synchro.net>
In reply to#18238
  To: Leif Roar Moldskred
From: yanamandra <venu.yanamandra@gmail.com>

On Monday, August 20, 2012 3:11:28 PM UTC+5:30, Leif Roar Moldskred wrote:
> yanamandra wrote: > Why does the function printall() print the base class
(c1) member > values? If that is reasonable, what is the method to print the > 
extended class (cls) values using the base class function printall(). I take it 
school's back in session? The issue you're running into is called variable 
shadowing. Reading up on that should give you the answer to your problem. -- 
Leif Roar Moldskred

But, variable shadowing is about the instance and local || local and scope 
specific. I have read the articles again after you asked me too. No luck on the 
exact problem I am mentioning.

I am talking about inherited variables. there is i1, i2 and i3 in the super 
class with a method printall. there are the same 3 variables in the derived 
class but there is no printall method in the sub class. When printall() is 
invoked from the derived class's object, it still prints the super-class's 
values.

And, if this is reasonable, what is the method by which I can get: 40, 50 and 
60 as the output from printall() when I invoke it using a derived class's 
object?

-Venu

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#18241

From"Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this>
Date2012-08-20 18:58 +0000
Message-ID<50327C3D.57166.calajapr@time.synchro.net>
In reply to#18240
  To: yanamandra
From: Leif Roar Moldskred <leifm@dimnakorr.com>

yanamandra <venu.yanamandra@gmail.com> wrote:
>
> I am talking about inherited variables. there is i1, i2 and i3 in
>  the super class with a method printall. there are the same 3
>  variables in the derived class but there is no printall method in
>  the sub class.

No, those are three _different_ variables which happen to have the same names 
as variables in the base class. You have _six_ variables in play here, not 
three. That's what's causing you problems.

--
Leif Roar Moldskred

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#18242

From"yanamandra" <yanamandra@1:261/38.remove-fzq-this>
Date2012-08-20 18:58 +0000
Message-ID<50327C3D.57167.calajapr@time.synchro.net>
In reply to#18241
  To: Leif Roar Moldskred
From: yanamandra <venu.yanamandra@gmail.com>

On Monday, August 20, 2012 3:46:52 PM UTC+5:30, Leif Roar Moldskred wrote:
> yanamandra wrote: > > I am talking about inherited variables. there is i1, i2
and i3 in > the super class with a method printall. there are the same 3 > 
variables in the derived class but there is no printall method in > the sub 
class. No, those are three _different_ variables which happen to have the same 
names as variables in the base class. You have _six_ variables in play here, 
not three. That's what's causing you problems. -- Leif Roar Moldskred

Yes, as per the output, it looks like that. But, where can I confirm that all 
three are different variables? Can I print addresses of the 3 variables, or is 
there any utility in java that can print the output similar to "nm"?

Also, for the output I need, 40, 50 and 60, I will have to re-write the 
printall() function. Is that the only way?

-Venu

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#18243

From"Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this>
Date2012-08-20 18:58 +0000
Message-ID<50327C3D.57168.calajapr@time.synchro.net>
In reply to#18242
  To: yanamandra
From: Leif Roar Moldskred <leifm@dimnakorr.com>

yanamandra <venu.yanamandra@gmail.com> wrote:
>
> Yes, as per the output, it looks like that. But, where can I confirm
>  that all three are different variables? Can I print addresses of the
>  3 variables, or is there any utility in java that can print the
>  output similar to "nm"?

I'm afraid there's no way to get hold of the address or reference to a 
primitive type in Java. To confirm that these are different variables you have 
to settle for the fact that they contain different values.

Consider the following (might contain typos):

public class Base {
  private int i = 0;

  public int getBaseI( ) {
    return i;
  }

  public void setBaseI( int value ) {
    i = value;
  }
}


public class Extended extends Base {
  private int i = 0;

  public int getExtendedI( ) {
    return i;
  }

  public void setExtendedI( int value ) {
    i = value;
  }
}

public class Test {
  public static void main( String[] args ) {
    Extended obj = new Extended( );

    obj.setBaseI( 5 );
    obj.setExtendedI( 10 );
    System.out.println( "Base: " + obj.getBaseI() + " Extended: " +
obj.getExtendedI() );
  }

}

>
> Also, for the output I need, 40, 50 and 60, I will have to re-write the
printall() function. Is that the only way?

As long as the two i2 variables are private, the only way to solve it is to 
override the printall() method in the extended class. A better way to do it is 
to have the printall() method refer to public or protected getter method and 
then override the getter method:

public class Base {
  private int i = 10;

  protected int getI( ) {
    return i;
  }

  public String toString() {
    return "i = " + getI( );
  }
}

public class Extended extends Base {
  private int i = 20;

  protected int getI( ) {
    return i;
  }
}

public class Test {
  public static void main( String[] args ) {
    Base base = new Base( );
    Extended ext = new Extended( );

    System.out.println( "Base:     " + base );
    System.out.println( "Extended: " + ext );
  }
}

--
Leif Roar Moldskred

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#18244

From"yanamandra" <yanamandra@1:261/38.remove-fzq-this>
Date2012-08-20 18:58 +0000
Message-ID<50327C3E.57169.calajapr@time.synchro.net>
In reply to#18243
  To: Leif Roar Moldskred
From: yanamandra <venu.yanamandra@gmail.com>

On Aug 20, 4:46 pm, Leif Roar Moldskred <le...@dimnakorr.com> wrote:
> I'm afraid there's no way to get hold of the address or reference to a
> primitive type in Java. To confirm that these are different variables
> you have to settle for the fact that they contain different values.
>
.
.
.
>
> > Also, for the output I need, 40, 50 and 60, I will have to re-write the
printall() function. Is that the only way?
>
> As long as the two i2 variables are private, the only way to solve it
> is to override the printall() method in the extended class. A better
> way to do it is to have the printall() method refer to public or
> protected getter method and then override the getter method:
>
.
.
.
> --
> Leif Roar Moldskred

Thank you Lief.

I will check that again.

-Venu

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#18246

From"Lew" <lew@1:261/38.remove-fzq-this>
Date2012-08-20 18:58 +0000
Message-ID<50327C3E.57171.calajapr@time.synchro.net>
In reply to#18242
  To: yanamandra
From: Lew <noone@lewscanon.com>

On 08/20/2012 03:39 AM, yanamandra wrote:
> On Monday, August 20, 2012 3:46:52 PM UTC+5:30, Leif Roar Moldskred wrote:
>> yanamandra wrote: > > I am talking about inherited variables. there is i1,
i2 and i3 in > the super class with a method printall. there are the same 3 > 
variables in the derived class but there is no printall method in > the sub 
class. No, those are three _different_ variables which happen to have the same 
names as variables in the base class. You have _six_ variables in play here, 
not three. That's what's causing you problems. -- Leif Roar Moldskred
>
> Yes, as per the output, it looks like that. But, where can I confirm that all
three are different variables? Can I print addresses of the 3 variables, or is 
there any utility in java that can print the output similar to "nm"?
>
> Also, for the output I need, 40, 50 and 60, I will have to re-write the
printall() function. Is that the only way?
>

Variables are not overridden, but methods can be.

All six variables in your question are 'static', meaning they belong to the 
class rather than the instance. The variables in the subclass "hide" (not 
"shadow") the declarations from the superclass.

You can make an overrideable instance method that reports the values of the 
(potentially hidden) variables as seen by the override.

The advice upthread to look up "shadowing", albeit the wrong concept, was not 
so far wrong that it wouldn't have led to the right concept, had you not 
summarily rejected it.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web