Object-oriented programming in under 5 minutes:
Object-oriented programming (OOP) is a programming paradigm in which programs are designed using ๐ฐ๐น๐ฎ๐๐๐ฒ๐ ๐ฎ๐ป๐ฑ ๐ผ๐ฏ๐ท๐ฒ๐ฐ๐๐. This design allows related functions and data to be grouped together in ๐๐ฒ๐น๐ณ-๐ฐ๐ผ๐ป๐๐ฎ๐ถ๐ป๐ฒ๐ฑ ๐ฎ๐ป๐ฑ ๐ฟ๐ฒ๐๐๐ฎ๐ฏ๐น๐ฒ ๐๐ป๐ถ๐๐.
A class is a template or blueprint from which objects are made from. Classes define the properties and methods that an object can have, and objects are unique instances of a class.
For example, let's say you want to create a life simulation game where players can adopt and raise pets. You would have a "Pet" class that defines the properties of each pet (like "name" and "age"), as well as behaviors they can do (like "speak" and "eat").
You could then create objects, or instances, of this class for each specific pet. Each object could have its own values for each property. For example, you could have a 2-year-old pet named "Winston", & a 1-year-old pet named "Wesley"; both initialized from the "Pet" class.
๐ข๐ฏ๐ท๐ฒ๐ฐ๐-๐ผ๐ฟ๐ถ๐ฒ๐ป๐๐ฒ๐ฑ ๐ฝ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด ๐ต๐ฎ๐ ๐ฐ ๐บ๐ฎ๐ถ๐ป ๐ฝ๐ฟ๐ถ๐ป๐ฐ๐ถ๐ฝ๐น๐ฒ๐; ๐ฒ๐ป๐ฐ๐ฎ๐ฝ๐๐๐น๐ฎ๐๐ถ๐ผ๐ป, ๐ถ๐ป๐ต๐ฒ๐ฟ๐ถ๐๐ฎ๐ป๐ฐ๐ฒ, ๐ฎ๐ฏ๐๐๐ฟ๐ฎ๐ฐ๐๐ถ๐ผ๐ป, ๐ฎ๐ป๐ฑ ๐ฝ๐ผ๐น๐๐บ๐ผ๐ฟ๐ฝ๐ต๐ถ๐๐บ.
๐๐ป๐ฐ๐ฎ๐ฝ๐๐๐น๐ฎ๐๐ถ๐ผ๐ป hides internal details but exposes data & methods via a public interface, preventing unintentional changes. E.g. โ a player can view a pet's age but can't accidentally change it. But they can run methods avail on the public interface like changing a pet's name.
๐๐ป๐ต๐ฒ๐ฟ๐ถ๐๐ฎ๐ป๐ฐ๐ฒ allows classes to inherit properties and methods from other classes, making code reusable and organized. E.g. โ A "SuperPet" class that extends from "Pet "and would inherit "age", "name", "eat", and "speak"; while defining new behaviors like "fly"
๐ฃ๐ผ๐น๐๐บ๐ผ๐ฟ๐ฝ๐ต๐ถ๐๐บ is a principle that enables objects to change their form by extending or overriding existing methods. E.g. A "Dog" & "Cat" class that extended from the "Pet", shouldn't share the same "speak" method. You'd override it to have its own logic like "woof" or "meow"
๐๐ฏ๐๐๐ฟ๐ฎ๐ฐ๐๐ถ๐ผ๐ป reduces complexity by only surfacing the information needed for a given context or use case. E.g. A โPlayerโ class doesnโt need to know how the โeatโ method works in the โPetโ class, it just needs to know how to interact with it โ i.e. its input and output.
OOP provides a way to design your program that makes it ๐ฟ๐ฒ๐๐๐ฎ๐ฏ๐น๐ฒ, ๐๐ฒ๐ฐ๐๐ฟ๐ฒ, ๐๐๐ฎ๐ฏ๐น๐ฒ, ๐ฎ๐ป๐ฑ ๐ฒ๐ฎ๐๐ ๐๐ผ ๐๐ป๐ฑ๐ฒ๐ฟ๐๐๐ฎ๐ป๐ฑ. But it isn't without disadvantages. A couple of arguments against it are that it can lead to over-engineering and complexity on a large scale.
Object-oriented programming (OOP) is a programming paradigm in which programs are designed using ๐ฐ๐น๐ฎ๐๐๐ฒ๐ ๐ฎ๐ป๐ฑ ๐ผ๐ฏ๐ท๐ฒ๐ฐ๐๐. This design allows related functions and data to be grouped together in ๐๐ฒ๐น๐ณ-๐ฐ๐ผ๐ป๐๐ฎ๐ถ๐ป๐ฒ๐ฑ ๐ฎ๐ป๐ฑ ๐ฟ๐ฒ๐๐๐ฎ๐ฏ๐น๐ฒ ๐๐ป๐ถ๐๐.
A class is a template or blueprint from which objects are made from. Classes define the properties and methods that an object can have, and objects are unique instances of a class.
For example, let's say you want to create a life simulation game where players can adopt and raise pets. You would have a "Pet" class that defines the properties of each pet (like "name" and "age"), as well as behaviors they can do (like "speak" and "eat").
You could then create objects, or instances, of this class for each specific pet. Each object could have its own values for each property. For example, you could have a 2-year-old pet named "Winston", & a 1-year-old pet named "Wesley"; both initialized from the "Pet" class.
๐ข๐ฏ๐ท๐ฒ๐ฐ๐-๐ผ๐ฟ๐ถ๐ฒ๐ป๐๐ฒ๐ฑ ๐ฝ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด ๐ต๐ฎ๐ ๐ฐ ๐บ๐ฎ๐ถ๐ป ๐ฝ๐ฟ๐ถ๐ป๐ฐ๐ถ๐ฝ๐น๐ฒ๐; ๐ฒ๐ป๐ฐ๐ฎ๐ฝ๐๐๐น๐ฎ๐๐ถ๐ผ๐ป, ๐ถ๐ป๐ต๐ฒ๐ฟ๐ถ๐๐ฎ๐ป๐ฐ๐ฒ, ๐ฎ๐ฏ๐๐๐ฟ๐ฎ๐ฐ๐๐ถ๐ผ๐ป, ๐ฎ๐ป๐ฑ ๐ฝ๐ผ๐น๐๐บ๐ผ๐ฟ๐ฝ๐ต๐ถ๐๐บ.
๐๐ป๐ฐ๐ฎ๐ฝ๐๐๐น๐ฎ๐๐ถ๐ผ๐ป hides internal details but exposes data & methods via a public interface, preventing unintentional changes. E.g. โ a player can view a pet's age but can't accidentally change it. But they can run methods avail on the public interface like changing a pet's name.
๐๐ป๐ต๐ฒ๐ฟ๐ถ๐๐ฎ๐ป๐ฐ๐ฒ allows classes to inherit properties and methods from other classes, making code reusable and organized. E.g. โ A "SuperPet" class that extends from "Pet "and would inherit "age", "name", "eat", and "speak"; while defining new behaviors like "fly"
๐ฃ๐ผ๐น๐๐บ๐ผ๐ฟ๐ฝ๐ต๐ถ๐๐บ is a principle that enables objects to change their form by extending or overriding existing methods. E.g. A "Dog" & "Cat" class that extended from the "Pet", shouldn't share the same "speak" method. You'd override it to have its own logic like "woof" or "meow"
๐๐ฏ๐๐๐ฟ๐ฎ๐ฐ๐๐ถ๐ผ๐ป reduces complexity by only surfacing the information needed for a given context or use case. E.g. A โPlayerโ class doesnโt need to know how the โeatโ method works in the โPetโ class, it just needs to know how to interact with it โ i.e. its input and output.
OOP provides a way to design your program that makes it ๐ฟ๐ฒ๐๐๐ฎ๐ฏ๐น๐ฒ, ๐๐ฒ๐ฐ๐๐ฟ๐ฒ, ๐๐๐ฎ๐ฏ๐น๐ฒ, ๐ฎ๐ป๐ฑ ๐ฒ๐ฎ๐๐ ๐๐ผ ๐๐ป๐ฑ๐ฒ๐ฟ๐๐๐ฎ๐ป๐ฑ. But it isn't without disadvantages. A couple of arguments against it are that it can lead to over-engineering and complexity on a large scale.
Want more engineering insights like this?
Subscribe to our free newsletter for a weekly deep-dive and roundup of our best content: blog.levelupcoding.co
Subscribe to our free newsletter for a weekly deep-dive and roundup of our best content: blog.levelupcoding.co
Loading suggestions...