I ran following code:
public class Cycling {
public static void main(String[] args) {
Cycle unicycle = new Unicycle();
System.out.println("Unicycle name: " + unicycle.name);
unicycle.ride();
}
}
class Cycle {
public String name = "cycle";
public void ride() {
System.out.println("Ride cycle!");
}
}
class Unicycle extends Cycle{
public String name = "unicycle";
public void ride() {
System.out.println("Ride unicycle!");
}
}
I expected this output:
Unicycle name: unicycle
Ride unicycle!
I got this output:
Unicycle name: cycle
Ride unicycle!
Method was successfully changed, but value of the argument was not. Can someone explain me why?