Chris Staudinger
Chris Staudinger

@ChrisStaud

2 تغريدة 35 قراءة Sep 14, 2023
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.
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

جاري تحميل الاقتراحات...