class DonkeyBattler {
static void doBattle(){
System.out.println("Weaponized donkey battling");
}
}
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.
Overriding facilitates class polymorphism. Overloading facilitates functional polymorphism. Redefining does neither and is an error.
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.
The redefined method accesses the private components of the redefined class and not any private components of the same name in the superclass. In the redefined method, the implementation of the direct superclass can be called using super->meth.
In method overriding, when base class reference variable pointing to the object of the derived class, then it will call the overridden method in the derived class. In the method hiding, when base class reference variable pointing to the object of the derived class, then it will call the hidden method in the base class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With