Stuck on object oriented programming

as above gone through freecodecamp and w3schools chatgpt but i still don’t understand object oriented programming

i understand that classes are like molds for building new objects, methods are what you can do with the object but i don’t understand constructors supertypes subtypes polymorphism prototypes
Can you please explain it in the simplest terms I don’t understand some of it even with chatgpt.

1 Like

Constructors give a class properties. They help create the instance of a class. Think of it as putting material into a mold and pressing it. The constructor essentially does that.

Let’s take an example for supertypes ,subtypes, polymorphism, and prototypes. Imagine there is an Animal, Dog, and Cat object.

Supertypes are essentially more abstract versions of an object where some of their properties come from. Like for example the supertype of Dog is Animal. And the supertype of Cat is Animal.

Subtypes are kinda like the opposite of supertypes. They’re more specified versions of an more generic class. Cat is a subtype of Animal. Dog is also an alternate subtype of Animal.

Polymorphism is essentially transforming subtypes into their supertypes. This lets us manipulate the object instances more easily. For example, if I have a collection of animals consisting of Dogs and Cats; I can treat them as one group of Animals. Then call a property that belongs to Animal like “Speak” on all of them.

The following example assumes that the Cat,Dog, and Animal classes exist; and that Animal has been implemented by Cat and Dog with it originally defined in Animal.

var dog1 = new Dog(); 
var cat1 = new Cat(); 

var animalArray = [dog1,cat1];

animalArray.forEach((animal) => animal.Speak()); 

A prototype essentially holds all the information that makes an object , an object. Every instance of an object shares these properties. It holds the constructor, any variables defined in the object, and all of the methods.

Hope this helps. :slight_smile:

1 Like

The above post explains it pretty well, and I’ll add on a few more points that I got from experience.

I’ve thought of two ways to explain object-oriented programming. The first one is similar to the “mold” concept you mentioned, in which object-oriented programming tries to model (a small subset) of the world, and the relationships between stuff in that world. Supertypes is a way to characterize a particular relationship which is the concept of hierarchy from general to specific (like the cat and animal example in the above post). Polymorphism is a way to express commonalities between different things (like in the above post, how cat and dog share the property that they can “speak”).

The second way is to look at it from a practical standpoint - what do you need from object oriented programming that you won’t get from, say, regular old programming? (probably something called “imperative programming”). One answer is that it’s hard to bundle things together in a package that makes sense. A class is just a collection of data and methods bundled together in for convenience. It is convenient because it makes it easier to track the state since everything is in one place. Now, for different purposes, it might be more suitable to create different bundles – some more general and vague to give room for a variety of purposes, and some more specific to one purpose. This is why supertypes and subtypes exist - it gives flexibility since people may want different things out of the same program. In summary, OOP emerged from a need for convenience, although it may not seem so at times.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.