• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Difference between overridding & redefining a method

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any body explain the difference between method overridding & method redefinition.You can find a discussion in the Kathy - Bates book on Pg-147 but its not clear to me.

Thanks a lot.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can observe the differences at two places...1)while defining the method in the subclass and 2) which method is called at runtime.

For the first case: In method overriding, the subclass must define the method that matches the inherited version exactly(consider reurn type , access specifier etc.). While redefining, we are free to chose access specifier, we can broaden the exception in signature etc.

2) In method overriding, even if the method is called on the super type reference, actual method called is decided at runtime based on the actual object. But if the method has been simply redefined in the subclass, and the method is called on the supertype reference, then supertype method will only be called, no matter what the object is.
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you write few examples on redefining methods? (in java)

I don't see how non-static method can be redefined. I know you can overload it or override it, but AFAIK it can't be redefined.

AFAIK only static methods can be redefined, and here are retrictions too, you can't narrow visibility or broaden exceptions in signature.
 
sarika kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi john,
You are correct only static methods can be redefined and also there are restrictions. I thought of redefinning a method has similar rule as overloading. I should have tested it before writing. Thanks for correcting me.
 
Gourav Sharma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both of you Sarika & John for your reply.

As per your reply Sarika for the 2nd difference,it means that it is decided at compile time that which method is going to be invoked rather than runtime.

I've one more question.In the code given below for method redifinition,how do I understand by looking at the code that whether it is a method redefinition or an illegal method overriding if the question comes in the exam.Suppose if there is an option "Compilation Fails" one might choose that based on the concept of illegal method overriding.

class Animal{
static void doStuff(){
System.out.println("a");
}
}
class Dog extends Animal{
static void dostuff(){
System.out.println("d");
}
public static void main(String[] args){
Animal[] a={new Animal(), new Dog(), new Animal()};
for(int x=0;x<a.length;a++){
a[x].doStuff();
}
}

The o/p comes as :

a a a

Thanks a lot.
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If both are not static, then it can be overloading or overriding.
If both are static, it can be redefining or overloading.
If one is static and one non-static you have error.

It seems (based on my experiments), that restrictions for overriding non-static methods and redefining static methods are the same. (can't narrow visibility, can't widen exceptions, only return type different is not enough)
This would mean, that if it looks like wrong overriding, but static is used, then it is wrong redifinition -> In either case, you have error.

The only difference is "static" keyword used and as you wrote: invoked method is chosen at compile-time by the type of variable used. (see code below)

BTW: in class Dog you have dostuff and in Animal it is doStuff (capital 'S')



output is "A1 A1"
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic