Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit4.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!53ab2750!not-for-mail From: "Leif Roar Moldskred" Subject: Re: Need help with a program. Message-ID: <50327C3D.57168.calajapr@time.synchro.net> X-Comment-To: yanamandra Newsgroups: comp.lang.java.programmer In-Reply-To: <50327C3D.57167.calajapr@time.synchro.net> References: <50327C3D.57167.calajapr@time.synchro.net> X-FTN-AREA: COMP.LANG.JAVA.PROGRAMMER X-FTN-MSGID: 1:261/38 e4bc52d3 X-FTN-REPLY: 1:261/38 e68a1124 Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.16a-Win32 NewsLink 1.98] Lines: 99 Date: Mon, 20 Aug 2012 18:58:40 GMT NNTP-Posting-Host: 69.21.70.65 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1345489120 69.21.70.65 (Mon, 20 Aug 2012 13:58:40 CDT) NNTP-Posting-Date: Mon, 20 Aug 2012 13:58:40 CDT Organization: tds.net Xref: csiph.com comp.lang.java.programmer:18243 To: yanamandra From: Leif Roar Moldskred yanamandra 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