aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static methods : Difference between redefining and overriding ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods : Difference between redefining and overriding ? " Watch "Static methods : Difference between redefining and overriding ? " New topic
Author

Static methods : Difference between redefining and overriding ?

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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:




SCJP 6. Learning more now.
Achilleas Achix
Ranch Hand

Joined: Apr 18, 2011
Posts: 123

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.


OCPJP 6.0
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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

Joined: Mar 24, 2008
Posts: 3855

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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

Joined: Mar 24, 2008
Posts: 3855

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

Joined: Jul 11, 2010
Posts: 247

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...


SCJP 6 [SCJP - Old is Gold]
priyanka bandi
Greenhorn

Joined: Jul 17, 2013
Posts: 1
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

Joined: Apr 12, 2009
Posts: 449

There is also a FAQ which might be helpful,


http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods : Difference between redefining and overriding ?