Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between redefining a method and overriding a method?

Tags:

java

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.

like image 291
enon Avatar asked Jan 28 '11 17:01

enon


People also ask

What is the difference between redefining and overriding a method?

Overriding facilitates class polymorphism. Overloading facilitates functional polymorphism. Redefining does neither and is an error.

What is the difference between redefining and overriding a method in Java?

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.

What is method redefinition?

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.

What is difference between method overriding and method hiding?

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.


4 Answers

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.

like image 66
Andrzej Doyle Avatar answered Nov 10 '22 08:11

Andrzej Doyle


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.

like image 23
justkt Avatar answered Nov 10 '22 10:11

justkt


Intention of overriding is actually Redefining the inherited method from the parent class.

Redefining involves:

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

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

like image 23
Murthy24 Avatar answered Nov 10 '22 08:11

Murthy24


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.

like image 32
Priyabrata G Avatar answered Nov 10 '22 10:11

Priyabrata G