+ 1
overriding field
5 ответов
+ 1
I can see you are trying anonymous classes, though you don't even have a method to override. Takes this as a simple example:
class Machine {
public int Age(int age) {
return age;
}
}
class Program {
public static void main(String[ ] args) {
Machine m = new Machine() {
@Override public int Age(int age) {
return age + 1;
}
};
System.out.println (m.Age(10));
}
}
+ 1
I can override method
+ 1
Am asking if we can override field
+ 1
No, not the way you are trying. This works however:
public class Vehicle {
static int maxSpeed = 5;
String color;
}
class MyClass {
public static void main(String[ ] args) {
Vehicle v1 = new Vehicle();
v1.color = "red";
System.out.println(v1.color);
v1.maxSpeed = 10;
System.out.println(v1.maxSpeed);
}
}
+ 1
Thanks...