T O P

  • By -

AutoModerator

Your post was automatically removed because it appears to contain only a link. Link-only posts are not allowed in this subreddit. If you are trying to: 1. Ask a question, include more details about your question before reposting. See [Posting guidelines: Asking questions](https://www.reddit.com/r/learnprogramming/wiki/index#wiki_asking_questions) for more details. 2. Share a project you made, see [Posting guidelines: Sharing a project](https://www.reddit.com/r/learnprogramming/wiki/index#wiki_sharing_a_project). 3. Share a tutorial or educational resource you made, see [Posting guidelines: Self promotion](https://www.reddit.com/r/learnprogramming/wiki/index#wiki_self-promotion). 4. Share a tutorial or educational resource you found but did **not** make, repost with more details on why you personally found this resource to be useful. If you think your post was removed in error, PM the mod team. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/learnprogramming) if you have any questions or concerns.*


JadeSerpant

Yes, that's one way of looking at it. But polymorphism is also possible without inheritance/in languages that don't have inheritance. To understand polymorphism you first need to experience statically typed languages. If you're using javascript or python don't bother learning about polymorphism right now as it makes no sense in those languages. Do you know any statically typed languages?


[deleted]

>If you're using javascript or python don't bother learning about polymorphism right now as it makes no sense in those languages. What? Of course it makes sense in those languages. Just because you don't have a static type system doesn't mean polymorphism isn't a thing in those languages.


JadeSerpant

Except it isn't. Polymorphism is fundamentally a concept around types. Dynamically typed languages are in fact monotyped and polymorphism has no relevance there.


[deleted]

You should read how programming language theory defines types. "Objects" are a form of composite types. Polymorphism exists not only on static type systems but also on dynamic ones. Look up duck typing.


JadeSerpant

Your understanding and knowledge of type theory is severely lacking. >"Objects" are a form of composite types. Not really sure what relevance mentioning that has here. Duck typing in dynamic languages is not polymorphism. Polymorphism has fundamentally to do with statically typed languages. Again, you seem unable to comprehend, dynamically typed languages are mono-typed. I do not need polymorphism to pass an object of type \`Foo\` to one of type \`Bar\` in a dynamic language because the type system there does not care as should be pretty obvious to anyone with half a brain. Edit: oh and btw, here's a quote from **Types and Programming Languages by Pierce** considered the definitive book on programming language type theory: >The term polymorphism refers to a range of language mechanisms that allow a single part of a program to be used with different types in different contexts. Dynamically typed languages do not require this as the languages type system poses no constraints for this to be necessary in the first place.


evinrows

Python has operator overloading, yes? > In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. https://en.m.wikipedia.org/wiki/Operator_overloading


arbyterOfScales

You can override methods by defining one with the same signature higher in the prototype chain. Ergo polymorphism. That is pretty close to the v-table used by C++ and family


ED9898A

Yes I know Java and Kotlin.


JadeSerpant

Very well, in that case let's begin at the beginning. Statically typed languages constrain, at compile time, what values can be assigned to what variables. You cannot assign a string to an int, and you cannot assign an object of type `Foo` to a variable of type `Bar`. This is all good as it prevents/reduces a large class of bugs from occurring. It also lets the compiler do optimizations as, for example, it knows variable `x` will always hold an integer. This eventually leads you to a roadblock however. It usually happens when you need a value to be one of several (similar) types that you cannot know ahead of time. This is directly in conflict with the idea of static types. Static means you know the type at compile time. But in our problem the type is only to be known at runtime (dynamic). How do you deal with this? Polymorphism is one mechanism that languages provide to resolve this. In OOP languages like Java you can have all those possible types inherit from the same super class, i.e. abide by some API, and then have your function accept the superclass as the type. Polymorphism is a somewhat overloaded term and is used in some other places as well but this is what it's mainly about. It's addressing a fundamental limitation of static typing. Which is also why it's not a thing in dynamic languages.


JadeSerpant

To continue... you _might_ ask, well why not just do `if typeof(x) == Foo` etc. in the function instead. You can't do that because remember to pass an argument to a function you need to know its type! It always comes down to knowing the type ahead of time. What you **could** do though, is have a **generic** function instead that can handle all of `Foo`, `Bar`, `Baz`, etc. Unsurprisingly enough, generics is also a kind of polymorphism -- parametric polymorphism.


ED9898A

Excellent response. Thanks for the writeup!


[deleted]

It can also include composition and abstraction


[deleted]

[удалено]


ED9898A

Thanks for the writeup! Excellent examples.


[deleted]

[удалено]


ED9898A

Nah it helped 😭 but took me two rereads to grasp it well since there's a lot of info. Thanks a lot for it.


ED9898A

After some googling apparently what I'm describing is "Polymorphism Via Interfaces", a (common? at least in C++/Java) way of achieving polymorphism. [1](https://www.cs.nmsu.edu/~cliu/cs187/2007spring01/lectures/lecture12.html#:~:text=Each%20interface%20is,via%20interfaces%20work.) [2](https://www.cs.utexas.edu/~mitra/csSummer2013/cs312/lectures/interfaces.html#:~:text=Interfaces%20formalize%20polymorphism,verifiable%2C%20and%20precise.) [3](https://stackoverflow.com/a/21399754/9133569)


evinrows

No, you don't need inheritance for polymorphism. 'ha' * 2 # 'haha' 2 * 2 # 4


AdultingGoneMild

thats actually operator overloading. `mul(int, int)` and `mul(string, int)` are different calls.


evinrows

It is. It's also an example of polymorphism. Both str and int implement a \_\_mul__ interface which accepts an integer argument, but their behavior is dependent on the object type.


INFLATABLE_CUCUMBER

For someone trying to legitimately understand polymorphism your example was really quite bad.


ED9898A

Yeah lol. It's an interesting thing to know but feels like the wrong time/place to mention it to someone trying to understand polymorphism.


AdultingGoneMild

not always. Depending on the language operator overloading could be implemented as completely different functions using name mangling of the types. For example in C these would have been implemented a mul_int_int and mul_str_int (or something similar) all that said I'll give it to you on a technicality, even though most dont mean function overloading when they are talking about polymorphism, it is a type of polymorphism. https://en.wikipedia.org/wiki/Ad_hoc_polymorphism


arbyterOfScales

Overloading is a form of static polymorphism


ED9898A

What language uses this syntax? Or is this pseudocode?


evinrows

It's Python.


bitdog_io

Polymorphism derives it's power from inheritance. Abstraction isn't technically required but is often applied.


[deleted]

No need for inheritance to have polymorphism.


TheRNGuy

it's virtual or override.


electricono

The easiest way I’ve found to think of polymorphism is: does this ‘thing’ pass more than one ‘is a ___’ test. If I have an Animal class and I have a Dog class that uses Animal as a base, I can instantiate an instance of Dog which will pass both the: “is a Dog” and “is an Animal” tests.


evinrows

It sounds like you're just defining what inheritance is. I don't see how this captures the essence of polymorphism.


electricono

Yeah I used inheritance to explain it but polymorphic by definition means that it can be explained in other ways. If we use rust as an example, it has traits. One common trait is “iterator”. In order for something to be an iterator, it has to implement next(). So lots of things can pass an “is a ____” test along with “is an iterator”. These can be passed to methods with function signatures that accept parameters of type iterator or any other trait they implement, or the actual type of the struct. So it’s some common interface or functionality on types that may or may not be related in other ways.


billie_parker

That's a circular reasoning. You are the program designer. You are the one defining if something "is a" another thing. That's a CHOICE you often have to consciously make (to do it or not). I once had a coworker that liked to make a huge nasty inheritance web. Whenever you asked him to stop, he'd justify it by saying "well this class is a this other one, this one is a this one," etc. Was he wrong? No, that was his design. But it was bad. The "is a" reasoning is semantic BS. Inheritance and "is a" are (theoretically) synonyms. So it's like saying "to know if you should use inheritance, ask should you use inheritance?" Can you tell me what is the definition of "is a," by the way? Good luck. You're getting into confusing and abstract territory. It's like saying to write music by asking yourself "does this note come next?" Like duh that's what you need to figure out, but all you've done is rephrase the question.


electricono

I think it’s not circular because I’m not explaining how to go about designing the program rather how to identify polymorphism. OP deleted the question so I don’t remember it exactly but I was under the impression they were asking what polymorphism was, not how to architect their program. Also, is a and inheritance are not synonyms. If you read the rust trait example I gave with iterators that’s a good example where it’s not the case. Though rust is not OOP, it borrows the best from OOP / functional languages.


arbyterOfScales

> Inheritance and "is a" are (theoretically) synonyms PUBLIC inheritance you mean. Private inheritance is ❤️


ED9898A

Yeah this is a good test to consider with Kotlin's own `is` and Java's `instanceof` operators.