1. Object-oriented programming (OOP) is a programming paradigm in which programs are designed using classes and objects.
This design allows related functions and data to be grouped together in self-contained and reusable units.
2. 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.
3. 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 behaviours they can do (like "speak" and "eat").
4. 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 initialised from the "Pet" class.
5. Object-oriented programming has 4 main principles: encapsulation, inheritance, abstraction and polymorphism.
6. Encapsulation 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 change a pet's name.
7. Inheritance allows for 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 behaviours like "fly"
8. Polymorphism 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 their own logic like "woof" or "meow"
9. Abstraction and encapsulation both intend to hide information, but abstraction does so to reduce complexity.
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.
Recap:
OOP provides a way to design your program that makes it reusable, secure, stable, and easy to understand.
But it isn't without disadvantages.
A couple of arguments against it is that it can lead to over-engineering and complexity on a large scale.
Thanks for viewing 🫶
If you found this tweet helpful, don't forget to:
Follow us @_blackdevdes for more.
Retweet the first tweet.
Enable notification to be the first to get our posts 🔔