Overriding

It is not possible to override a static method, because of early binding.


\begin{lstlisting}
package teststatic ;
\par
class Parent {
void show() {
Syst...
...c = new Child();
// calling Child ’s show ()
c.show();
}
}
\end{lstlisting}

In non-static method, we can override a non-static method. Because for override we need runtime polymorphism, which is happens only in runtime binding.


\begin{lstlisting}
package teststatic ;
\par
class Parent {
static void show() ...
...= new Child () ;
// calling Child ’s show ()
c.show();
}
}
\end{lstlisting}

This code throws below error in Eclipse.


\begin{lstlisting}
Multiple markers at this line
- This instance method cannot override the static
method
from Parent
\par
\end{lstlisting}