how change in child class instance variable also changes val
Posted: Mon Aug 14, 2017 6:00 am
class Feline {
public String type = "f ";
public Feline() {
System.out.print("feline ");
}
}
public class Cougar extends Feline {
public Cougar() {
System.out.print("cougar ");
}
void go() {
type = "c ";
System.out.print(this.type + super.type);
}
public static void main(String[] args) {
new Cougar().go();
}
}
In above code we have changed value of the inherited variable ("type") inherited from parent class from 'f' to 'c' . So (this.type) gives current class instance variable("type") value which is 'c' , because we change it inside method go() . OK , but why that change is also reflected in parent class since (super.type) also gives 'c' . But How.How change in child class instance variable also changes value of base class instance variable ?
public String type = "f ";
public Feline() {
System.out.print("feline ");
}
}
public class Cougar extends Feline {
public Cougar() {
System.out.print("cougar ");
}
void go() {
type = "c ";
System.out.print(this.type + super.type);
}
public static void main(String[] args) {
new Cougar().go();
}
}
In above code we have changed value of the inherited variable ("type") inherited from parent class from 'f' to 'c' . So (this.type) gives current class instance variable("type") value which is 'c' , because we change it inside method go() . OK , but why that change is also reflected in parent class since (super.type) also gives 'c' . But How.How change in child class instance variable also changes value of base class instance variable ?