aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the difference b/w overriding and redifining 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 "what is the difference b/w overriding and redifining" Watch "what is the difference b/w overriding and redifining" New topic
Author

what is the difference b/w overriding and redifining

rohit shekhar
Ranch Hand

Joined: Mar 05, 2010
Posts: 32
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?

Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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:


SCJP 6
rohit shekhar
Ranch Hand

Joined: Mar 05, 2010
Posts: 32
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

Joined: Feb 02, 2010
Posts: 247
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

Joined: Mar 05, 2010
Posts: 32
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

Joined: Feb 02, 2010
Posts: 247
Simply change your own code by removing all the "static" keywords and you will have overriding.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...

BEE MBA PMP SCJP-6
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

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

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2012
OCEJPAD [83%] September 6th, 2014
If you find any post useful, click the "plus one" sign on the right
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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

Joined: Mar 05, 2010
Posts: 32
Thank you all...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Rohit you cal also read this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Excellent example by Larry.

Good work mate.


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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

Joined: Oct 13, 2009
Posts: 774
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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the difference b/w overriding and redifining