Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods : Difference between redefining and overriding ?

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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:



 
Achilleas Achix
Ranch Hand
Posts: 123
BSD Java Postgres Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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...

 
priyanka bandi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also a FAQ which might be helpful,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic