0

Just trying to clarify the difference between overloaded & overridden methods... Consider the scenario below;

Say I have a class, let's say 'class Master'. Suppose I have sub class, 'class Apprentice', such that the two classes are related by inheritance.

       public class Apprentice extends Master

Hypothetically considering that the master class contains 2 void methods each named attack, one takes a Single string argument, the other a String and an Int argument.

        public class Master{
void attack(String bodyPart){
//code for attacking
}

void attack(String bodyPart, int Damage){
//code for specific attack
}

If the Apprentice class has 2 methods named the exact same which takes exactly similar arguments, would the attack Method defined in the Master class be overloaded or overridden?

Wouldn't it be both overridden and overloaded!?

RamanSB
  • 1,162
  • 9
  • 26
  • Search for the question asked. Read. *Only* if there are remaining questions, ask them. – user2864740 Jun 30 '15 at 21:55
  • It would be overloaded – Toumash Jun 30 '15 at 21:55
  • The `attack` method is overloaded in class `Master` and each overloaded method would be overridden by a method in class `Apprentice`. – Ted Hopp Jun 30 '15 at 21:56
  • 1
    `Apprentice.attack(String)` would override `Master.attack(String)`. `Apprentice.attack(String, int)` would override `Master.attack(String, int)`. `attack(String)` and `attack(String, int)` are overloads. – Mattias Buelens Jun 30 '15 at 21:57

1 Answers1

1

Overriding:

 class A {
     public void do() { /* ... */ }
 }

 class B extends A {
     @Override
     public void do() { /* ... */ } // have an other definition than the one in A
 }

Overloading:

class C {
    public void do(E e) { /* ... */ }
    public void do(E e, F f) { /* ... */ } // same name but different arguments
}

They are two different concepts.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118