Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Tuesday, January 27, 2009

Variable Hiding In Java

/**

* Write a program which ilustrates variable hiding.

* class S declares an instance variable named x of type Integer.

* class T extends S and has an instance variable named x of type StringBuffer.

* instantiate both of these classes.initialise and display the variable

* named x in each of these objects

*

* @author Ritesh

*

*/

public class Inher2 {

/**

* execution begin from here

*

* @param args

*/

public static void main(String[] args) {

// creating object of T class and display it

System.out.println("Display Values Of Member Variables : ");

new T(new Integer(1),new StringBuffer("Ram")).display();

}

}

/**

* class T extends S and has an instance variable named x of type StringBuffer.

* @author Ritesh

*

*/

class T extends S{

// member variable

StringBuffer x;

/**

* inialize members

* @param intObj Integer object

* @param strObj StringBuffer object

*/

T(Integer intObj,StringBuffer strObj) {

// calls the super class constructor

super(intObj);

// set the dbVar

x=strObj;

}

/**

* display all the variables of T and S

*

*/

public void display() {

System.out.println("\tFrom Class T -> display() :");

System.out.println("\t\tsuper.x : " + super.x);

System.out.println("\t\tx : " + x);

}

}

/**

* class S declares an instance variable named x of type Integer.

* @author Ritesh

*

*/

class S{

// member variable of type Integer

Integer x;

/**

* initialize the member variable

* @param intObj Integer object

*/

S(Integer intObj) {

// set the dbVar

x=intObj;

}

}

No comments:

Post a Comment

Followers