DEV Community

Cover image for πŸš€ Day 12 of My Automation Journey – Understanding Polymorphism in Java
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 12 of My Automation Journey – Understanding Polymorphism in Java

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)
Enter fullscreen mode Exit fullscreen mode

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);
    }

}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Code Explanation
Step 1 – Program Execution Starts

Java starts execution from the main() method.

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

Step 2 – Object Creation

ATM sbi = new ATM();
Enter fullscreen mode Exit fullscreen mode

Here:

  • ATM β†’ Class name
  • sbi β†’ Reference variable

new ATM() β†’ Creates an object in heap memory

Step 3 – Calling First Method

sbi.debitamount(1000, 123);
Enter fullscreen mode Exit fullscreen mode

Java searches for a method with:

(int , int)
Enter fullscreen mode Exit fullscreen mode

So it executes:

void debitamount(int amount, int pin)
Enter fullscreen mode Exit fullscreen mode

Output:

SBI Bank successfully debited 1000
Step 4 – Second Object Creation
ATM icici = new ATM();
Enter fullscreen mode Exit fullscreen mode

A second object is created.

Step 5 – Calling Overloaded Method

icici.debitamount(3000, 6364, "Rajiv", 10.23);
Enter fullscreen mode Exit fullscreen mode

Java now searches for a method with parameters:

(long, int, String, double)
Enter fullscreen mode Exit fullscreen mode

So it executes:

void debitamount(long amount, int pin, String name, double balance)

Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

If we directly pass values sometimes Java treats them as int, so we may need type casting.

Example:

(byte)100
Enter fullscreen mode Exit fullscreen mode

3️⃣ Long Values

For long values we usually add L.

Example:

long amount = 3000L;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)