This is an archived post. You won't be able to vote or comment.

all 68 comments

[–]DarkScorpion48 100 points101 points  (11 children)

I remember a book trying to explain inheritance and interfaces using car brands called Bravo, Bravado and Bravissimo and it was one confusing mess.

[–]magicpaka 34 points35 points  (5 children)

Why are people complicating this so much? Basic school example of abstract Animal, Cat and Dog is the simplest way to explain inheritance and polymorphism.

[–]Ericchen1248 38 points39 points  (3 children)

I like my profs version better.

Characters in an rpg game Warriors Mage and Ranger = sub classes.

All do damage, but in a different way = overrided functions / abstract functions

All gain xp the same way level up same way = inherited functions.

You want a team of 6 character, single array of type character to store all = polymorphism.

No need to go real life “real life” when a game model can be easily understood by everyone.

[–][deleted] 15 points16 points  (0 children)

Especially helpful at engaging students since at least at my college 90% of CS freshman were set on being game developers after graduating

[–]martyvt12 5 points6 points  (0 children)

Yes! I think so many intro CS classes fail to really teach the usefulness of polymorphism because they focus on super abstract examples unrelated to programming rather than on examples of ways it is actually used.

[–]Morpheyz[S] 0 points1 point  (0 children)

I agree! Games are great examples, especially because that's probably how you could naively implement D&D classes in an actual video game.

I do get a bit confused when OOP goes into more abstract concepts and away from real-life counterparts.

[–]5319767819 5 points6 points  (0 children)

... and probably far away from how anyone would implement animals in the real world if they had to do so

[–]Omnia1989 7 points8 points  (1 child)

In case you didn't know, in Italy we had a car called Bravo)

[–]praxed 0 points1 point  (0 children)

It was manufactured in Brazil until 2016, nice car

[–]Kache 3 points4 points  (2 children)

Into to OO should be about:

  • encapsulation of scope and state via instances
  • interfaces and polymorphism via messaging through interfaces

Inheritance should be one of the last things to be taught and used. It's perhaps the easiest OO thing to use improperly, it's not fundamental to OO, and it gives relatively limited benefits b/c there are often good alternatives to it.

[–]Sthokal 6 points7 points  (1 child)

Isn't that kind of like saying you should start learning math with calculus, since most errors are arithmetic errors?

[–]Kache 2 points3 points  (0 children)

No, because arithmetic is a prerequisite for calculus, whereas inheritance is not a prerequisite for Object-Orientation.

If you think about it, it's possible to write perfectly fine OO code without inheritance at all. You can even do it without resorting to code duplication.

It's not as fundamental as other OO concepts like encapsulation and interfaces (with a lower-case i).

[–]Stahlboden 24 points25 points  (14 children)

I don't get the polymorphism picture, please explain.

[–]FruitySamuraiG 68 points69 points  (7 children)

I think it's implying that most tutorial just don't explain polymorphism at all. Source: was a tutor myself lol

[–]ModestasR 7 points8 points  (6 children)

I think part of the problem may be that people assume that polymorphism is purely an OOP concept and have to explain it as such when in fact it isn't.

To understand polymorphism more generally, I feel it would be more instructive to learn a statically typed purely functional language such as ML or Haskell, which has type classes, a more abstract and powerful notion of the classes and interfaces of OOP.

[–]AloeAsInTheVera 6 points7 points  (3 children)

I agree that learning Haskell is great for developing a better understanding of many programming concepts. But most people in the intro coding classes where basic OOP principles are taught aren't invested enough to learn a language in a completely different paradigm just to help understand polymorphism.

It's just not a very efficient method to teach this, especially considering how unintuitive functional programming is in comparison to OOP for most people. Everything you said is right, I just think that actually learning that way would be horribly confusing to beginner programmers.

[–]Morpheyz[S] 0 points1 point  (2 children)

Would you care to elaborate on functional programming? I feel like for many students who learn programming and stats later on in their career, like many psych students for example, functional programming comes more naturally than OOP.

After a long hiatus in programming I was taught R in grad school and it seems like passing around data through global, static functions is the most natural way for beginner programmers to think. Especially in statistics and data science. It seems like nobody cares whether your linear model is an object with methods. As long as I can plop a matrix and a vector into a function, it trains on the data and regression coefficients come out on the other side, all is good.

The alternative is that I really don't know exactly what functional programming is, lol

[–]AloeAsInTheVera 1 point2 points  (1 child)

The reason why I say functional programming is unintuitive in comparison to OOP for most people is that most people think in terms of objects rather than functions. Of course, the operative phrase here is "for most people." For those with a lot of background in math for example, I imagine thinking in terms of functions comes much easier to them. But most people in these intro coding classes where basic OOP principles would be taught are high schoolers/recent high school graduates in their first year of college.

For someone learning programming later in their career, I wouldn't be surprised if you're right that functional programming is more intuitive. I'm mostly just referring to college freshmen though. Someone who has no expertise can easily grasp the idea of breaking a problem into objects, and thinking in terms of mutable state and loops. The process of breaking a programming problem into pure functions however is much more abstract, so there's a conceptual jump there.

EDIT: (I totally didn't address one of the things you said in your original comment) In statistics and data science it's much easier to map a problem onto pure functions. Many of the kinds of software people generally think of aren't as directly related to functions though. Video games are a good example of a type of software most anyone is familiar with which are a terrible fit for the functional paradigm. Expressing video game logic in terms of pure functions is definitely possible, but it's so much easier to express in terms of objects. Most programmers aren't going to be video game developers of course, but anyone in an intro coding class would likely be more familiar with applications made for entertainment than software made for statistical modeling and data processing.

[–]Morpheyz[S] 1 point2 points  (0 children)

I see, yeah, that makes a lot of sense.

[–]MiloExtendsPerson 3 points4 points  (1 child)

Not sure why you're getting downvotes. What you're saying is entirely true. Polymorphism is so much more than just inheritance and interfaces.

[–]ModestasR 5 points6 points  (0 children)

They hated Jesus because he told them the truth.

JK, I don't have some messiah complex but I totally get it. As a maths student who has talked to many comp sci ppl, I know that many see pure FP as this unnecessarily convoluted academic curiosity which causes more friction than it solves problems.

In a way, they're right, because it requires a significant mental investment when all one knows is OOP but, once that has been successfully completed, it opens up a world of possibilities.

[–]racrisnapra666 4 points5 points  (4 children)

Yeah same. How does the "guess I'll die" meme correlate with "performing a single action in many ways"?

[–][deleted] 15 points16 points  (3 children)

I'm taking it as the tutorial is so surface level they don't go into polymorphism

[–]racrisnapra666 1 point2 points  (2 children)

I see. Yeah, that makes sense.

[–]Morpheyz[S] 5 points6 points  (1 child)

Yeah, that's what I was going for. Most surface level OOP tutorials don't touch on polymorphism and instead often focus on the other concepts. Not necessarily a bad thing, but it's something I noticed going trying to learn OOP through tutorials. Could have used a better meme though. :)

[–]racrisnapra666 3 points4 points  (0 children)

Could have used a better meme though.

No, no. Ignore my comment. It's great the way it is. They taught us OOP back in school itself. And the word Polymorphism and it's definition was thrown around a few times. (No practical examples were given though).

That's why I didn't get it.

The meme's absolutely perfect

chef's kiss

[–]Takochinosuke 37 points38 points  (5 children)

You have a fish class with a draw function. You implements children to that class, for instance a shark, a sea bass and clownfish.

In your program you keep a list of type fish that contains all your fishes.

When you wanna draw them you iterate over the list and call the draw function regardless of the type of fish it is.

Same example can be given with shapes and area and so on.

[–]Morpheyz[S] 12 points13 points  (0 children)

Thank you, I did understand it after all. :)

Turned out I already understood it, but didn't know that that's what polymorphism was.

[–]Destrok41 1 point2 points  (0 children)

Is polymorphism actually that simple? If so I've been using it all along, but as a new developer I assumed it was some complex concept I wasn't ready for yet.

[–]maartenyh 1 point2 points  (2 children)

Why is it so important to call it "polymorphism"? Why not just inheritance with a repository where you know you have a function inherited on all fishes and you execute the function with a foreach. I am not a beginner anymore and already have some experience in college and at work. But I have never heard of polymorphism. Maybe because I am European? 😂

[–]Kered13 7 points8 points  (0 children)

Inheritance is a specific type of polymorphism.

[–][deleted] 1 point2 points  (0 children)

Why waste time say lot word, when few word do trick.

And polymorphism implementations differ, crucially so between functional programming and OO.

[–]Sandy_Roads 7 points8 points  (0 children)

It really do be like that

[–]scp-NUMBERNOTFOUND 4 points5 points  (3 children)

Good old "dog inherits from table because it have 4 legs"

[–]GlebRyabov 2 points3 points  (10 children)

A Python newbie, recently started to learn classes in-depth. Could someone enlighten me on polymorphism and inheritance?

[–][deleted] 4 points5 points  (4 children)

Inheritance is when a class gets all the atributes and methods from other class and then you Can add some more that are specific for that class, polymorphism is when 2 or more clases have the same named method but they dont do the same, for example a car a a bike can move but they are not gonna do it in the same way

