Need help with a program.

Y

yanamandra

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

}
}
=========
 
Y

yanamandra

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
 
Y

yanamandra

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
 
Y

yanamandra

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

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:
..
..
..

Thank you Lief.

I will check that again.

-Venu
 
L

Lew

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top