Java Programming Language
Table of content:
- History Of Java Programming Langauge
- Infographic For History Of Java
- What’s In The Name | History Of Java
- Key Features Of Java
- Advantages And Disadvantages Of Java
- The Version History Of Java Langauge
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is JDK?
- How To Download Java Development Kit (JDK) For Windows, MacOS, and Linux?
- Set Environment Variables In Java
- How To Install Java (JDK) On Windows 64-Bit Machine?
- How To Install Java (JDK) On Linux?
- How To Install Java (JDK) On macOS?
- How To Test Java Installation?
- How To Write Your First Java Program On Linux OS?
- Conclusion
- Frequently Asked Questions
Table of content:
- Java Programming Language | An Introduction
- 15 Key Features Of Java
- Write Once Run Anywhere (WORA) | Features Of Java
- Java Editions
- 5 New Features Of JAVA 8
- 5 New Features Of JAVA 11
- What Makes Java Popular?
- Conclusion
- Frequently Asked Questions
Table of content:
- What is Java?
- Advantages of Java
- Disadvantages of Java
Table of content:
- What Is Java Programming?
- Role Of Integrated Development Environments (IDEs) In Java Development
- 15 Best Java IDE For Developers
- In-Depth Comparison Table
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Differences Between Java And Core Java
- What Is Java?
- What Is Core Java?
- Applications Of Java
- Applications Of Core Java
- When To Use Java?
- When To Use Core Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Variables In Java Language?
- How To Declare Variables In Java Programs?
- How To Initialize Variables In Java?
- Naming Conventions For Variables In Java
- Types Of Variables In Java
- Local Variables In Java
- Instance Variables In Java
- Static Variables In Java
- Final Variables In Java
- Scope and Lifetime of Variables In Java
- Data Types Of Variables In Java (Primitive & Non-primitive)
- Java Variable Type Conversion & Type Casting
- Working With Variables In Java (Examples)
- Access Modifiers & Variables In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Identifiers In Java?
- Syntax Rules For Identifiers In Java
- Valid Identifiers in Java
- Invalid Identifiers in Java
- Java Reserved Keywords
- Naming Conventions & Best Practices For Identifiers In Java
- What Is An Identifier Expected Error In Java?
- Reasons The Identifier Expected Error Occurs
- How To Fix/ Resolve Identifier Expected Errors In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Data Types In Java?
- Primitive Data Types In Java
- Non-Primitive Data Types In Java
- Key Differences Between Primitive And Non-Primitive Data Types In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Operators In Java?
- Types Of Operators In Java
- Unary Operators In Java
- Arithmetic Operators In Java
- Assignment Operators In Java
- Relational Operators In Java
- Logical Operators In Java
- Bitwise Operators In Java
- Shift Operators In Java
- Increment & Decrement Operators In Java
- Ternary Operator In Java
- Instanceof Operator In Java
- Precedence & Associativity Of Java Operators
- Advantages & Disadvantages Of Operators In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Return Statement In Java?
- Use Cases Of Return Statements In Java
- Returning A Value From A Method In Java
- Returning A Class Object In Java
- Returning Void (No Value) In Java
- Advantages Of Using Return Statements In Java
- Limitations Of Using Return Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Keywords In Java?
- List Of Keywords In Java
- Detailed Overview Of Java Keywords With Examples
- What If When Keywords In Java Are Used As Variable Names?
- Difference Between Identifiers & Keywords In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Abstract Keyword In Java?
- Use Of Abstract Keyword In Java
- Abstract Methods In Java
- Abstract Classes In Java
- Advantages Of Abstract Keyword In Java
- Disadvantages Of Abstract Keyword In Java
- Abstract Classes Vs. Interfaces In Java
- Real-World Applications Of Abstract Keyword
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is throws Keyword In Java?
- How Does The throws Keyword Work?
- Throwing A Checked Exception Using throws In Java
- Throwing Multiple Exceptions Using throws In Java
- Throwing A Custom Exception Using throws In Java
- When To Use The throws Keyword In Java
- Difference Between throw and throws Keyword In Java
- Best Practices For Using The throws Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Volatile Keyword In Java?
- How Does Volatile Keyword In Java Work?
- Using Volatile Keyword In Java To Control Thread Execution
- Using Volatile Keyword In Java To Signal Between Multiple Threads
- Difference Between Synchronization And Volatile Keyword
- Common Mistakes And Best Practices While Using Volatile Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Super Keyword In Java
- Super Keyword In Java With Instance Variables
- Super Keyword In Java With Method Overriding
- Super Keyword In Java With Constructor Chaining
- Applications Of Super Keyword In Java
- Difference Between This And Super Keyword In Java
- Advantages Of Using Super Keyword In Java
- Limitations And Considerations Of Super Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding This Keyword In Java
- Uses Of This Keyword In Java
- Using This Keyword For Referencing Instance Variables
- Using This Keyword For Invoking A Constructor
- Using This Keyword For Invoking A Method
- Using This Keyword With Getters And Setters
- Difference Between This And Super Keyword In Java
- Best Practices For Using This Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is new Keyword In Java?
- Uses Of The new Keyword In Java
- Memory Management With new Keyword In Java
- Example 1: Creating An Object Of A Class Using new Keyword In Java
- Example 2: Creating An Array Using The new Keyword In Java
- Best Practices For Using new Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Transient Keyword In Java?
- Real-Life Example Of The Transient Keyword In Java
- When To Use The Transient Keyword In Java
- Example 1: Effect Of Transient Keyword On Serialization In Java
- Example 2: Skipping Sensitive Data During Serialization With Transient Keyword In Java
- Using Transient With Final Keyword In Java
- Using Transient With Static Keyword
- Difference Between Transient And Volatile Keyword In Java
- Advantages And Disadvantages Of Transient Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Static Keyword In Java?
- Characteristics Of Static Keyword In Java
- Static Variables In Java
- Static Method In Java
- Static Blocks In Java
- Static Classes In Java
- Static Variables Vs Instance Variables In Java
- Advantages Of Static Keyword In Java
- Disadvantages Of Static Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Static Method In Java?
- Use Cases Of Static Method In Java
- Using Static Method In Java To Create A Utility Class
- Using Static Method In Java To Implement The Singleton Design Pattern
- Difference Between Static And Instance Methods In Java
- Limitations Of Static Method In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Final Keyword In Java
- Final Variables In Java
- Final Methods In Java
- Final Classes In Java
- Difference Between Static And Final Keyword In Java
- Uses Of Final Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Difference Between final, finally, And finalize In Java
- What Is final Keyword In Java?
- What Is finally Keyword In Java?
- What Is finalize Keyword In Java?
- When To Use Which Keyword In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The extends Keyword In Java?
- Use Of extends Keyword In Java
- Using Java extends To Implement Single Inheritance
- Using Java extends With Interfaces (Default Methods)
- Overriding Using extends Keyword In Java
- Difference Between extends And implements In Java
- Real World Applications Of Extends Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Decision Making Statement In Java?
- If Statement In Java
- If-Else Statement In Java
- Else-If Ladder In Java
- Switch Statement In Java
- Ternary/Conditional Operator (?:) In Java
- Best Practices For Writing Decision Making Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Control Statements in Java?
- Decision-Making Control Statements In Java
- Looping Control Statements In Java
- Jump (Branching) Control Statements In Java
- Application Of Control Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Break Statement In Java?
- Working Of The Break Statement In Java
- Using Java Break Statement With Loops
- Using Java Break Statement With Switch Statement
- Using Java Break Statement With Infinite Loops
- Common Pitfalls While Using Break Statements In Java
- Best Practices For Using The Break Statement In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Switch Statement In Java?
- Working Of The Switch Statement In Java
- Example Of Switch Statement In Java
- Java Switch Statement With String
- Java Nested Switch Statements
- Java Enum In Switch Statement
- Java Wrapper Classes In Switch Statements
- Uses Of Switch Statement In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Syntax Of main() Method In Java
- public Specifier – Main Method In Java
- static Keyword – Main Method In Java
- void Return Type Of Main Method In Java
- The main Identifier – Main Method In Java
- String[] args In Main Method In Java
- The Role Of Java Virtual Machine (JVM)
- Running Java Programs Without The Main Method
- Variations In Declaration Of Main Method In Java
- Overloading The Main Method In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Method Overriding In Java?
- Example Of Method Overriding In Java
- Ideal Use Cases Of Method Overriding In Java
- Rules For Method Overriding In Java
- Super Keyword & Method Overriding In Java
- Constructor & Method Overriding In Java
- Exception Handling In Method Overriding In Java
- Access Modifiers In Method Overriding In Java
- Advantages & Disadvantages Of Method Overriding In Java
- Difference Between Method Overloading Vs. Method Overriding In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Method Overloading In Java?
- Different Ways Of Method Overloading In Java
- Overloading The main() Method In Java
- Type Promotion & Method Overloading In Java
- Null Error & Method Overloading In Java
- Advantages Of Method Overloading In Java
- Disadvantages Of Method Overloading In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Difference Between Overloading And Overriding In Java (Comparison Table)
- What Is Method Overloading In Java?
- What Is Method Overriding In Java?
- Key Differences Between Overloading & Overriding In Java Explained
- Difference Between Overloading And Overriding In Java Code Example
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A One-Dimensional Array In Java?
- Key Characteristics Of One-Dimensional Arrays In Java
- Declaration Of One-Dimensional Array In Java
- Initialization Of One-Dimensional Array In Java
- Common Operations On One-Dimensional Array In Java
- Advantages Of One-Dimensional Arrays In Java
- Disadvantages Of One-Dimensional Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Multidimensional Array In Java?
- Difference Between Single-Dimensional And Multidimensional Arrays In Java
- Declaring Multidimensional Arrays In Java
- Initializing Multidimensional Arrays In Java
- Accessing And Manipulating Elements In Multidimensional Arrays In Java
- Working Of Multidimensional Arrays With Jagged Arrays In Java
- Why Use Multidimensional Arrays In Java?
- Limitations Of Multidimensional Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Jagged Arrays In Java?
- Comparison With Regular Multi-Dimensional Arrays
- Declaring Jagged Arrays In Java
- Initialization Of Jagged Arrays In Java
- Printing Elements Of A Jagged Array In Java
- Accessing And Modifying Elements Of A Jagged Array In Java
- Advantages Of Jagged Arrays In Java
- Disadvantages Of Jagged Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Array Of Objects In Java?
- Declare And Initialize An Array Of Object In Java
- Example Of An Array Of Objects In Java
- Sorting An Array Of Objects In Java
- Passing Arrays Of Objects To Methods In Java
- Returning Arrays Of Objects From Methods In Java
- Advantages Of Arrays Of Objects In Java
- Disadvantages Of Arrays Of Objects In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Dynamic Array In Java?
- Why Use Dynamic Array In Java?
- What Is The Size And Capacity Of A Dynamic Array In Java?
- How To Create A Dynamic Array In Java?
- Managing Dynamic Data Input In Java
- Storing And Processing Real-Time Data In Java
- Use Cases Of Dynamic Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Return An Array In Java?
- How To Return An Array In Java
- Example 1: Returning An Array Of First N Squares
- Example 2: Doubling the Values of an Array
- Common Scenarios For Returning Arrays In Java
- Points To Remember When Returning Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding ArrayList In Java
- Differences Between Arrays And ArrayList In Java
- Returning An ArrayList In Java
- Common Use Cases For Returning An ArrayList In Java
- Pitfalls To Avoid When Returning An ArrayList In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Thread In Java?
- Thread Vs Process
- What is a Thread Life Cycle In Java?
- What Are Thread Priorities?
- Creating Threads In Java
- Java Thread Methods
- Commonly Used Constructors In Thread Class
- Thread Synchronization In Java
- Common Challenges Faced While Using Threads In Java
- Best Practices For Using Threads In Java
- Real-World Applications Of Threads In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Multithreading In Java
- Methods Of Multithreading In Java (Examples)
- Difference Between Multithreading And Multitasking In Java
- Handling Exceptions In Multithreading
- Best Practices For Multithreading In Java
- Real-World Use Cases of Multithreading In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Thread Priority In Java?
- Built-In Thread Priority Constants In Java
- Thread Priority: Setter & Getter Methods
- Limitations Of Thread Priority In Java
- Best Practices For Using Thread Priority In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Thread Synchronization In Java?
- The Need For Thread Synchronization In Java
- Types Of Thread Synchronization In Java
- Mutual Exclusion In Thread Synchronization In Java
- Coordination Synchronization (Thread Communication) In Java
- Advantages Of Thread Synchronization In Java
- Disadvantages Of Thread Synchronization In Java
- Alternatives To Synchronization In Java
- Deadlock And Thread Synchronization In Java
- Real-World Use Cases Of Thread Synchronization In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Daemon Thread In Java?
- User Threads Vs. Daemon Threads In Java
- Methods For Daemon Threads In The Thread Class
- Creating Daemon Threads In Java
- Checking The Daemon Status Of A Thread
- Exceptions In Daemon Threads
- Limitations Of Daemon Threads In Java
- Practical Applications Of Daemon Threads In Java
- Common Mistakes To Avoid When Working With Daemon Threads In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Do Threads Need To Communicate?
- Understanding Inter Thread Communication In Java
- The wait() Method In Inter-Thread Communication
- The notify() Method In Inter-Thread Communication
- The notifyAll() Method In Inter-Thread Communication
- Difference Between wait() And sleep() Methods In Java
- Best Practices For Inter Thread Communication In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The Factorial Concept
- Approaches To Implementing Factorial In Java
- Find Factorial In Java Using Iterative Approach (Using a Loop)
- Find Factorial In Java Using Recursive Approach
- Complexity Analysis Of Factorial Programs In Java
- Applications Of Factorial Program In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The Leap Year Concept
- Approach To Check A Leap Year In Java
- Alternative Approach To Check A Leap Year In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Difference Between JDK, JRE, and JVM?
- What Is JVM (Java Virtual Machine)?
- What Is JRE (Java Runtime Environment)?
- What Is JDK (Java Development Kit)?
- Understanding The Difference Between JDK, JRE, And JVM
- Comparison Table For Difference Between JDK, JRE, And JVM
- Conclusion
- Frequently Asked Questions
Table of content:
- Difference Between Abstraction And Encapsulation In Java
- Understanding Abstraction In Java
- Understanding Encapsulation In Java
- When To Use Abstraction And Encapsulation?
- Conclusion
- Frequently Asked Questions
Table of content:
- Differences Between Abstract Class And Interface In Java
- What Is An Abstract Class In Java?
- What Is An Interface In Java?
- When To Use An Abstract Class?
- When To Use Interface?
- Compatibility Between Abstract Class And Interface In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Error Vs. Exception In Java
- What Is Error In Java?
- What Is Exception In Java?
- Best Practices For Handling Exceptions In Java
- Why Errors Should Not Be Handled In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Differences: Java Vs. JavaScript
- What Is Java?
- What Is JavaScript?
- Difference Between Java And JavaScript Explained
- Conclusion
- Frequently Asked Questions
Table of content:
- Brief Introduction To C++
- Brief Introduction To Java
- Difference Between C++ and Java
- Overview & Features Of C++ Language
- Overview & Features of Java Language
- Example of C++ and Java Program
- Key Difference Between C++ And Java Explained
- Similarities Between Java Vs. C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Basic Java interview questions and answers
- Intermediate Java interview questions and answers
- Advanced Java interview questions and answers
Table of content:
- Difference between core Java and advanced Java
- Important Core Java Questions
- Tips for Preparing for Core Java
15 Differences Between Overloading And Overriding In Java (+Examples)

Overloading and overriding are two essential object-oriented programming concepts that allow developers to enhance code flexibility and reusability. While overloading focuses on defining multiple methods with the same name but different parameters, overriding emphasizes redefining a method in a subclass to modify its behavior.
In this article, we will discuss the difference between overloading and overriding in Java. By the end, you’ll understand when and where to use these techniques, ensuring you write efficient, maintainable, and clean code.
- Method Overloading: Creating multiple methods in the same class with the same name/ identifier but different parameter lists (number, data type, or both).
- Method Overriding: Redefining a method in a child class that already exists in the parent class. In the definition of methods in subclass, you maintain the same signature but add a new functionality or a variation.
Difference Between Overloading And Overriding In Java (Comparison Table)
Method overloading and method overriding may sound similar, but they serve distinct purposes. They are used in different contexts and have distinctions that affect how they are implemented. The table below highlights the difference between method overloading and method overriding in Java language.
Basis |
Method Overloading |
Method Overriding |
Definition |
Multiple methods with the same name but different parameter lists within the same class. |
Redefining methods in subclass that already exists in the parent class. |
Class |
All overloaded methods must be defined in a single class. |
Happens across parent class and child class/ classes. |
Parameters List |
Must differ in number, type, or order. |
Must have the exact same parameter list (signature) as the original method. |
Return Type |
The return type can be different from the overloaded method. |
The return type of the overriding method must be the same as the overridden method or a subclass of it (covariant return type). |
Type of Polymorphism |
Compile-time polymorphism (dynamic binding) |
Runtime polymorphism (static binding) |
Inheritance |
Not influenced by inheritance relationships, as all methods belong to the same class. |
Inheritance is mandatory (parent-child relationship) as the sub class redefined method inherited from the super class. |
Access Modifier Rules |
No specific rules. |
Cannot reduce visibility (e.g., protected cannot become private). |
Annotations |
No annotations are required. |
@Override annotation is optional but highly recommended. |
Type of Arguments |
Works with different argument types or counts. |
Arguments must remain the same as the overridden method. |
Exception Handling |
Overloaded methods can throw different exceptions. |
Overridden methods can only throw the same or narrower exceptions than the parent method (checked exceptions). |
Binding/ Method Resolution |
Resolved at compile-time based on method signature (early binding). |
Resolved at runtime based on the object's actual type (late binding). |
Usage Context |
Used for method variations (e.g., initializing with different parameters). |
Used to provide specific behavior in the child class. |
Private Methods |
Can be overloaded. |
Cannot be overridden (private methods are not inherited). |
Static Methods |
Can be overloaded. |
Cannot be overridden but can be hidden by redefining in the child class. |
Final Methods |
Can be overloaded. |
Cannot be overridden (final methods cannot be modified). |
Now that we know about the differences between method overloading and overriding in Java let's discuss these concepts individually.
What Is Method Overloading In Java?
Method overloading allows a class to have multiple methods with the same name but different parameter lists. This is an example of compile-time polymorphism, where the method call is resolved during compilation based on the arguments passed. It improves code readability and reusability by enabling method customization for different inputs without requiring unique method names.
Rules Of Method Overloading In Java
- The method name must be the same.
- The parameter list must differ (by number, type, or order).
- The return type may or may not be the same.
- It is independent of access modifiers.
- Static methods can be overloaded.
Example Of Method Overloading In Java
In the example below, we will illustrate how to define three methods with the same name, which accept different types of variables/ arguments and then a different number of parameters.
Code Example:
class OverloadExample {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Overloaded method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Overloaded method to add two doubles
double add(double a, double b){
return a+b;
}
}
class Main{
public static void main(String[] args) {
OverloadExample obj = new OverloadExample();
System.out.println("Addition of 2 integers: " + obj.add(5, 10)); // Calls first method
System.out.println("Addition of 3 integers: " + obj.add(5, 10, 15)); // Calls second method
System.out.println("Addition of 2 doubles: " + obj.add(5.5, 10.5)); // Calls third method
}
}
Output:
Addition of 2 integers: 15
Addition of 3 integers: 30
Addition of 2 doubles: 16.0
Explanation:
In this example, the add() method is overloaded three times. Depending on the number and type of arguments passed during the method call, the appropriate method is executed. This demonstrates the flexibility of method overloading in handling various scenarios.
Advantages Of Overloading In Java
- Increases code readability and reusability by avoiding repetitive method names.
- Provides flexibility in defining methods to perform similar operations with different input types.
- Reduces the need for complex method names, simplifying the code structure.
For more, read: Method Overloading In Java | Ways, Rules & Type Promotion (+Codes)
What Is Method Overriding In Java?
Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class. This is an example of runtime polymorphism, where the method call is resolved at runtime. Overriding is essential for achieving dynamic behavior and adhering to the principles of inheritance and abstraction in OOP.
Rules Of Method Overriding In Java
- The method must have the same name, return type, and parameter list as the parent class method.
- The overriding method cannot have a more restrictive access modifier than the overridden method.
- Only inherited methods can be overridden. This also means that static, final, or private methods cannot be overridden.
- The overriding method can throw the same or narrower checked exceptions.
- Requires inheritance between parent and child classes.
Example Of Method Overriding In Java
In the simple Java example below, we first define a single method in the base class and then override it to redefine the functionality within the subclass.
Code Example:
class Parent {
void showMessage() {
System.out.println("This is the parent class message.");
}
}
class Child extends Parent {
@Override
void showMessage() {
System.out.println("This is the child class message.");
}
}
public class Main {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
obj1.showMessage(); // Calls parent class method
obj2.showMessage(); // Calls child class method
}
}
Output:
This is the parent class message.
This is the child class message.
Explanation:
In this example, the showMessage() method is overridden in the Child class. When a Child object is assigned to a Parent reference (obj2), the overridden method in the child class is executed. This demonstrates runtime polymorphism and how overriding allows behavior modification in a subclass.
Advantages Of Overriding In Java
- Supports runtime polymorphism, enabling dynamic method invocation.
- Allows behavior customization in child classes without modifying the parent class.
- Enhances code maintainability by centralizing method definitions in parent classes.
- Adheres to the DRY (Don't Repeat Yourself) principle by reusing the parent class method signature.
For more, read: Method Overriding In Java | Rules, Use-Cases & More (+Examples)
Want to expand your knowledge of Java programming? Check out this amazing Java course and become the Java developer of your dreams.
Key Differences Between Overloading & Overriding In Java Explained
Let's elaborate on some of the key differences between overloading and overriding in Java programming language.
1. Definition | Difference Between Overloading & Overriding In Java
Method Overloading: This occurs when you define multiple methods with the same name but different parameter lists within the same class. The compiler chooses the correct method to invoke based on the number or type of arguments passed.
class Calculator {
int add(int a, int b) { // Adds two integers double
return a + b;
}
add(double a, double b) { // Adds two doubles
return a + b;
}
}
Method Overriding: In method overriding, a subclass redefines a method that already exists in its parent class. The method signature (name, return type, and parameters) in the subclass must match the parent class exactly.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
} }
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks"); } // Overriding the parent class method
}
2. Class | Difference Between Overloading & Overriding In Java
Method Overloading: All overloaded methods must be defined in a single class. These methods are independent of inheritance and don’t rely on any parent-child relationship.
For example, methods add() in the Calculator class above are all within the same class.
Method Overriding: Happens across a parent class and its child class. The child class redefines a method inherited from the parent class, providing specific functionality for the subclass.
For example, the sound() method in the Dog class overrides the sound() method from the Animal class, in the snippet above.
3. Return Type | Difference Between Overloading & Overriding In Java
Method Overloading: The return type can differ between overloaded methods. However, the method signatures must still differ in their parameters.
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; } // Overloading with a different return type
}
Method Overriding: The return type must be the same as the parent method's return type or a subclass of it (covariant return type). This ensures that the overriding method maintains the contract of the parent class.
class Animal {
Animal sound() { return this; }
}
class Dog extends Animal {
@Override
Dog sound() { return this; } // Covariant return type (returns a Dog, which is a subclass of Animal)
}
4. Type Of Polymorphism | Difference Between Overloading & Overriding In Java
Method Overloading: This is an example of compile-time polymorphism (also known as static binding). The method that gets called is determined at compile time based on the method signature.
For example, the method to be called (add(int, int) or add(double, double)) is resolved during compilation.
Method Overriding: This is run-time polymorphism (also called dynamic binding). The method that gets called is determined at runtime based on the actual object type, not the reference type.
For example, if we create an object of Dog but refer to it through an Animal reference, the overridden sound() method in Dog is called at runtime, not the one in Animal.
5. Inheritance | Difference Between Overloading & Overriding In Java
Method Overloading: In method overloading, inheritance is not a factor. All methods are within the same class and do not require inheritance or parent-child relationships.
For example, overloaded methods, such as in the Calculator class, are defined within the same class.
Method Overriding: In overriding, inheritance is mandatory. The child class inherits the method from the parent class and overrides it to provide a specific implementation.
For example, the Dog class overrides the sound() method from the Animal class, requiring inheritance.
6. Access Modifier Rules | Difference Between Overloading & Overriding In Java
Method Overloading: There are no specific access modifier restrictions for overloaded methods. They can have different access levels (e.g., public, private).
For example, the overloaded add methods could have any valid access modifier.
Method Overriding: The overriding method cannot reduce the visibility of the parent method. For instance, if the parent method is protected, the child class cannot override it with private visibility.
For example, if the sound() method in Animal is protected, the overriding method in Dog must also be protected or public, but not private.
7. Exception Handling | Difference Between Overloading & Overriding In Java
Method Overloading: Overloaded methods can throw different types of exceptions. Since the methods are resolved at compile-time, the compiler doesn't require uniform exception handling across overloaded methods.
For example, overloading methods like add() can throw different exceptions (e.g., IllegalArgumentException for invalid input types).
Method Overriding: Overridden methods can only throw the same exceptions or narrower exceptions (checked exceptions). This ensures that the subclass does not introduce unexpected exceptions that the parent method doesn't handle.
For example, if the parent class method throws IOException, the overridden method in the child class can throw IOException or a subclass of it, but not a broader exception like Exception.
8. Binding / Method Resolution | Difference Between Overloading & Overriding In Java
Method Overloading: Resolved at compile-time (early binding/ binding at compile time). The compiler selects the method to call based on the method signature (parameter types and count).
Method Overriding: Resolved at runtime (late binding/ binding at runtime). The method to call is determined by the actual object type, not the reference type, and is executed when the program runs.
Here is your chance to top the leaderboard while practising your coding skills: Participate in the 100-Day Coding Sprint at Unstop.
Difference Between Overloading And Overriding In Java Code Example
In the following example, we first illustrate method overloading and then overriding. We will then compare the definition and impact of the two concepts in code execution.
Code Example:
// Method Overloading
class OverloadingDemo {
void display(int a) {
System.out.println("Integer: " + a); }
void display(double b) {
System.out.println("Double: " + b); }
}
// Method Overriding
class Parent {
void show() {
System.out.println("Parent's show method"); }
}
class Child extends Parent {
@Override
void show() {
System.out.println("Child's show method"); }
}
class Main {
public static void main(String[] args) {
// Method Overloading
OverloadingDemo obj = new OverloadingDemo();
obj.display(10); // Calls the method with int parameter
obj.display(10.5); // Calls the method with double parameter
// Method Overriding
Parent parentObj = new Parent();
parentObj.show(); // Calls the method in Parent class
Child childObj = new Child();
childObj.show(); // Calls the overridden method in Child class
}
}
Output:
Integer: 10
Double: 10.5
Parent's show method
Child's show method
Explanation:
In the Java code example,
- We first demonstrate the concept of method overloading by defining a class called OverloadingDemo, with two methods named display(), where one accepts an int parameter and the other a double.
- These are distinct methods because their parameter types differ. The compiler determines which method to call at compile time based on the argument passed.
- Next, we illustrate the concept of method overriding using two classes with an IS-A (inheritance) relationship.
- Here, we first define a class Parent containing a method show() that simply prints "Parent's show method."
- Then, we define another class Child that inherits from Parent, and overrides the original method with its own implementation. The show() method here prints "Child's show method."
- In the Main class, we demonstrate the implementation of method overriding and overloading defined above.
- We first create an object of the OverloadingDemo class named obj and then use it to invoke the overloaded methods with different type of argument in call.
- When we call obj.display(10), the display(int a) method is invoked.
- When we call obj.display(10.5), the display(double b) method is invoked.
- Here, the method is resolved at compile time based on the method signature (i.e., the number and type of parameters). This is why the parameter types (int vs. double) differentiate the methods.
- Then, we create two more instances, one of Parent class and one of Child class, and use them to call the show() method.
- When parentObj.show() is called, the show() superclass method is executed.
- When childObj.show() is called, the overridden show() subclass method is executed despite being called from a Parent reference.
- Here, the method is resolved at runtime based on the actual object type, which allows for dynamic method dispatch.
- The JVM checks the actual object type (Parent or Child) at runtime, and the appropriate method is invoked accordingly. This is a key feature of runtime polymorphism. In this case, the Child's method takes precedence, as it's the method associated with the actual object (childObj).
Need more guidance on how to become a Java developer? You can now select an expert to be your mentor here.
Conclusion
Method overloading and method overriding are two fundamental concepts in Java that both offer distinct benefits in object-oriented programming, enabling flexibility and efficiency in code.
Method Overloading allows you to define multiple methods with the same name but different parameter lists within a single class.
- This gives you the flexibility to handle various argument types or counts, with method resolution taking place at compile-time.
- This mechanism is great for scenarios where you need to perform similar actions with different inputs, such as initializing an object with different data types or providing multiple ways to process information.
Method Overriding, on the other hand, empowers subclasses to redefine methods from their parent class, allowing for runtime polymorphism.
- This is particularly useful when you want to provide specific behavior in a subclass while maintaining a consistent interface.
- The method resolution happens at runtime, based on the actual object type, making this mechanism key to implementing dynamic method dispatch and achieving flexibility in behavior based on object instances.
Key differences between overloading and overriding in Java also include their relationship with inheritance and exception handling.
- Overloading does not require inheritance as it can be done within the same class, while overriding mandates inheritance between a parent and subclass.
- Additionally, when it comes to exception handling, overloading allows different exceptions to be thrown, whereas overriding restricts you to throwing exceptions that are narrower than those declared in the parent method.
By understanding the nuances of these mechanisms, you can harness the power of polymorphism to write cleaner, more efficient, and more maintainable code.
Frequently Asked Questions
Q1. Can method overloading occur in different classes?
No, method overloading occurs within the same class. It involves multiple methods with the same name but different parameter lists. If similar methods exist across different classes, it is not considered overloading.
Q2. Why is method overriding possible only in a parent-child relationship?
Method overriding requires inheritance because the concept revolves around redefining a method from the parent class in the child class. Without inheritance, there is no "parent method" to override.
Q3. Is the return type considered for method overloading?
No, method overloading is determined solely by the method's name and parameter list. Changing only the return type while keeping the method name and parameters the same will result in a compilation error.
Q4. Can static methods be overridden in Java?
No, static methods cannot be overridden because they are bound to the class rather than the object. They can, however, be hidden by defining a method with the same name in the subclass. Note that this is not true overriding.
Q5. Can method overloading involve inheritance?
Yes, method overloading can involve inheritance. If a child class inherits methods from its parent and also defines overloaded methods, the overloaded versions in the child class will coexist with the inherited methods.
Q6. Why does method overriding require the same method signature?
For method overriding to work, the overridden and overriding methods must have the exact same signature (name, parameters, and return type). This ensures the method call can be dynamically resolved at runtime based on the actual object's type.
Q7. How does binding differ in overloading and overriding?
Overloading: Uses static binding (also called compile-time binding), where the method to be called is resolved at compile time based on the method signature.
Overriding: Uses dynamic binding (also called runtime binding), where the method call is resolved at runtime based on the object's actual type.
This compiles our discussion on the difference between overloading and overriding in Java. You must also explore the following Java topics:
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.

Subscribe
to our newsletter
Esuru Pooja.C 4 hours ago
Ankita Das 2 days ago