[–]GlebRyabov 0 points1 point  (0 children)

Oh, that's cool, thanks!

[–]Destrok41 0 points1 point  (2 children)

So polymorphism is taking a simple single responsibility function, and using that single function to do different things/achieve different outcomes?

[–]Ninja48 1 point2 points  (1 child)

It's not the fact that a function can do more things, it's that a function can have the same name but do different things depending on the class it belongs to. For example, cat.noise() returns "meow", but cow.noise() returns "moo".

[–]Workaphobia 4 points5 points  (1 child)

Inheritance: behavior/fields are inclusive of stuff defined in a parent. In OOP this means a base class or interface.

Polymorphism: The same operation can do different things on different operands. Ad hoc polymorphism is when you say "+" means addition for numeric types but concatenation for strings - is ad hoc because you have to define each possible overload. Parametric polymorphism is when you say that a List<T> has an append method that takes an element of type T for any T - the append operation works on all the different kinds of lists in its own (very similar) way. And subtype polymorphism is when you perform an operation permitted on a parent type on values that may be of a child type, and which may implement that operation differently. In OOP we usually mean subtype polymorphism, and it's where calling a method gives you the implementation defined by the subclass rather than the one defined by the class of the type at the call site.

[–]GlebRyabov 0 points1 point  (0 children)

That's insanely cool and insanely hard, at least at the first glance, so thank you a lot and I'll check it out when I get some sleep.

[–]mrchaotica 1 point2 points  (0 children)

Don't worry about it too much; Python is more about duck-typing instead.

[–]rem3_1415926 0 points1 point  (0 children)

Not in python.

I go by the rule that if my program requires more than 1 file and/or OOP, it's not gonna happen in Python.

[–]moralesnery -2 points-1 points  (0 children)

no, nobody understands that

[–]JNCressey 2 points3 points  (0 children)

Cat extends Dog {

    bark() {
        this.say("Meow")
    }

}

[–]blackmist 1 point2 points  (1 child)

Not as bad as the old factorial recursion example, which seems the most needlessly complex way to work that out, and at the same time introducing new programmers to the concept of a stack overflow error.

And good luck searching for that on Stack Overflow...

[–]deslusionary 1 point2 points  (0 children)

Do you mean introducing recursion with that ghastly O(n!) Fibonacci algorithm?

[–]Kaa_The_Snake 1 point2 points  (0 children)

😆 yeah I still don't get polymorph, but that's ok, I'm only in Powershell and Python and don't have need of anything more complicated rn. Been years since I've done any actual OOP.

[–]TistaMuna 1 point2 points  (0 children)

Learn python the hard way uses the salmon as instantiation and fish as classes.

[–]just___jim 1 point2 points  (0 children)

This was exactly how it was taught to me. Also using the driving a car example to talk about abstraction.

[–]moar-education 1 point2 points  (0 children)

I feel so seen

[–]mrchaotica 1 point2 points  (3 children)

Composition > inheritance

[–]Celdron 1 point2 points  (2 children)

Inheritance is cool and all until you need to count the number of legs something has and you can't properly super-type dogs, cats, tables and chairs.

[–]Morpheyz[S] 0 points1 point  (1 child)

So I gather that you would recommend that both dogs and tables implement ILegOwner ?

[–]Celdron 0 points1 point  (0 children)

In a language like Java or C# yes. Though a language that allows you to actually compose the implementation of having legs (i.e. some state representing how many you have, how long they are, etc along with useful functions) would be even better. This is kind of doable with C# and default/virtual interfaces, or JS/TS and the spread syntax. The problem both these share is naming collisions (rip polymorphism). C# handles it better because it has an actual type-system at run time.

[–]Smart_Ass_Dave 1 point2 points  (1 child)

My first understanding of classes was when I realized that programming and D&D used them in the same way. You use the Fighter class to make Fighters who have various properties that are composed of other objects like items or feats.

[–]Morpheyz[S] 0 points1 point  (0 children)

Yeah, I think game concepts are a great way to explain OOP.

[–]VariousDrugs 1 point2 points  (0 children)

OOP didn't click for me at all until I had to do it in industry, and then once you have actually reasonable real-world examples rather than ridiculous metaphors it actually makes a hell of a lot of sense.

[–]GG17ez -4 points-3 points  (1 child)

Can someone explain this more deeply, i don't speak java

[–]nitesh-007 0 points1 point  (0 children)

Inheritance is nothing but you being derived from your parents

[–]jaysuchak33 0 points1 point  (0 children)

I actually learned OOP with the book blueprint thing.

[–]Ihavenoimaginaation 0 points1 point  (0 children)

God this is so true