1. Examples of Object-Oriented Programming
Now let's take a look at some of the advantages and disadvantages of object-oriented programming, as well as some examples of how this paradigm is used in the real world.
2. Applications of object-oriented programming
Applications for object-oriented programming are numerous, and many of us interact with software that uses this paradigm under the hood on a regular basis. Some of the more common use cases for object-oriented programming are simulations of various kinds, such as for modeling stock prices or simulating the physics of moving objects. Many concepts can be modeled with objects and classes. Object-oriented programming is also used in the creation of a specific type of database known as an object-oriented database, of which MongoDB is one example. Finally, since it is possible to generate many objects of the same class at the same time, this paradigm is helpful for problems that require this kind of parallelization, such as sorting many independent lists.
3. Pros and cons of object-oriented programming
Some of the biggest advantages of object-oriented programming are that, used correctly, it is easier to maintain the security and privacy of data in this paradigm, as information could be kept within a particular class rather than shared with the entire program. The modularity of having different classes also makes it possible for multiple developers to work simultaneously without conflict more easily. Finally, well-organized code in this paradigm is generally quite reusable and easy to maintain.
On the flip side, object-oriented programs often take longer to run than programs written in other paradigms. These programs also tend to be longer in terms of the number of lines of code. The result is that object-oriented programming is not appropriate for all tasks, and can result in long, slow, wordy programs if used inappropriately.
4. Public and private attributes
On the previous slide, we mentioned that it's possible to maintain greater data security with object-oriented programming than other paradigms if used correctly. One mechanism for doing this is via the use of public and private attributes within a class. At a high level, "public" refers to those attributes (such as "name" in our Dog example) and methods (such as "bark") within a class that are available throughout the entire program. "Private" refers to attributes and methods that are only accessible within the class itself.
Every language that implements object-oriented programming handles this distinction a bit differently. In Python, everything is public by default, and in fact when we make something "private", it's still technically accessible outside of the class indirectly, but we use a specific syntax to prevent other programmers from referencing it directly: the double underscore prefix.
5. Public vs. private example
To understand why we might want to use private attributes, consider again our example of the Dog class. Imagine that we want to include another attribute for our dogs: whether or not they are hungry. We will say that every dog starts out hungry until they eat. We can then define a method "eat" that sets the dog's hungry attribute to false.
We have indicated that "hungry" is a private attribute, and we don't want anyone changing this attribute unless they call the "eat" method on the dog. Because of this, if we try to change it directly outside of the class, we will get an error! The dog has to eat in order to stop being hungry.
6. Let's practice!
We've seen a lot of new information about the value and applications of object-oriented programming. Now let's put that to the test in some exercises.