[go: up one dir, main page]

0% found this document useful (0 votes)
45 views4 pages

Method Overloading and Method Overriding

Uploaded by

Rimsha Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

Method Overloading and Method Overriding

Uploaded by

Rimsha Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Method Overloading and Method Overriding

Understanding the difference between Method Overloading and Method


Overriding a plays a very important role in programming. These two are
the important concepts that help us to define multiple methods with the
same name but different behavior, both of these are used in different
situations. The main difference is in how each method is called and how
Java handles it.
 Method Overloading: It occurs when we have multiple methods in the
same class with the same name but have different numbers of
parameters. It allows to perform operations with different inputs.
 Method Overriding: It occurs when a subclass provides a specific
implementation for a method that is already defined in the superclass.

Method Overloading

Method Overloading is also known as compile-time polymorphism,


which means it is a polymorphism that occurs at compile time. In method
overloading, we have multiple methods in the same class with same name
but each method has a different number of parameters. In method
overloading the return type of these methods can be same or different, but
the important thing is each method must have different number of
parameters.
Key Points:
 Method overloading does not require inheritance.
 In method overloading, the return type can be the same or different, but
the parameter list must differ. Changing only the return type is not
method overloading.
 Method overloading improves code readability by allowing methods
with similar functionality to share a name.
Example: Here, in this example mutilple methods have the same name but
each method have different number of parameters.
#include <iostream>
using namespace std;

class Calculator {
public:
// Method to add two integers
int add(int a, int b) {
return a + b;
}

// Method to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Method to add two float numbers


float add(float a, float b) {
return a + b;
}
};

int main() {
Calculator calc;

cout << "Sum of 5 and 10: " << calc.add(5, 10) << endl;
cout << "Sum of 2, 4, and 6: " << calc.add(2, 4, 6) << endl;
cout << "Sum of 3.5 and 4.5: " << calc.add(3.5f, 4.5f) << endl;

return 0;
}

Advantages of Method Overloading

The advantages of Method Overloading are listed below:


 It allows same methods name to be used for different operations.
 It allows us to define multiple methods with the same name but with
different number of parameter
 Overloaded methods have the same name so we can easily update them
in one place rather than updating them in multiple method names.

Disadvantages of Method Overloading

The disadvantages of Method Overloading are listed below:


 Too many overloaded methods with similar signatures can make the
code more difficult to understand.
 Managing multiple overloaded methods with different parameters can
make debugging more difficult.
 To many overloaded methods can also increase the complexity of the
program
Method Overriding

Method Overriding is a type of runtime polymorphism. In method


overriding, a method in a derived class has the same name, return type, and
parameters as a method in its parent class. The derived class provides a
specific implementation for the method that is already defined in the parent
class.
Key Points:
 Method Overriding requires inheritance.
 In method overriding, method signature including name and parameter
must match exactly.
 In method overriding, return type must be same.
 In method overriding, private and final methods can not be overridden.
 @Override annotation ensures correct overriding.
Example: Here, in this example polymorphism allows calling the subclass
method through a parent class reference.

#include <iostream>
using namespace std;

class Parent {
public:
virtual void show() {
cout << "This is Parent class" << endl;
}
};

class Child : public Parent {


public:
void show() override {
cout << "This is Child class" << endl;
}
};

int main() {
Parent* p; // Base class pointer
Child c; // Derived class object
p = &c;

p->show(); // Output: This is Child class (due to overriding)

return 0;
}
Advantages of Method Overriding

The advantages of Method Overriding are listed below:


 It allows for dynamic method dispatch which enhance the flexibility.
 It allows changes to specific class behaviors without changing the entire
class hierarchy.
 We can easily extend classes and modify behaviors without altering the
parent class.

Disadvantages of Method Overriding

The disadvantages of Method Overriding are listed below:


 Overriding methods can make the code more complex in large
inheritance hierarchies.
 It is only useful in inheritance, it is not applicable for all design
situation.
 Overriding methods can also lead to unintentional behavior changes.

You might also like