txt / plm19

web site for csc417 programming languages (ncsu, spring, 2019)
http://tiny.cc/plm19
Other
3 stars 6 forks source link

add abstractions #1

Open timm opened 5 years ago

timm commented 5 years ago

inheritance

polymorphism

timm commented 5 years ago

Polymorphism is everywhere in daily life. Object oriented programming concepts are developed from real life hence we can find several examples of every concept of OOP in real life.

Lets come to the exact definition of polymorphism.

Polymorphism is occurrence an entity that has single name and many forms which acts differently in different situations or circumstances. (the later part is very imp in the definition). It reduces our complexities.

Note : Polymorphism can only be achieved through the behavior (menthods/functions)

The best real life example is of an Air Conditioner (AC). Is is a single device that performs different actions according to different circumstances. During summer it cools the air and during winters it heats the air.

Another best example is Humans. We have different behaviors in different situations and in front of different people. A man behaves differently with his father, when he is with his friend he has different behavior, when he is with his wife he has different behavior and so on.

So what is the need of polymorphism ?

Suppose you are the Principal of a school and you are going to conduct a meeting of the teachers in our school. So instead of calling the teachers of different section like teachers of science, teachers of mathematics and teachers of commerce you simply called them as "all the teachers should attend the meeting". This reduced the complexity of calling each and every teachers separately.

Now from programming point of view, if you have ever used Java just for basics them you must know about the method "println()", single name many form. It is an example of polymorphism.

println (10); // int Literal println ("Hello Java"); // String println (23.4); // Double

Hence you can see that we have to remember only one function name (reducing our complexity) to print all the different values instead of having different functions to print the values of different data types.

timm commented 5 years ago

You use a polymorphic function when you want to refer to a group of objects by their common base class, even if each object can have different behavior. This is useful when the objects are created at runtime and are not known when you are writing your code. Regardless of which type of mammal it is, you can access its methods with a common, well defined interface. This is typically called dynamic polymorphism since the types can be dynamically (at runtime) determined.

An alternative method would be to use template functions which would produce a different version of a function for each of the mammal sub-classes when the template was instantiated. Using a generic method like this provides static polymorphism, since the types are determined at compile time (i.e. when each template is instantiated, the types are fully known).

The two cases provide different benefits (generic access to objects), and both have some detriments (runtime cost for dynamic, compilation time and code size for static).

timm commented 5 years ago

https://medium.com/@shanikae/polymorphism-explained-simply-7294c8deeef7

timm commented 5 years ago

https://www.thoughtworks.com/insights/blog/composition-vs-inheritance-how-choose

timm commented 5 years ago

https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance