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


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

Need help with a program.

Started byyanamandra <venu.yanamandra@gmail.com>
First post2012-08-20 02:24 -0700
Last post2012-08-20 17:22 -0700
Articles 10 — 4 participants

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


Contents

  Need help with a program. yanamandra <venu.yanamandra@gmail.com> - 2012-08-20 02:24 -0700
    Re: Need help with a program. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-08-20 04:41 -0500
      Re: Need help with a program. yanamandra <venu.yanamandra@gmail.com> - 2012-08-20 02:57 -0700
        Re: Need help with a program. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-08-20 05:16 -0500
          Re: Need help with a program. yanamandra <venu.yanamandra@gmail.com> - 2012-08-20 03:39 -0700
            Re: Need help with a program. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-08-20 06:46 -0500
              Re: Need help with a program. yanamandra <venu.yanamandra@gmail.com> - 2012-08-20 04:58 -0700
            Re: Need help with a program. Lew <noone@lewscanon.com> - 2012-08-20 07:31 -0700
              Re: Need help with a program. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-08-21 03:43 -0500
    Re: Need help with a program. Roedy Green <see_website@mindprod.com.invalid> - 2012-08-20 17:22 -0700

#18168 — Need help with a program.

Fromyanamandra <venu.yanamandra@gmail.com>
Date2012-08-20 02:24 -0700
SubjectNeed help with a program.
Message-ID<82bcd41c-df92-4103-b674-718cf64a999b@x8g2000yqf.googlegroups.com>
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().


=========
class c1
{
  public static int i1;
  private static int i2;
  protected static int i3;


  c1()
  {
    i1 = 10;
    i2 = 20;
    i3 = 30;
  }

  protected void printall()
  {
    System.out.println("From C1: i1 is: {" + i1 + "}, i2 is: {" + i2 +
"} and i3 is: {" + i3 + "}");
  }
}

class cls extends c1
{
  public static int i1;
  private static int i2;
  protected static int i3;

  cls()
  {
    i1 = 40;
    i2 = 50;
    i3 = 60;
  }

/*

*/

  public static void main(String[] args)
  {
    cls cls1  = new cls();
    cls1.printall();

    c1 cls2 = cls1;
    cls2.printall();

    System.out.println("From CLS: i1 is: {" + i1 + "}, i2 is: {" + i2
+ "} and i3 is: {" + i3 + "}");

  }
}
=========

[toc] | [next] | [standalone]


#18169

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-08-20 04:41 -0500
Message-ID<Rp-dnfT58e_Vm6_NnZ2dnUVZ8kydnZ2d@giganews.com>
In reply to#18168
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

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


#18170

Fromyanamandra <venu.yanamandra@gmail.com>
Date2012-08-20 02:57 -0700
Message-ID<8fc96fef-1eb1-41bb-a5d2-619ce71f5101@googlegroups.com>
In reply to#18169
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

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


#18171

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-08-20 05:16 -0500
Message-ID<ALednd5HHvkJk6_NnZ2dnUVZ8iadnZ2d@giganews.com>
In reply to#18170
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

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


#18172

Fromyanamandra <venu.yanamandra@gmail.com>
Date2012-08-20 03:39 -0700
Message-ID<ba00e484-489c-4b6b-af25-f1f6a0ebca54@googlegroups.com>
In reply to#18171
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

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


#18173

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-08-20 06:46 -0500
Message-ID<gMidneib4fYUvq_NnZ2dnUVZ8lmdnZ2d@giganews.com>
In reply to#18172
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

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


#18175

Fromyanamandra <venu.yanamandra@gmail.com>
Date2012-08-20 04:58 -0700
Message-ID<cff66c3a-a921-43e4-8fbf-5e866e91dddb@n8g2000yqn.googlegroups.com>
In reply to#18173
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

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


#18177

FromLew <noone@lewscanon.com>
Date2012-08-20 07:31 -0700
Message-ID<k0tho0$g4g$1@news.albasani.net>
In reply to#18172
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

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


#18264

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-08-21 03:43 -0500
Message-ID<2YCdnf385fmk167NnZ2dnUVZ8jKdnZ2d@giganews.com>
In reply to#18177
Lew <noone@lewscanon.com> wrote:
> 
> 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.

Whoops. Yes, my bad -- I overlooked that the variables were static.

> [SNIP]
> 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.

I think you're being a little too harsh here. Didn't look to me like
they "summarily rejected" anything; they just didn't understand how it
applied. These concepts can be tricky to get one's head around when
one first starts out.

-- 
Leif Roar Moldskred

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


#18258

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-08-20 17:22 -0700
Message-ID<r2l5385gc6m7bo691hbj5f7hkrlnrmn47i@4ax.com>
In reply to#18168
On Mon, 20 Aug 2012 02:24:42 -0700 (PDT), yanamandra
<venu.yanamandra@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>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().

It is confusing when you ignore coding conventions, e.g. classes have
capital letters.

http://mindprod.com/jgloss/codingconventions.html

see http://mindprod.com/jgloss/shadow.html
-- 
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04 

[toc] | [prev] | [standalone]


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


csiph-web