Object-Oriented Programming
As Steve Jobs said, "Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here."
Some points about OOP I personally feel are Good way to code maintenance, Code re-usability, Method & property security.
Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages including JavaScript, C++, Java, and Python.
A class is an abstract blueprint used to create more specific, concrete objects. Classes often represent broad categories, like Car or Dog that share attributes. These classes define what attributes an instance of this type will have, like color, but not the value of those attributes for a specific object.
Classes can also contain functions, called methods available only to objects of that type. These functions are defined within the class and perform some action helpful to that specific type of object. Class templates are used as a blueprint to create individual objects. These represent specific examples of the abstract class, like "myBottle" or "myCar". Each object can have unique values to the properties defined in the class.
For example, say we created a class, Car, to contain all the properties a car must have, color, brand, and model. We then create an instance of a Car type object, myCar to represent my specific car. We could then set the value of the properties defined in the class to describe my car, without affecting other objects or the class template.
There are four building blocks of OOP:
Classes: Classes contain fields for attributes, and methods for behaviors. In our Man class example, attributes include name & birthday, while methods include buy() and updateSalary().
Objects: Objects are instances of classes created with specific data, for example in the code snippet below Jack is an instance of the Man class.
Methods: Methods represent behaviors. Methods perform actions; methods might return information about an object, or update an object’s data. The method’s code is defined in the class definition.
Attributes: Attributes are the information that is stored. Attributes are defined in the Class template. When objects are instantiated individual objects contain data stored in the Attributes field.
Four Principles of OOP
Inheritance: Inheritance allows classes to inherit features of other classes. Put another way, parent classes extend attributes and behaviors to child classes. Inheritance supports reusability. For instance, we are humans. We inherit certain properties from the class 'Human' such as the ability to speak, breathe, eat, drink, etc. The benefits of inheritance are programs can create a generic parent class, and then create more specific child classes as needed. This simplifies overall programming.
Encapsulation: Encapsulation means containing all important information inside an object, and only exposing selected information to the outside world. Attributes and behaviors are defined by code inside the class template. Then, when an object is instantiated from the class, the data and methods are encapsulated in that object. Encapsulation hides the internal software code implementation inside a class, and hides internal data of inside objects.
The benefits of encapsulation are :
Adds security: Only public methods and attributes are accessible from the outside
Protects against common mistakes: Only public fields & methods accessible, so developers don’t accidentally change something dangerous.
Protects IP: Code is hidden in a class, only public methods are accessible by the outside developers.
Abstraction: Abstraction means that the user interacts with only selected attributes and methods of an object. Abstraction uses simplified, high level tools, to access a complex object. Abstraction is using simple classes to represent complexity. Abstraction is an extension of encapsulation. Abstraction also serves an important security role. By only displaying selected pieces of data, and only allowing data to be accessed through classes and modified through methods, we protect the data from exposure.
Polymorphism: Polymorphism means designing objects to share behaviors. Using inheritance, objects can override shared parent behaviors, with specific child behaviors. Polymorphism allows the same method to execute different behaviors in two ways: method overriding and method overloading.
Conclusion Object Oriented programming requires thinking about the structure of the program and planning at the beginning of coding. Looking at how to break up the requirements into simple, reusable classes that can be used to blueprint instances of objects.