0% found this document useful (0 votes)
1 views2 pages

Overriding Vs Overloading Java

Method overriding in Java involves redefining a parent class method in a subclass, requiring inheritance and maintaining the same parameters. In contrast, method overloading allows multiple methods with the same name but different parameters within the same class. Overriding is associated with runtime polymorphism, while overloading is linked to compile-time polymorphism.

Uploaded by

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

Overriding Vs Overloading Java

Method overriding in Java involves redefining a parent class method in a subclass, requiring inheritance and maintaining the same parameters. In contrast, method overloading allows multiple methods with the same name but different parameters within the same class. Overriding is associated with runtime polymorphism, while overloading is linked to compile-time polymorphism.

Uploaded by

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

Difference Between Method Overriding and Method Overloading in Java

Feature Method Overriding Method Overloading


Definition Redefining a parent class Defining multiple methods
method in a subclass with the same name but
different parameters
Class Requirement Requires inheritance (two Occurs within the same
classes involved) class
Parameters Must be exactly same as the Must be different (type,
parent method number, or order)
Return Type Same or covariant (subclass Can be same or different
of return type)
Polymorphism Runtime Polymorphism Compile-time
(dynamic binding) Polymorphism (static
binding)
Access Modifier Cannot be more restrictive No such rule
than parent method
Purpose To provide specific To perform different tasks
behavior in the subclass with the same method name

✅ Overriding Example
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}

class Dog extends Animal {


@Override
void sound() {
System.out.println("Dog barks");
}
}

✅ Overloading Example
class Calculator {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
}

🔍 Quick Summary:
- Overriding: Same name + same parameters, different classes

- Overloading: Same name + different parameters, same class

You might also like