Skip to main content

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

Page 1

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


tccicomputercoaching.com


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.


tccicomputercoaching.com 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

method talk() make different sounds.

let

the


tccicomputercoaching.com ď‚§ 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).


tccicomputercoaching.com ď‚§ 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).


tccicomputercoaching.com To learn more about Programming Languages, Computer Courses, Python Course, C++, Java Call us @ 9825618292 Visit us @ www.tccicomputercoaching.com


tccicomputercoaching.com

Thank you for watching


Turn static files into dynamic content formats.

Create a flipbook
What is Difference Between Polymorphism and Duck Typing? tccicomputercoaching.com by Rosario Riley - Issuu