I was reading an online excerpt from a C++ book on polymorphism and interfaces. The book made a distinction between polymorphism and interfaces, and specified how to implement them in C++. However, I was always under the idea that interfaces in C++ (implemented using a base class with pure virtual functions) were nothing more than an application of polymorphism. I would like to know the clear distinction between polymorphism and interfaces because the excerpt confused me.
Polymorphism is one of the core concepts of object-oriented programming (OOP) and describes situations in which something occurs in several different forms. In computer science, it describes the concept that you can access objects of different types through the same interface.
Polymorphism in Java has two types: Runtime polymorphism (dynamic binding) and Compile time polymorphism (static binding).
Each interface is considered as a type. An object of a class can be casted to the type of each interface it implements. This is how polymorphism via interfaces work.
This is an example of polymorphism, which is method overloading. Types of polymorphism in Java: Run time polymorphism. Compile-time polymorphism.
Polymorphism is the abstract concept of dealing with multiple types in a uniform manner, and interfaces are a way to implement that concept. Code that interacts with an interface can interact with any type that provides that interface.
Note that C++ has (at least) two forms of polymorphism: dynamic (i.e. run-time) polymorphism via interfaces formally defined by virtual functions, and static (i.e. compile-time) polymorphism via interfaces informally defined by the use of template parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With