Hi All I have doubt what is the difference between redefining and overriding. overriding is one way of redefining only? I am referring to page 147 of Kathy sierra static method can't be overridden but they can be redefined. what specifically it means by saying redefining static method?
Hi Sweety, As its pointed out static methods cannot be ovveridden[or re-defined] by subclass this means they will not behave polymorphically. For instace you have
Here in above code if you have the following Animal anim = new Cat(). anim.show(); Here anim is of type Animal but it is reffering to an instance of Cat. Now if you invoke anim.show(); since show is an instance method and not static method , Because of JAva Polymorphism, JVM will invoke the eat() version of the method that is defined in the Cat class and not Animal class. i.e., its the type of the object [Cat object here] that the reference variable refers to is used to determine which instance method to invoke and not the type of the reference variable [Animal Object]. This is such a powerful concept. Imagine you were asked to write a API were in you were asked passed a Animal , this array could have any type of Animal object, like CAt, Dog and so on. And you were asked to iterate through the array and invoke eat. With polymorphism you can do the following
The above code will automatically invoke the proper version of eat based on the type of the Animal object that the array holds. Also suppose you had a new Animal named Tiger in your application, then you will have to just define the eat() for the Tiger class, But the above API will not have to be changed . Remember any class you write must be "closed for modification" [It must be coded in that way] Without polymorphism you would have to do the following
Look at the number of if else , Also if you have Tiger class, then you will have to write another if-else for Tiger and hence modify makeAnimalsEat() API. This is the power of polymorphism.
So now lets talk about static methods. Say if you modify the above code to make eat() static methods as follows:
Animal anim = new Cat(). anim.show(); its type of the reference variable [Animal Object] that is used to determine which static method to be invoke and not the the type of the object [Cat object here] that the reference variable points to. Excatly opposite to instance methods. hence the output would be Animal eating.
The concept of polymorphism is not applicable for static methods. And it makes sense because to invoke a static method you dont need to create an instance of a class and can be invoked directly. In fact using the above syntax to invoke a static method, only causes confusiong. This is the reason static methods must be invoked using the class names and not reference variables. A correct and more clear invocation would be Animal.eat() or Cat.eat(). More details @ Overriding Vs Hiding
I hope this clears your doubt. My first post after getting certified Thanks Deepak SCJP 5.0 Preparing SCJP 6.0 [ January 02, 2008: Message edited by: Deepak Jain ]