Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriden Vs Re-Define

 
siva prasaad
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
siva prasaad
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
ani.eat(); //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.

Regards,
cmbhatt
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Yours,
Bu.
[ April 16, 2007: Message edited by: Burkhard Hassel ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic