aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between redefining and overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between redefining and overriding" Watch "difference between redefining and overriding" New topic
Author

difference between redefining and overriding

sweety singh
Greenhorn

Joined: Dec 04, 2006
Posts: 13
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?
Hamraj Kulshreshtha
Ranch Hand

Joined: Oct 06, 2007
Posts: 31
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();
p.add();
}
}

Here on calling p.add() method parent class method will be called, unlike the Overriding .


SCJP 1.5 91%
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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 ]

SCJP, SCWCD, SCBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between redefining and overriding