Sitemap
Samoonalina

Samoonalina

POLYMORPHISM( OOP concept)

1 min readFeb 3, 2024

The word Poly means many and morphism means forms, collectively it is the way of doing work with different methods.
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows for flexibility and code reusability.

there are two types of polymorphisms ;

1- FUNCTION OVERLOADING: It is a compile-time morphism, in this method we create different functions with the same name which means they will perform different tasks but will have having same name.

some rules for differentiating factors for function overloading are:

1-they should have had a different return type.

2- if the return type is the same, parameter names should be different, that means if one has a string in it so other should have a different data type.

3- if the parameter name is also the same then several parameters should be different.

class Calculator {

public int add(int a, int b) {

Get Samoonalina’s stories in your inbox

Join Medium for free to get updates from this writer.

return a + b;

}

public double add(double a, double b) {

return a + b;

}

public String add(String a, String b) {

return a + b;

}

}

2- FUNCTION OVERRIDING: It is a runtime morphism. Happens when a subclass redefines a method inherited from its parent class. Subclasses can redefine inherited methods from parent classes to provide their implementations specific to their needs. This allows for flexible customization without code duplication.

  • It’s like inheriting a house but renovating it to suit your preferences while keeping the basic structure (walls, roof).

--

--