Saturday, 15 January 2011

java - Why does super not refer to the variable in super class? -



java - Why does super not refer to the variable in super class? -

in below code, why super still referring subclass variable, , not super class variable?

class feline { public string type = "f "; public feline() { system.out.print("feline "); } } public class cougar extends feline { public cougar() { system.out.print("cougar "); } public static void main(string[] args) { new cougar().go(); } void go() { type = "c "; system.out.print(this.type + super.type); } }

what subclass variable? haven't declared one, this.type refers superclass's variable.

if declared public string type; in both superclass , subclass, work way expecting. right now, type variable you've declared 1 on superclass.

also, shadowing variables bad practice, gets confusing type mean.

java super

No comments:

Post a Comment