+ 4
we cannot override final method but can we do for static method how?
please help me with example
2 Respuestas
+ 11
To call a static method you don't need to create an object of the method containing class. That's the answer itself - if you don't have an object, where will the method be overridden?
0
[Question Extension]
#more #info
Then - extending the question - if I call the static method on the object (accepts it) then it is compiled as calling from the class?
Example:
class A {
static void X() { /*A*/}
}
class B extends A {
static void X() { /*B*/ }
}
code somewhere: {
A a = new B();
a.X();
}
So, a.X() runs like A.X() not caring about what the A variable contains (a B object)?
ANSWER:
I tested, and the A.X() runs :)
TEST: https://code.sololearn.com/ch4Qfgmyjy9q