Can we override instance/static members in Java?
What could happen if we did?
Asked
Active
Viewed 75 times
-5
-
possible dup of http://stackoverflow.com/questions/12589274/slight-confusion-regarding-overriding-where-variables-are-concerned – SSH Jun 06 '15 at 20:56
-
install any editor say eclipse - https://eclipse.org/downloads/packages/eclipse-ide-java-developers/lunasr2 and try it yourself!! Both of your doubts will be instantly clarified – spiderman Jun 06 '15 at 20:58
2 Answers
0
Yes you can.
public class HelloWorld{
public static void main(String []args){
Child.hello();
}
static class Parent{
public static int i = 0;
public static void hello(){
System.out.println("hello");
}
}
static class Child extends Parent{
public static int i = 1;
public static void hello(){
System.out.println("world");
}
}
}

Arthur Ceccotti
- 380
- 2
- 6
-1
No we can't override instance/static fields in java. Also static methods can't be overridden.

Prudvinath
- 15
- 1
- 5