What's the difference between redefining a method and overriding a method?
class DonkeyBattler {
static void doBattle(){
System.out.println("Weaponized donkey batt开发者_开发技巧ling");
}
}
class FunkyBattler extends DonkeyBattler {
static void doBattle(){
System.out.println("Weaponized donkey battling with bellbottoms");
}
}
doBattle method is supposed to be a redefinition or an override? Oh this is Java by the way.
I've never heard of "redefine" as an OO term as applied to Java.
However, the example you give is not overriding, because static methods are not inherited, but are statically dispatched based on the type of the variable (as opposed to the dynamic dispatch that occurs with member methods).
I wouldn't call it a redefinition, though - you had a method called DonkeyBattler.doBattle
, and you've now defined a compiletely separate method called FunkyBattler.doBattle
.
The term "redefinition" isn't usually used with regards to Java methods and inheritance. There are two terms that are commonly used: "override" as you said, and "overload." To overload in Java is to create two methods in the same class with the same name but different signatures (number and/or types of arguments). For example:
public interface MyInterface
{
public int doStuff(int first, int second);
public int doStuff(double only);
}
To override is to do something like what you are doing in your example: create a child class with a method that has the same name and signature as a method in the parent class that will be used for all instances of the child class but not of the parent class or any other child classes of that parent.
The only issue with your example as it relates to overloading is the use of the keyword static
. Overriding is determined dynamically, but static methods by definition are not.
Intention of overriding is actually Redefining the inherited method from the parent class.
Redefining involves:
Replacement
1. **Replacement** is the case in which child class is overriding
The inherited method of parent class with a behavior(functionality) which is completely different from corresponding parent method and a sign for this process is not calling super.method() in the body of child method.
Refinement
2. Refinement is the case in which child is overriding inherited
The method from parent with a functionality related to parent method functionality, sign of this process is calling generally super.method() in the body of child method.
It is totally a wrong concept that a method can only be overloaded in the same class. Instead method can be overload in the subclass also.
Overriding and Redefining (also known as hiding) are almost the some thing except:
Overriding is for instance methods and Redefining or hiding is for Class methods. Redefining is only in case of Static methods as Static methods don't have a run time Polymorphism.
Please refer to the below code for clarification:
class Foo {
public static void classMethod()
SOP("classMethod() in Foo");
}
public void instanceMethod()
{
SOP ("instanceMethod() in Foo");
}
}
class Bar extends Foo {
public static void classMethod()
{
SOP ("classMethod() in Bar");
}
public void instanceMethod() {
SOP ("instanceMethod() in Bar");
}
}
class Test {
public static void main(String[] args) {
Foo f = new Bar();
f.instanceMethod();
f.classMethod();
}
}
The Output for this code is: instanceMethod() in Bar classMethod() in Foo
Reason for this Output:
Since instanceMethod () is an instance method, in which Bar overrides the method from Foo, at run time the J.V.M. uses the actual class of the instance f to determine which method to run. Although f was declared as a Foo, the actual instance we created was a new Bar(). So at run time, the J.V.M. finds that f is a Bar instance, and so it calls instanceMethod () in Bar rather than the one in Foo.
With classMethod () , as it's a class method, the compiler and J.V.M. don't expect to need an actual instance to invoke the method. And even if you provide one the J.V.M. will never look at it. The compiler will only look at the declared type of the reference, and use that declared type to determine, at compile time, which method to call. Since f is declared as type Foo, the compiler looks at f.classMethod() and decides it means Foo. class Method. It doesn't matter that the instance referred to by f is actually a Bar - for static methods, the compiler only uses the declared type of the reference.
That's why I said in the beginning that a static method does not have run-time polymorphism.
Redefinition is valid for Java and it is for static methods. If you want to change what a static method does for your class you can redefine it. This is not effected by polymophism since that doesn't apply to static methods.
Using your example all calls to the class name or all sub classes will get the original doBattle( )
method. A specific call to FunkyBattler.doBattle()
will invoke the redefined static method in FunkyBattler
class.
You have method "doBattle" which is static and in Java static methods cannot be overridden, This doesn't mean they can't be redefined in a subclass, Thus here you are redefining the method doBattle in FunkyBattler class a subclass of DonkeyBattler
public class Animal{
public static void doStuff(){
System.out.println("In animal");
}
}
public class Dog extends Animal {
//redifinition of the method, since it is... Static
static void doStuff() {
System.out.println("In Dog...");
}
public static void main(String[] args) {
Animal animal = new Dog();
animal.doStuff();
}
}
The output will be 'In animal' even if you called the static method of reference to Dog, you should deduce then that runtime polymorphism doesnt work as with OVERRIDING.
Redefining and Overriding comes with in the same scenarios. Only difference is that if methods used are Static, its redefining.
ex:
Overriding:
Class A{
public void show(){
SOP("class a");
}
}
Class B extends A{
public void show(){
SOP("class B");
}
}
Redefining:
Class A{
public static void show(){
SOP("class a");
}
}
Class B extends A{
public static void show(){
SOP("class B");
}
}
Note: Static methods looks as if they are over-rided but they are actually redefined.
- Redefining is with Static Methods.
- Static methods are associated with Class and not with Object, so we do not override as per instance for run-time.
- In case of static we are just redefining the method.
精彩评论