Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Need help with a program.

Reply
Thread Tools

Need help with a program.

 
 
yanamandra
Guest
Posts: n/a
 
      08-20-2012
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 + "}");

}
}
=========

 
Reply With Quote
 
 
 
 
yanamandra
Guest
Posts: n/a
 
      08-20-2012
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
 
Reply With Quote
 
 
 
 
yanamandra
Guest
Posts: n/a
 
      08-20-2012
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
 
Reply With Quote
 
yanamandra
Guest
Posts: n/a
 
      08-20-2012
On Aug 20, 4:46*pm, Leif Roar Moldskred <(E-Mail Removed)> 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-20-2012
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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-21-2012
On Mon, 20 Aug 2012 02:24:42 -0700 (PDT), yanamandra
<(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM



Advertisments