It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding problem" Watch "overriding problem" New topic

overriding problem

Sreenivasarao Venepalli

Joined: Aug 23, 2006
Posts: 1
Please see the below code
class Exam{
protected String diff = " easy ";
public void printDiff(){
public class Test1 extends Exam{
private String diff = " killing ";
public static void main(String[] args){
Test1 test = new Test1();
it is giving output as "easy" and "killing" I am invoking the super class method with subclass object, here object type is subclass only but at the time of execution why it is considering super class variable? and I tried this way also like I override parent class method in subclass and I tried to execute and it is printing "killing" 2 times..why it is?can anybody explain exactly what is happening here?

Thanks in advance.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Instance variables is bound at compile time.
I agree. Here's the link:
subject: overriding problem
It's not a secret anymore!