Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

Method Overloading Vs Method Overriding in Java

Method overloading refers to defining multiple methods with the same name but different parameters within a class, while method overriding refers to redefining a superclass method in a subclass. The key differences are that argument types can differ in overloading but must be the same in overriding, return types must be the same in overriding but can differ in overloading, and overriding involves inheritance between classes while overloading is defined within a class. Method resolution is static in overloading and dynamic in overriding based on the runtime object.

Uploaded by

Amol Adhangale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Method Overloading Vs Method Overriding in Java

Method overloading refers to defining multiple methods with the same name but different parameters within a class, while method overriding refers to redefining a superclass method in a subclass. The key differences are that argument types can differ in overloading but must be the same in overriding, return types must be the same in overriding but can differ in overloading, and overriding involves inheritance between classes while overloading is defined within a class. Method resolution is static in overloading and dynamic in overriding based on the runtime object.

Uploaded by

Amol Adhangale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Method Overloading vs Method Overriding in Java

There are the following differences between method overloading and method
overriding in Java. They are as follows:
1. Definition:
a. When a class has more than one method having the same name but
different in parameters, it is called method overloading in Java.
b. When the method of superclass is overridden in subclass to provide more
specific implementation, it is called method overriding in Java.
2. Argument type:
a. Argument type must be different (at least order) in overloading.
b. In overriding, argument type must be the same (including order).

3. Method signature:
a. The signature of the overloaded method must be different.
b. The signature of the overriding method must be the same.

4. Return type:
a. In method overloading, the return type can be the same or different.
b. In method overriding, the return type must be the same until Java 1.4
version but Java 1.5 onwards, method overriding can be done by changing
the covariant return type.
5. Class:
a. Method overloading is generally performed in the same class.
b. Method overriding is performed in two classes through inheritance (Is-A
relationship).
6. Private/static/final method:
a. Private, static, and final method can be overloaded in Java.
b. Overriding concept is not applicable to private, static, and final method.
Private, static, and final method can be overridden in Java.
7. Access modifiers:
a. In overloading, access modifiers can be anything or different.
b. In overriding, subclass method’s access modifier must be the same or
higher than superclass method access modifier i.e we cannot reduce the
visibility subclass method while overriding.
8. Throws clause:
a. Exception thrown can be anything in the overloading concept.
b. In case of method overriding, if child class method throws any checked
exception compulsory parent class method should throw the same checked
exception are its parent otherwise we will get compile-time error but there is
no restriction for an unchecked exception.
9. Method resolution:
a. Method resolution in overloading always takes care by the compiler based
on reference type.
b. Method resolution in overriding always takes care by JVM based on runtime
object.
10. Polymorphism:
a. Method overloading is also known as compile-time polymorphism, static
polymorphism, or early binding.
b. Method overriding is also known as runtime polymorphism, dynamic
polymorphism, or late binding.
Overloading vs Overriding in Tabular Form

A list of differences between overloading and overriding in Java is given below


for quick revision in tabular form.

S Property Overloading Overriding


N

1 Argument type Must be different Must be the same (including


(at least order). order).

2 Method Must be different. Must be the same.


signatures

3 Return type Same or different. Must be the same until Java 1.4
version only. Java 1.5 onwards,
Covariant return type is allowed.

4 Class Generally Performed in two classes through


performed in the Inheritance (Is-A relationship).
same class.

5 Private/ Can be Cannot be overridden.


Static/Final overloaded.
method

6 Access Anything or Subclass method’s access


modifiers different. modifier must be same or higher
than superclass method access
modifier.

7 Throws clause Anything If child class method throws any


checked exception compulsory
parent class method should
throw the same exception is its
parent otherwise we will get
compile-time error but there is no
restriction for an unchecked
exception.

8 Method Always take care Always take care by JVM based


resolution by java compiler on runtime object.
based on
reference type.

9 Polymorphism Also known as Also known as runtime


compile-time polymorphism, dynamic
polymorphism, polymorphism, or late binding.
static
polymorphism, or
early binding.

1 Performance Better Less


0

You might also like