• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Static methods : Difference between redefining and overriding ?

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The K&B SCJP 1.6 book says static methods cant be overridden, but can be redefined. What is redefining ? Is it just changing what the print statements do, as shown in the book example ?

Here is the page and code :

Finally, remember that static methods can't be overridden! This doesn't mean they
can't be redefined in a subclass, but redefining and overriding aren't the same thing.
Let's take a look at an example of a redefined (remember, not overridden), static
method:



 
Ranch Hand
Posts: 125
Postgres Database BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding happens at runtime VS Redefining happens at compile time.
Overriding means the Object's type dictates which method to use, and not the reference variable type, and this happens in runtime VS Redefining means that the reference variable type determines this, and this is done at compile time.

If you replaced static to normal in your example, you should get "a d a" as output.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Achilleas Achix wrote:Overriding happens at runtime VS Redefining happens at compile time.
Overriding means the Object's type dictates which method to use, and not the reference variable type, and this happens in runtime VS Redefining means that the reference variable type determines this, and this is done at compile time.



sorry, i cant understand what is happening.
<EDIT> What is the source of this information ?

Achilleas Achix wrote:
If you replaced static to normal in your example, you should get "a d a" as output.



Thats not the output i want.

 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Sudip Bose wrote:...What is redefining ? Is it just changing what the print statements do, as shown in the book example ?


Here redefining means that you declare a static method with the same signature as the supper class'. So the same method in super class is redefined in the subclass. As you already know that the static methods can't be overridden so this is what you can do if you want the static method to behave differently in the subclass. But remember you can't have the polymorphism since no overridden happens.

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:
Here redefining means that you declare a static method with the same signature as the supper class'. So the same method in super class is redefined in the subclass.



I am not clear about the meaning.
Are you saying that this is what redefining is - keep the same signature same and the code inside the method different ?
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Sudip Bose wrote:...Are you saying that this is what redefining is - keep the same signature same and the code inside the method different ?


Yes, if you change the signature it would be a different method altogether.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static Methods stick to the Class scope and not to the instance (or) object scope... That is static method cannot execute based on instance property variables....

So, when static methods are called, method binding is done at compile time based on Class type of the reference variable (or) direct Class name.

Even though the static method is created in child class with the same signature as what in parent class, static methods invocation confirmation will be done based on Class type of reference variable (or) direct Class name.


Redefining is something like changing the behavior logic of the method... what the difference between redefining and overriding is... Redefined method can be called only by specifying the correct Class which contains the redefined method.

This is not the case for Overriding... No need of specifying the Class explicitly to call the overriding method...

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it helped me a lot thankuuuu

Achilleas Achix wrote:Overriding happens at runtime VS Redefining happens at compile time.
Overriding means the Object's type dictates which method to use, and not the reference variable type, and this happens in runtime VS Redefining means that the reference variable type determines this, and this is done at compile time.

If you replaced static to normal in your example, you should get "a d a" as output.

 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also a FAQ which might be helpful,
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic