- Method Overloading
- Method Overriding
Method Overloading
- Method overloading is when more than one method with the same name but a different number or type of arguments defined in the same class.
- The return type of the method can be different.
public class CalculatorUtil { public static int sum(int i, int i2) {
return i + i2;
} public static int sum(int i, int i2, int i3) {
return i + i2 + i3;
}
public static double sum(double i, double i2) {
return i + i2;
}
public static double sum(double i, double i2, double i3) {
return i + i2 + i3;
}
}
In the above example, we have four overloaded methods. It’s convenient because when the client code will use our sum
they won't think should we pass int or double.
One of the most popular method
System.out.println();
has 10 overloaded methods
Did you know about that?
It is super useful for us. When we use it we just know it will print output in the console and we can pass everything we wanted to print.
Method Overriding
Method overriding when a child or concrete class overrides its parent method. We can override our parent class methods when we extend them and we can override methods of interfaces when we implement them(we implement abstract methods by overriding them).
Rules of overriding:
- Method name and number, order, and type of arguments should be exactly the same as the parent’s method.
- Return type should be the same or covariant with the parent method.
- The access modifier should be the same or more visible than the parent method.
- If an exception declaration exists in the parent method, the child method can have the same type of exception declaration or a smaller type.
That’s all I have for today. Have a nice day!
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET