DEV Community

Cover image for πŸš€ Day 13 of My Automation Journey: Polymorphism – Method Overriding in Java
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 13 of My Automation Journey: Polymorphism – Method Overriding in Java

Today I learned about Method Overriding, which is part of Runtime Polymorphism in Java.

Method Overriding happens when a child class provides its own implementation of a method that is already defined in the parent class.

βœ” Same method name
βœ” Same parameters
βœ” Different implementation in child class

πŸ’» Example

Parent Class – RbiBank
package overriding;

public class RbiBank {

    void interestRate() {
        System.out.println("5% interest for fixed deposit upto 1 Lakh");
    }

}
Child Class – SbiBank
Enter fullscreen mode Exit fullscreen mode
package overriding;

public class SbiBank extends RbiBank {

    @Override
    void interestRate() {
        System.out.println("10% interest for fixed deposit upto 1 Lakh");
    }

    public static void main(String[] args) {

        SbiBank sbi = new SbiBank();
        sbi.interestRate();

    }

}
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Output

10% interest for fixed deposit upto 1 Lakh
Enter fullscreen mode Exit fullscreen mode

Here the SbiBank class overrides the method of RbiBank, so the child class implementation executes.

πŸ”— Relationship

RbiBank (Parent Class)
        ↑
     SbiBank (Child Class)
Enter fullscreen mode Exit fullscreen mode

This demonstrates Inheritance + Method Overriding.

🎯 Key Points Learned

βœ” Child class overrides parent method
βœ” Used for Runtime Polymorphism
βœ” Method name and parameters must be same
βœ” @override annotation improves readability
βœ” Helps build flexible and reusable code

πŸ“š What I’ll Learn Tomorrow
πŸ” Encapsulation

Topic:

Data Protection
Private variables
Getter & Setter methods

πŸ‘¨β€πŸ« 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)