File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Member variable overriding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Member variable overriding problem" Watch "Member variable overriding problem" New topic
Author

Member variable overriding problem

choks
Greenhorn

Joined: Nov 06, 2003
Posts: 1
Dear Ranchites,

Am confused with the principle of overriding when it comes to member variables. For example, in the following code:

class Exam {
public String difficultyLevel = "easy";
public void diffLevel() {
System.out.println("parentmethod");
}

public void getDifficulty() {
diffLevel();
System.out.println("parent variable " + difficultyLevel);
}

}
class SCJPExam extends Exam {
public String difficultyLevel = "killing";

public void diffLevel() {
System.out.println("childmethod");
}

}
class ourMain {
public static void main(String args[]){
SCJPExam myExam = new SCJPExam();
Exam ex = new Exam();
myExam.getDifficulty();
System.out.println("value is " + myExam.difficultyLevel);
}
}

On executing the following code,
OUTPUT
childmethod
parentvariable easy
value is killing

Both member method (diffLevel) and member variable (difficultyLevel) are overridden.

In getDifficulty(), when a method is called the child method is invoked. In a similar fashion when a variable is printed,its taking the parent variable.

Whats the reason for such a behaviour? What happens when a variable is overridden ? How does the JVM maintain both the variables ?

Please help me.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
choks,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We request that display names follow the pattern FIRST_NAME + SPACE + LAST_NAME.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by choks:
What happens when a variable is overridden?


Variables aren't overridden, they are hidden. That is, they aren't polymorphic, which one you access is determined by the compile time type of the reference you use.

Try adding

Exam exam = myExam;
exam.getDifficulty();
System.out.println("value is " + exam.difficultyLevel);

to your code and see how the method call and the field access behave differently.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Member variable overriding problem