Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between interface and polymorphism [closed]

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.

like image 503
iart Avatar asked Jan 28 '14 07:01

iart


People also ask

What is polymorphism interface?

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.

What are the two kinds of interface polymorphism?

Polymorphism in Java has two types: Runtime polymorphism (dynamic binding) and Compile time polymorphism (static binding).

How polymorphism is achieved by interface?

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.

Is interface an example of runtime polymorphism?

This is an example of polymorphism, which is method overloading. Types of polymorphism in Java: Run time polymorphism. Compile-time polymorphism.


1 Answers

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.

like image 118
Mike Seymour Avatar answered Nov 15 '22 07:11

Mike Seymour