Yes, static method can not be overridden.
The static method you have overriden is not actual overriding(because
it is static), you are simply hiding another static method.
If you try to override a static method, the compiler doesn't actually
stop you - but just doesn't do what you think it does.

And yes if you override an instance method then only you will be
implementing actual polymorphism. If you override a static method,
compiler will not stop you but you will not be actually achieving
polymorphism.

hope that helps.

<Neeraj Sidhaye/>
http://ExtremePortal.blogspot.com




On Jan 2, 11:46 pm, nn roh <[email protected]> wrote:
> Hi ,
>
> Following program shows the overriding ,the ouput is:
> classMethod() in Foo
> instanceMethod() in Bar
>
> it means we can't override static methods , why??
>
> class Test {
>     public static void main(String[] args) {
>   Foo f = new Bar();
>         f.classMethod();
>          f.instanceMethod();
>     }
>
> }
>
> class Foo {
> protected  int i=3;
> static void classMethod() {
>         System.out.println("classMethod() in Foo");
>     }
>
>     public void instanceMethod() {
>         System.out.println("instanceMethod() in Foo");
>     }
>
> }
>
> class Bar extends Foo {
>  static   public  void classMethod() {
>         System.out.println("classMethod() in Bar");
>     }
>
>     public void instanceMethod() {
>         System.out.println("instanceMethod() in Bar");
>     }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to