The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Use a public method in ancestor

Posted by Kishori Sharan on April 28, 2001 at 2:30 PM

Of course, memory is aloocated for private variables in ancestor when an object of descendant is created. However, you cannot refer to a private variable in ancestor using super in descendant. The correct way to using the private variables from ancestor is to provide pulic methods in ancestor which will return the private variable. For example,

class Ancestor {
private int num = 200 ;
public int getNum ( ) {
return num ;
}
}

class Derived extends Ancestor {
public static void main ( String[] args ) {
Derived d = new Derived ( ) ;
// You can use private variable num in ancestor calling
// getNum ( )
Sysyem.out.println ( "num in ancestor is " + getNum ( ) ) ;
}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us