Recently I defined Object-oriented programming (OOP) as a program design pattern where employing abstraction and inheritance we can solve polymorphic problems including but not limited to decoupling. Polymorphism refers to the use of an abstraction to represent multiple different concrete types. Do you know a better and shorter definition?
Loading

Sarthak VarshneyPosted May 25, 2024, 10:15 AM
Let's address your points systematically to clarify the distinctions and how polymorphism fits into different stages of programming.
1. Interface-based vs. Inheritance-based Polymorphism
Inheritance-based Polymorphism:
Example:
Interface-based Polymorphism:
Example:
Difference:
2. Operator Overloading vs. Method Overloading
Method Overloading:
Example:
Operator Overloading:
Example:
Difference:
3. Delegate Types and Variables
Delegates:
Example:
Polymorphism in Different Stages
a. Algorithm Stage:
b. Program Design Stage:
IShapewith a methodDrawthat different shape classes implement.c. Program Run Time:
IShapeobjects and callingDrawon each, regardless of the actual shape type.Conclusion
Mariusz PostolPosted May 25, 2024, 8:41 AM
@Sarthak Varshney - good point. Thanks.
1. Not sure what is the difference between interface-based and inheritance-based polymorphism- interface can be recognized as an abstract class
2. The same question is for operator-overloading and method overloading
3. I agree that method overloading can be recognized as a polymorphism form, but what about delegate types and variables?
In conclusion, to answer my doubts we need to consider three stages of the program, namely (a) the algorithm, (b) program design time, and (c) program run time. (a) Talking about algorithms ( knowledge required to solve a problem) polymorphism is a problem that we must address by a program. (b) Talking about the design stage the programming language should support constructs that can be applied to implement the polymorphism. In other words, at design time it is part of the solution. (c) At run-time polymorphism may be only observed to improve the program in the future and get back to the design stage.
Sarthak VarshneyPosted May 25, 2024, 12:51 AM
Yes, besides inheritance, there are other ways to achieve polymorphism in object-oriented programming. Here are the primary ways:
1. Inheritance-based Polymorphism
This is the most common form of polymorphism where a subclass inherits from a superclass and overrides its methods. Objects of the subclass can be treated as objects of the superclass.
Example:
2. Interface-based Polymorphism
This involves defining interfaces that different classes implement. This allows for different classes to be treated polymorphically through the interface they implement.
Example:
3. Method Overloading
Method overloading allows multiple methods in the same class to have the same name but different parameters. This is also considered a form of compile-time polymorphism.
Example:
4. Operator Overloading
In languages that support operator overloading, you can define or change the behaviour of operators for user-defined types.
Example in C++:
Summary
There are various ways to achieve polymorphism in object-oriented programming, each providing different mechanisms for treating objects in a polymorphic manner.
Mariusz PostolPosted May 23, 2024, 8:18 AM
@Sarthak Varshney, - many thanks for your point - it looks like a common one. Last question. You said "Inheritance is one way to achieve polymorphism" - do you know another one?
Sarthak VarshneyPosted May 23, 2024, 2:48 AM
You're absolutely correct. Inheritance and polymorphism are related concepts in object-oriented programming, but they are not interchangeable. Let's clarify the relationship between them and how they contribute to solving problems in OOP.
Inheritance:Inheritance is a mechanism in object-oriented programming where a class (subclass or derived class) inherits properties and behaviors (methods) from another class (superclass or base class). It promotes code reusability by allowing a new class to inherit the characteristics of an existing class.
Polymorphism:Polymorphism is the ability of different objects to respond to the same message (method call) in different ways. It allows objects of different types to be treated as objects of a common superclass. Polymorphism enables flexibility and extensibility in the code by providing a single interface to entities of different types.
Relationship:- Inheritance facilitates Polymorphism: Inheritance is one way to achieve polymorphism. By inheriting from a common superclass, subclasses can share common behavior and be treated polymorphically.
- Polymorphism can exist without Inheritance: Polymorphism can also be achieved through other mechanisms like interface implementation, method overriding, and method overloading, without relying on class inheritance.
Conclusion:In summary, while inheritance is one mechanism to enable polymorphism, polymorphism itself is a broader concept that encompasses various ways objects can behave differently in response to the same message. They are complementary concepts in OOP, with inheritance facilitating polymorphism but not replacing its concise definition.
Mariusz PostolPosted May 22, 2024, 7:17 PM
@Sarthak Varshney, I agree, but my concern is if Inheritance can replace the CONCISE DEFINITION of polymorphism.
I point out that something cannot simultaneously be a problem and a solution.
Sarthak VarshneyPosted May 22, 2024, 12:27 PM
Polymorphism in OOP:
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different types to be treated as objects of a common supertype. It enables a single interface to represent different underlying forms (data types).
Concise Definition:
Polymorphism is an OOP principle that allows objects of different types to be accessed through the same interface, enabling one interface to control access to a general class of actions. This supports code flexibility and reuse.
This definition captures both the essence of polymorphism and its role in solving specific problems in OOP.
Mariusz PostolPosted May 21, 2024, 7:25 PM
@Sarthak Varshney - I am sorry but I didn't ask what "Polymorphism allows" but what it is. It is two different questions.
To be precise:
1. It is a problem to be solved using OOP or alternatively
2. It is part of the solution
Sarthak VarshneyPosted May 21, 2024, 3:37 AM
Object-oriented programming (OOP) is a programming paradigm based on the concepts of objects, which can contain data and methods. It leverages key principles like encapsulation, inheritance, and polymorphism to create modular, reusable, and maintainable code. Polymorphism allows objects of different types to be treated as instances of a common superclass.