File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriden Vs Re-Define 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 "Overriden Vs Re-Define" Watch "Overriden Vs Re-Define" New topic

Overriden Vs Re-Define

siva prasaad

Joined: Jan 10, 2007
Posts: 23
I had read static methods cannot be overriden but can be re-defined.

Can you please tell me the difference between overriding and Re-defineing. Thanks Siva
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
Hi Siva,

overriding applies to inheritance.
It means that you have the exact same notation of a method, or a covariant return type of a method, but with different body.

The covariant return means, that the overriding class can return anything that is a subclass of the original (in class Animal it is "Object") return type. Since "String" is a subclass of "Object", we are fine.

Static methods can NOT be overwritten, because they are class methods. Only instance methods can be overwritten.

Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
siva prasaad

Joined: Jan 10, 2007
Posts: 23
Thanks Tommaso Nuccio for your response.

I agree with you in case of overiden concept.

But i want to know what is the difference between overriden and re-define
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
Hi Prasad,

Prasad says:
But i want to know what is the difference between overriden and re-define

In one line,
Overriding means polymorphism can be availed.
Redefining means no polymorphism.

Polymorphism means, being able to bind subclass methog at run time
using the base class reference variable.
Animal ani = new Dog();; //at run time Dog's eat will be selected. (if dog overrides it) This is called polymorphism.

Redefining, you can understand in case of static method. methods will be selected on behalf of what is the type of reference type instead of what object it is referring to.


Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

redifining can be done
1) with statics
2) with private

Example for the latter:

Also with redifining private methods you won't see polymorphy, but here only because it just doesn't compile that way.

[ April 16, 2007: Message edited by: Burkhard Hassel ]

all events occur in real time
I agree. Here's the link:
subject: Overriden Vs Re-Define
It's not a secret anymore!