Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding Vs Redefinition

 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am quite confused with redefinition. In KB book it mentioned that overrriding is different from Redefinition.

Any example with explanation would greatly appreciated..
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am quite confused with redefinition. In KB book it mentioned that overrriding is different from Redefinition.


When you override a method you open the door of having polymorphic behavior.
That is calling subclass version of the method using parent class reference.

So far redefing is concerned, there is nothing like polymorphism. Redefining takes place in case of static methods because static methods can't be overridden (they are class specific things).

Look at the following example:



You can see easily, while redefining a method is different, it simply hides the parent class method. While overriding a method gives you avail run time method invocation decision on behalf of what type of object your reference variable is holding instead of the type of reference variable(that is taking place when calling static methods).

Hope you got the concept!

Thanks and Regards,
cmbhatt
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we use covariant returns when re-defining?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Megha Joshi

Can we use covariant returns when re-defining?


Hi Megha,

While redefining a method, you can change the method return type. The actual meaning of covariant return is availed when we override a method so that run time polymorphism is assured.

Look at the code example below:



You may comprehend this!

Thanks and Regards,
cmbhatt
 
Sathya Shanmugam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks chan, now I got clear picture
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic