override - Overriding member variables in Java -


i studying overriding member functions in java , thought experimenting overriding member variables.

so, defined classes

public class a{     public int intval = 1;     public void identifyclass()     {         system.out.println("i class a");     } }  public class b extends {     public int intval = 2;     public void identifyclass()     {         system.out.println("i class b");     } }  public class mainclass {     public static void main(string [] args)     {         a = new a();         b b = new b();         aref;         aref = a;         system.out.println(aref.intval);         aref.identifyclass();         aref = b;         system.out.println(aref.intval);         aref.identifyclass();     } } 

the output is:

1 class 1 class b 

i not able understand why when aref set b intval still of class a?

when make variable of same name in subclass, that's called hiding. resulting subclass have both properties. can access 1 superclass super.var or ((superclass)this).var. variables don't have of same type; 2 variables sharing name, 2 overloaded methods.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

Why does math.random(999999999999) returns 1 in Lua? -