Need help with a program.

Discussion in 'Java' started by yanamandra, Aug 20, 2012.

  1. yanamandra

    yanamandra Guest

    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 + "}");

    }
    }
    =========
    yanamandra, Aug 20, 2012
    #1
    1. Advertising

  2. yanamandra

    yanamandra Guest

    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 calledvariable 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 onthe exact problem I am mentioning.

    I am talking about inherited variables. there is i1, i2 and i3 in the superclass 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
    yanamandra, Aug 20, 2012
    #2
    1. Advertising

  3. yanamandra

    yanamandra Guest

    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 same3 > variables in the derived class but there is no printall method in > the sub class. No, those are three _different_ variables which happen to havethe same names as variables in the base class. You have _six_ variables inplay 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
    yanamandra, Aug 20, 2012
    #3
  4. yanamandra

    yanamandra Guest

    On Aug 20, 4:46 pm, Leif Roar Moldskred <> 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 theprintall() 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
    yanamandra, Aug 20, 2012
    #4
  5. yanamandra

    Lew Guest

    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
    Lew, Aug 20, 2012
    #5
  6. yanamandra

    Roedy Green Guest

    On Mon, 20 Aug 2012 02:24:42 -0700 (PDT), yanamandra
    <> 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
    Roedy Green, Aug 21, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wrecker
    Replies:
    3
    Views:
    378
    Kevin Spencer
    Jun 21, 2005
  2. pipi
    Replies:
    1
    Views:
    607
    Joseph Millar
    Jul 21, 2003
  3. Replies:
    1
    Views:
    458
    Walter Roberson
    Aug 12, 2005
  4. Replies:
    66
    Views:
    1,148
    Kenny McCormack
    Jun 18, 2006
  5. Kris Stark
    Replies:
    1
    Views:
    87
    John Bokma
    Sep 8, 2005
Loading...

Share This Page