I tried with below example, it is working fine.
I expected it to pick sub-class's value since object won't be created for super class (as it is abstract). But it is picking up super class's field value only.
Please help me understand what is the concepts behind this?
abstract class SuperAbstract {
private int a=2;
public void funA() {
System.out.println("In SuperAbstract: this.a "+a);
}
}
class SubClass extends SuperAbstract {
private int a=34;
}
I am calling new SubClass.funA();
I am expecting it to print 34, but it is printing 2.
P.S.: What I want to know is why using this in an abstract class not giving me an error?
As below text is emphasizing this
would work on an instance and abstract classes won't have an instance.
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. from: http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html