Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add abstractions #1

Open
timm opened this issue Jan 11, 2019 · 5 comments
Open

add abstractions #1

timm opened this issue Jan 11, 2019 · 5 comments

Comments

@timm
Copy link
Contributor

timm commented Jan 11, 2019

inheritance

polymorphism

@timm
Copy link
Contributor Author

timm commented Jan 11, 2019

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
Copy link
Contributor Author

timm commented Jan 11, 2019

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
Copy link
Contributor Author

timm commented Jan 11, 2019

@timm
Copy link
Contributor Author

timm commented Jan 11, 2019

@timm
Copy link
Contributor Author

timm commented Jan 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant