Basic computer course, C programming language, Education, Engineering Course, Engineering Courses, Maths Coaching, online computer coaching, PGDCA course coaching, programming course, school computer coaching, training, Uncategorized, Web Design course

What is Difference Between Polymorphism and Duck Typing? tccicomputercoaching.com

Polymorphism is the ability of object of different types to be handled by a common interface. While duck typing, is a kind of dynamic typing that allows objects of different types to respond to the same methods.

duck vs polymorphism

Definition:

Polymorphism means a subclass can override a method of the base class. This means a method of a class can do different things in subclasses.

For example: a class Animal can have a method talk() and the subclasses Dog and Cat of Animal can let the method talk() make different sounds.

Duck typing means code will simply accept any object that has a particular method.

For Example: animal.quack(). If the given object animal has the method we want to call then we’re good (no additional type requirements needed). It does not matter whether animal is actually a Duck or a different animal which also happens to quack. That’s why it is called duck typing: if it looks like a duck (e.g., it has a method called quack() then we can act as if that object is a duck).

Now, we can conclude is both are related?

They are simply separate features that a programming language may have. There are programming languages which have polymorphism but that do not have duck typing (such as Java). There are also languages that have polymorphism and duck typing (such as Python).

To learn more about Programming Languages, Computer Courses, Python Course, C++Java

Call us @ 9825618292

Visit us @ www.tccicomputercoaching.com

 

Leave a comment