Polymorphism in C# or generally in programming.

647 views

Why do we use it, what’s the purpose of it?

In: Technology

4 Answers

Anonymous 0 Comments

A professor of mine once tried to explain polymorphism with this example:

Imagine you are programming an image editor (think: Photoshop, Gimp, Paint, etc.). Now that editor offers various tools, probably 20-30 of them. Two very different examples might be a pencil and an eraser. Those tools are shown in a tool window side by side. Each tool can be „used“ and does something. If you try to put that to code you might have a class Tool with a method „use()“. Imagine the default tool implements use() in a way that it moves objects in your image. Now every other tool is a sub class of Tool. Pencil and Eraser are both classes that implement their own version of „use()“ with their expected functionality (pencil draws lines, eraser erases content).
Back to your program. As a user you select a Tool you want to work with. The code handling the active tool can just use a reference to a „Tool“ object since every tool can be „use()“d and as every sub-class of Tool can implement its own logic just calling „activeTool.use()“ will result in different actions depending on your chosen tool without the calling code needing to know which tool you are really using. And there might also be pencil sub-types even, with the pencil drawing lines with various strengths or colours. Polymorphism is the idea that the correct implementation of „use()“ will be called in your class hierarchy depending on what actual type is used at runtime and whether that type implements its own version of use() or not.

I‘m pretty sure someone will tell me this example could be explained better and I‘m happily accepting improvements. I‘m on mobile and tried my best. Hope it helps a bit though, OP.

Anonymous 0 Comments

As someone who is many years in programming, i honestly advice OP to check any tutorial (youtube is also good) for this answer.There are plenty of them. It needs some background explanations, and pictures, and it is just extremely hard to do it in this post.

Anonymous 0 Comments

[deleted]

Anonymous 0 Comments

Different programming languages support different programming methodologies. One way to program software is to use Object Oriented Programming (OOP). In OOP, every variable used in the program is considered an object (Instance) of some type (Class). A Class is like the blueprint that defines what members – variables and methods – a particular object type has.

One of the major features of OOP is inheritance. This is the ability to make a class that inherits attributes from another class, which we describe as parent and child classes. You might do this if you want to add specific features to the class that are only valid for certain cases. Polymorphism is the ability of your code to treat the child classes as if they were instances of the parent class.

Example: the class Animal represents animals in a zoo. That class will contain all the common attributes animals have – variables like the *number of limbs*, the *name of the animal*, or the *enclosure they reside in*; and methods for common action like *move*, *sleep*, or *make noise*.

You can then create a class Elephant to extend the Animal class, with a variable for *trunk length*. An object of the Elephant type will have all of the members it inherited from Animal, as well the trunk length unique to this class. Your code can call the method to make the animal *sleep* without specifically knowing what type of animal it is, because all the child classes of Animal can also be treated as if they are of type Animal.