Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Student Reviews


For Business / Universities

Corporate Training

Hire from US

Academic Up-skilling



All Courses

Choose a category

Loading...

All Courses

All Courses

logo

Mechanical

Uploaded on

24 Jan 2023

All About the OOP Technique, Polymorphism

logo

Skill-Lync

Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented Programming (OOP) is a methodology to design a program with classes and objects. OOP techniques simplify software development and its maintenance by providing the following concepts:

In the subsequent discussion, let us discuss the concept of Polymorphism.

Polymorphism

It is called polymorphism if the same task can be performed in different ways. For example, a shopkeeper can convince the customer differently; different animals speak differently, like a cat meows, a dog barks woof, etc. The word polymorphism also signifies different forms. Polymorphism is a very important concept in programming. It refers to using a single type entity (method, function,  operator, or object) to represent different types in different scenarios.

Some examples of polymorphism in Python are described below:

Polymorphism, in addition to operator

In python ‘+’ operator doesn’t just have simple usage. For integer data types, it is used to add numbers, as shown below. 

The output of the above program is 30

For string data types, polymorphism is used for the concatenation of strings, as shown below:

The output of the above program is “Skill-Lync”.

As can be seen from the above two examples, the same “+” operator can be used differently for two different data types. This is the most simple occurrence of polymorphism in python.

Polymorphism with len() function

In the below example, len() function is applied to different data types. For different data types, the output is different.

The output of above program is as follows.

Polymorphism with class methods: 

The below example shows how python used two different types of classes in the same way. We create a for loop that iterates through a tuple of objects. Then call the methods without being concerned about which class type each object is. These methods exist in each class. 

Program: 

Output:

Polymorphism with Inheritance (Method Overriding):

In inheritance, properties of the parent class are inherited in a child class. For example, three classes (Bird, sparrow, and ostrich) are created in the python program shown below. Bird is the parent class, and sparrow and ostrich are the child class. Child classes inherit all the properties of the parent class. Methods/functions written in a parent class also become a part of child classes, or methods/functions written in a parent class also belong to the parent class. But the methods written in the parent class can be modified in the child class if it does not fit the purpose. This process of re-implementing a method in the child class is known as method overriding. 

Polymorphism can also be achieved by using method overloading. It is a method with the same name but different input arguments. Method overloading is not possible in Python.

Polymorphism can be an incredibly useful technique for code management. Code for any use case in the real world can be worked upon by multiple developers at once. It’s important to be clear about the structure of your code and make sure the hierarchy of functions used is clear.


Author

author

Navin Baskar


Author

blogdetails

Skill-Lync

img

Continue Reading

Related Blogs

Moving Frame of Reference

A Moving Reference Frame (MRF) is a very straightforward, reliable, and effective steady-state Computational Fluid Dynamics (CFD) modeling tool to simulate rotating machinery. A quadcopter's rotors, for instance, can be modeled using MRFs.

Mechanical

12 May 2023


Analysis Settings in Ansys Software

Analysis settings in Ansys are the parameters which determine how the simulation should run.

Mechanical

08 May 2023


Comparing the Explicit and Implicit Methods in FEA

In Ansys, the analysis settings play a very important role in converging the solution and obtaining the results. These involve settings about the timestep size, solver type, energy stabilization etc.

Mechanical

06 May 2023


Tensors, Stress, and 2D Meshing: A Primer for Beginners

A tensor is a mathematical object that describes a geometric relationship between vectors, scalars, and other tensors. They describe physical quantities with both magnitude and direction, such as velocity, force, and stress.

Mechanical

05 May 2023


Reynold's law of Similarity

The Reynolds number represents the ratio of inertial to viscous forces and is a convenient parameter for predicting whether a flow condition will be laminar or turbulent. It is defined as the product of the characteristic length and the characteristic velocity divided by the kinematic viscosity.

Mechanical

04 May 2023



Author

blogdetails

Skill-Lync

img

Continue Reading

Related Blogs

Moving Frame of Reference

A Moving Reference Frame (MRF) is a very straightforward, reliable, and effective steady-state Computational Fluid Dynamics (CFD) modeling tool to simulate rotating machinery. A quadcopter's rotors, for instance, can be modeled using MRFs.

Mechanical

12 May 2023


Analysis Settings in Ansys Software

Analysis settings in Ansys are the parameters which determine how the simulation should run.

Mechanical

08 May 2023


Comparing the Explicit and Implicit Methods in FEA

In Ansys, the analysis settings play a very important role in converging the solution and obtaining the results. These involve settings about the timestep size, solver type, energy stabilization etc.

Mechanical

06 May 2023


Tensors, Stress, and 2D Meshing: A Primer for Beginners

A tensor is a mathematical object that describes a geometric relationship between vectors, scalars, and other tensors. They describe physical quantities with both magnitude and direction, such as velocity, force, and stress.

Mechanical

05 May 2023


Reynold's law of Similarity

The Reynolds number represents the ratio of inertial to viscous forces and is a convenient parameter for predicting whether a flow condition will be laminar or turbulent. It is defined as the product of the characteristic length and the characteristic velocity divided by the kinematic viscosity.

Mechanical

04 May 2023


Book a Free Demo, now!

Related Courses

https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/masters-cfd_1636550988.jpg
Post Graduate Program in Computational Fluid Dynamics
4.8
125 Hours of content
Cfd Domain
Know more
https://d28ljev2bhqcfz.cloudfront.net/mainproject/thumb/advanced-fan-modelling-and-analysis-using-moving-reference-frame-approach-in-ansys-icepak_1616587608.jpgRecently launched
0 Hours of content
Electrical Domain
https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/combustion-cfd-specialisation_1636553169.png
4.8
35 Hours of content
Cfd Domain
Showing 1 of 5 courses
Try our top engineering courses, projects & workshops today!Book a Live Demo