As part of my automation learning journey, today I explored one of the core Object-Oriented Programming (OOP) concepts in Java β Polymorphism.
So far, we started with the first type of polymorphism, which is Method Overloading.
This concept allows a class to have multiple methods with the same name but different parameters.
π What is Polymorphism?
Polymorphism means βmany forms.β
In Java, polymorphism allows the same method name to behave differently based on the parameters passed.
Two types of polymorphism in Java
1οΈβ£ Compile-Time Polymorphism (Method Overloading)
2οΈβ£ Run-Time Polymorphism (Method Overriding)
Today we focused on Method Overloading.
π What is Method Overloading?
Method overloading means:
Same method name
Different number of arguments
Different types of arguments
Different order of parameters
Example idea:
debitamount(int amount, int pin)
debitamount(long amount, int pin, String name, double balance)
Even though the method name is the same, the parameters are different, so Java treats them as different methods.
π» Example Program β ATM Transaction
package polymorphism;
public class ATM {
public static void main(String[] args) {
ATM sbi = new ATM();
sbi.debitamount(1000, 123);
ATM icici = new ATM();
icici.debitamount(3000, 6364, "Rajiv", 10.23);
}
void debitamount(int amount, int pin) {
System.out.println("SBI Bank successfully debited " + amount);
}
void debitamount(long amount, int pin, String name, double balance) {
System.out.println("ICICI Bank successfully debited " + amount + " for " + name);
}
}
π Code Explanation
Step 1 β Program Execution Starts
Java starts execution from the main() method.
public static void main(String[] args)
Step 2 β Object Creation
ATM sbi = new ATM();
Here:
- ATM β Class name
- sbi β Reference variable
new ATM() β Creates an object in heap memory
Step 3 β Calling First Method
sbi.debitamount(1000, 123);
Java searches for a method with:
(int , int)
So it executes:
void debitamount(int amount, int pin)
Output:
SBI Bank successfully debited 1000
Step 4 β Second Object Creation
ATM icici = new ATM();
A second object is created.
Step 5 β Calling Overloaded Method
icici.debitamount(3000, 6364, "Rajiv", 10.23);
Java now searches for a method with parameters:
(long, int, String, double)
So it executes:
void debitamount(long amount, int pin, String name, double balance)
Output:
ICICI Bank successfully debited 3000 for Rajiv
π§ How Java Decides Which Method to Call
Java compiler checks:
1οΈβ£ Method name
2οΈβ£ Number of parameters
3οΈβ£ Data types of parameters
This process is called:
Compile-Time Method Binding
Because the decision happens during compilation.
π Important Findings While Practicing
While experimenting with data types, I noticed some interesting behaviours in Java.
1οΈβ£ Integer Default Type
When we write a number like this:
100
Java treats it as int by default.
2οΈβ£ Byte / Short Not Accepted Automatically
Example:
byte balance = 100;
If we directly pass values sometimes Java treats them as int, so we may need type casting.
Example:
(byte)100
3οΈβ£ Long Values
For long values we usually add L.
Example:
long amount = 3000L;
But sometimes Java automatically converts int β long if needed.
4οΈβ£ Decimal Numbers
Decimal numbers behave differently.
10.23
By default Java treats this as double.
If we want float, we must use f.
Example:
float balance = 10.23f;
But double works without adding d because it is the default.
π Summary of Numeric Defaults in Java
Value Default Type
100 int
100L long
10.5 double
10.5f float
In Java, integer values are treated as int by default. When assigning values to smaller data types like byte or short, the compiler may require explicit type casting because the literal is considered an int.
Java allows automatic conversion from smaller types to larger types (like int to long). However, decimal numbers are treated as double by default. If we want to store decimal values in a float, we must append f at the end of the number.
π Key Takeaways from Day 12
Today I learned:
β
What Polymorphism means
β
Difference between method overloading and overriding
β
How method overloading works
β
How Java selects methods during compile time
β
Default behaviour of numeric data types
Understanding polymorphism is important because it helps us write flexible and reusable code.
π¨βπ« Trainer: Nantha from Payilagam
π€ A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)