• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the difference b/w overriding and redifining

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please read this para(it was in SCJP java 6 study guide)
Finally, remember that static methods can't be overridden! This doesn't mean they can't redefined in a subclass, but redefining and overriding aren't the same thing. And the example was this :
class Animal{
static void x(){
System.out.println("a ");
}
}
class Dog extends Animal{
static void x(){
System.out.println("b "); // its a redefinition not a override
}
}
.....
my problem is that if we will override method x() same code we have to write. Then what is the difference between redefinition and override?

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you try to call the Dog's x() method in a polymorphic way, you will see a good demonstrate of the difference between overriding and redefining. It is only with overriding can we get polymorphic behavior:

 
rohit shekhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some confusion still... can you please explain if i have to do override(that is not possible actually in case of static), what code i have to write in same code and the difference..
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That paragraph from SCJP 6 Study Guide implies that there is no other choice but to make all the x() methods non-static in order to override the parent's x() method.

So, parent method with no "static" modifier means override is possible.
Parent method with "static" modifier means override is impossible.
 
rohit shekhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what you told that is correct but my confusion is that what code they have written in book its look like method overriding any they are saying its not overriding its redefining. Can you please send one example that explains proper difference between overriding and redefinition. Thank you.
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply change your own code by removing all the "static" keywords and you will have overriding.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key lies in the behavior of this code, offered by Larry Chung:If x() in Dog overrides x() in Animal, the output is, "from Dog" as determined by the type of
the object, Dog. But the output is "from Animal" which means the method called was based
on the variable type, Animal. Another clue is the compiler warning about calling a static method
on an instance variable. With proper calling syntax, Animal.x() or Dog.x(), there is no confusion
about overriding versus redefinition.

Jim ... ...
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you want ask "what makes it redefinition by just adding static keyword to it? "
Well
Statics are always associated with the class and not the instance of the class
even if we call the static method or variable name using instance, implicitly it is called as className.methodName();
now as static methods are only associated with class only, making them inherit makes no difference
also
we override any method to make some more specific behavior by writing some more specific code by overriding the parent method
I am repeating again, as the static methods are associated with class itlself, they will be called on the type of the variable
now suppose we are writing a method
in the animal class in the Dog class we are writing the code as


when we call the method using

then the method in animal class gets called as it is static
as there is no sense in calling the method in dog class as overriding method as it does not actually overrides the method in animal class
it is called as method redefinition
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the support, Jim.

Rohit, to find out the difference you must answer the question I asked in the code snippet, "but what happens here?" Run the code snippet in a main() method first without and then with the "static" modifiers for the x() methods.
 
rohit shekhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all...
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rohit you cal also read this...
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent example by Larry.

Good work mate.
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Prithvi. I needed that kind of endorsement.

Rohit, be sure to try executing the excellent code examples in the tutorial Ankit pointed to.

 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That kind of endorsement is always there mate.

I quoted this as a reference over my blog. Congratulations for completing your 100th message by the way

Best Regards,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic