-1

I get them but i don't understand why our professor says that this is override. Shouldn't it be override since the parameter type is different than that of the same method with the same name?

public void stuff(char x) { // overloading? overriding?
    System.out.println("x is a char in overRide");
}

I provided some code below for the context

class OverLoad {
    public int xyz = 9;

    public void stuff(int x) {
        System.out.println("x is an int in OverLoad");
    }

    public void stuff(double x) {
        System.out.println("x is a double in OverLoad");
    }
}

class OverRide extends OverLoad {
    public int xyz = 99; // don't do this! creates confusion  public void 

    stuff(char x) { // overloading? overriding?
        System.out.println("x is a char in overRide");
    }

    public void stuff(int x) {
        System.out.println("x is an int in overRide");
        System.out.println("call parent stuff(int x)");

        super.stuff(x);
    }
}

Bor Laze
  • 2,458
  • 12
  • 20
  • 6
    Possible duplicate of https://stackoverflow.com/q/837864/2988730 – Mad Physicist Jul 18 '19 at 14:15
  • 2
    @Antoniossss Please format your question properly. – Mad Physicist Jul 18 '19 at 14:15
  • 1
    Overloading is when you have multiple methods with the same name but different arguments. Overriding is when you change implementation of given version of superclass' method in extending class. – Antoniossss Jul 18 '19 at 14:16
  • Overriding is implementing a method that already exists in the super class. Overloading is implementing multiple versions of a method with different parameters. – Dwagh Jul 18 '19 at 14:19

2 Answers2

0

You should try to use the code format when posting code so that it is easier to read.

Overriding a method is when you are redefining a method obtained from another class, such as with inheritance. People often override the toString() method that classes inherit for example. You always annotate them with @Override.

Overloading is when you are defining methods that have the same name, but a different signature (such as return type or parameters), e.g:

public void add(int x, int y){};

public void add(double x, double y){};
omoshiroiii
  • 643
  • 5
  • 11
0

'overloading' is the notion of having different methods in the same type that nevertheless have the same name:

public class Animal {
    void m(int x) {System.out.println("Animal::int";}
    void m(double y) {System.out.println("Animal::double";}
}

This is called overload because.. that's what it is called. Why is 'red' called 'red'?

Note that in java, which method overload is chosen is determined at compile time.

'overriding', on the other hand, is redefining the implementation of a method in your supertype. So:

public class Dog extends Animal {
    void m(int x) {System.out.println("Dog::int";}
}

Here the m method in the Dog class overrides one of the two m methods in Animal.

This is NOT determined at compile time, but at runtime:

Animal a = new Dog(); // legal java; all Dogs are Animals
a.m(5); // prints "Dog::int", not "Animal::int"
a.m(10.0); // prints "Animal::double".

play around with this code and I'm sure you'll figure it out.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72