• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About overriding

 
Narasimha Reddy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to override the static method.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you cannot override static method. Static method in derived class will hide the static method in the parent class.



Run the above code and see the output. The output should explain how static methods hide the base implementation.
[ December 03, 2004: Message edited by: Jay Pawar ]
 
Seema Manivannan
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can redefine the base class static method in your derived class, however this would not be overriding.

For example, assume you have a static method- static void f()- in your base class as well as the derived class, and you invoke it as shown.

Base b=new Derived();
b.f(); // this will invoke the method in the base class only

This is because static methods belong to the class and not to the object, and cannot be overridden. The method invoked is chosen according to the declared type of the reference variable.

Also, note that base class static methods cannot be redefined in the derived class as non static and vice versa. This will cause a compile time error.

Thanks,
Seema
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"narasimha" please read our JavaRanch Naming Policy and give your self two names like: <firstname><space><family name>.
Thankyou,
-Barry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic