This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

difference between redefining and overriding

Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sweety,

Since static methods doesn't behave polymorphically ,so it is said that redefining the method or hiding the method.

For example:

class Parent{
static void add(){System.out.println("In Parent");}
class Child extends Parent{
static void add(){System.out.println("In child");}

public static void main(String[] args){
Parent p = new Child();

Here on calling p.add() method parent class method will be called, unlike the Overriding .
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().;
Here anim is of type Animal but it is reffering to an instance of Cat. Now if you invoke; 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().;
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 or
More details @ Overriding Vs Hiding

I hope this clears your doubt.
My first post after getting certified
SCJP 5.0
Preparing SCJP 6.0
[ January 02, 2008: Message edited by: Deepak Jain ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!