nus-cs2030 / 2021-s2

2 stars 2 forks source link

What is the difference between Overloading and Overriding? #20

Open bonsibyl opened 3 years ago

hypernuggets commented 3 years ago

Overriding is when you define the same method in a subclass. Then when you call the method using the subclass, the method defined in the subclass is used instead of the method defined in the superclass even though they have the same name

jiahuitan36 commented 3 years ago

Overloading is when two methods in the same class have the same name but different parameters

bonsibyl commented 3 years ago

omg thank you so much!

jethrokyq commented 3 years ago

Crucially, Overriding is a run-time concept while overloading is a compile-time concept. This means that overloading occurs when you have two or more methods in one class that have the same method name but different parameters.

ruthkangyr commented 3 years ago

I have the same question. Then for equals in Circle, I still don't really get the difference between overriding and overloading.

nhzaci commented 3 years ago

Let me give a more concrete example of overloading vs overriding, from our recitation.

Overloading

Overloading essentially mean that there is more than one implementation of the same method, ie. there are multiple methods of the same name, taking in different numbers of arguments.

For example, below, Rectangle has a method called scaleHeightBy that has two different implementations, one takes in an argument, scaleHeightBy(double factor), and the other does not take in any arguments, scaleHeightBy().

Overriding

Overriding on the other hand, usually occurs when you are inheriting from another class and you replace the exact same method with the exact same number of arguments to override that method, such that the method called is the one from the child class, rather than the parent class.

For example, below, Square overrides Rectangle's scaleHeightBy(double factor) method, so when you call scaleHeightBy(double factor) on a Square object, it calls the implementation from the Square and the implementation from the Rectangle is never reached.

Another common example is for the toString() method. All classes in Java will inherit from the base Object class. The base Object class has a toString() method and that is why you put an @Override tag on the public String toString() method, since all classes already have an existing implementation for toString().

Code

class Rectangle {
    ...
    // For Squares, the method is overridden, such that this does not get
    // run for any Square object.
    Rectangle scaleHeightBy(double factor) {
        ...
    }

    // The method scaleHeightBy is overloaded since there are now two
    // implementations, one with an argument of a factor, which is a double,
    // another without any arguments.
    Rectangle scaleHeightBy() {
        ...
    }
}
class Square extends Rectangle {
    ...
    @Override
    // Overrides method from super class, Rectangle, so this implementation 
    // is called instead of Rectangle's implementation
    Square scaleHeightBy(double factor) {
        ...
    }
}

Hope this helps!

tshuenhau commented 3 years ago

i like to think of overriding as "replacing" a method and overloading as "adding variations" (which accept different arguments) of the method.

ShyamalSankar commented 3 years ago

Overloading is when there are multiple methods with the same name but take in different parameters. For example in the lecture, adding a equals method in the FilledCircle class which takes in another FilledCircle is a way of overloading. If you pass in a Circle instead of a filled circle into the equals method, it would call the equals method of its parent class instead of using its own equals method.

On the other hand, if you override the method, there is no way for the filled circle class to access the equals method of Circle without a super call and it would always make use of the equals method in the FilledCircle class regardless of the arguments passed.

chengseong commented 3 years ago

So for Overriding, there already exists a method of the same name in the superclass, however you want to create a method with the same name in the subclass that is more specific to the subclass. So you "override" the original superclass method in the subclass but coding in another method with the same name and now your subclass uses this method whenever it is called instead of the superclass method with the same name.

Overloading on the other hand, is just using methods with the same names, but different parameters. In that sense, both methods can exist at the same time and the method that is being called is based on the parameters inputted into the method.

In some sense, you can see it as overriding looks at the class whereas overloading looks at the parameters.

ghost commented 3 years ago

I note that there have already been some comments made here. Just giving it another stab:

Firstly, Overriding implements Runtime Polymorphism whereas Overloading implements Compile time polymorphism. The method Overriding occurs between superclass and subclass. Overloading occurs between the methods in the same class.

Secondly, Overriding methods have the same signature i.e. same name and method arguments. Overloaded method names are the same but the parameters are different.

Thirdly, with Overloading, the method to call is determined at the compile-time. With overriding, the method call is determined at the runtime based on the object type.

Fourthly, If overriding breaks, it can cause serious issues in our program because the effect will be visible at runtime. Whereas if overloading breaks, the compile-time error will come and it’s easy to fix