Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding static methods

 
Divya Dronamraju
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class sup
{
static void method(){System.out.println("Super");} // 1
}

class test extends sup
{
public static void main(String args[]) {}
static void method(){System.out.println("test");}
}

Is this an example of overriding? I thought static methods cannot be overridden...they can only be hidden... This concept is not clear to me. Can someone help me understand this?

Thanks!
 
vijaya saradhi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Divya.

First of all static methods belong to class.And they are not per each and evry object, ie not per instance of a class.So overriding can be done only on
non static member functions (instance methods only).

Since static members are part of a class nt seperate for each and evry individual objects of a class, this overriding concept doesn't apply to them.

even though static methods are inherited, they cannt be invoked dynamically using parent class reference(ie no dynamic binding since static)

for eg:
note even though the object is of type B the static function of the
type A (base) is called



So they are hidden in subclass.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see Overriding vs. Hiding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic