Lakhyadeep Sen’s Post

Another form of polymorphism in Java appears through method overriding. This happens when a child class provides its own implementation of a method that already exists in the parent class. Things that became clear : • method overriding occurs during inheritance • the child class keeps the same method name and parameters as the parent class • the implementation of the method is changed in the child class • the method that runs is decided at runtime • this is known as runtime polymorphism A simple example shows how this works : class Parent { void show() { System.out.println("Parent method"); } } class Child extends Parent { void show() { System.out.println("Child method"); } } When an object of the child class is used, the child’s version of the method is executed. This mechanism allows subclasses to adjust behaviour while still keeping the structure defined by the parent class. #java #oop #programming #learning #dsajourney

To view or add a comment, sign in

Explore content categories