Transcript for:
Understanding Object-Oriented Programming Principles

hey my name is Mike I'm a developer from Philadelphia and in this video we're gonna answer the question why object-oriented programming in order to answer that question we'll take a look at object-oriented programming as a programming paradigm and compare it to two other popular programming paradigms we'll also take a look at the four pillars of object orientation so without further ado let's get into the video and get started in the early days of computing most programs developers were writing were simple and fairly straightforward but as computers started to become faster and more powerful the complexity of the program's developers were writing increased so as code became more and more complex programming languages and best practices evolved to suit the needs of developers eventually these evolutions and languages and best practices led to what we now call programming paradigms a programming paradigm is a specific style of organizing and writing programs with the goal of increased organization less bugs and better code maintainability in this video I'll be discussing one of the most popular programming paradigms used today object orientation we'll take a look at some of the features of object orientation and look at how it compares to other popular paradigms first it'll help to have an example we can use to illustrate these concepts so let's imagine a simple to-do list program this program would allow users to keep track of tasks they need to complete throughout their day each task would have a description and a status whether or not it's complete now before we dive into object orientation let's take a step back and examine what exactly makes up a program all programs have data and behavior the data is what the program knows in the case of our to do list that's the name of the tasks and whether or not they're complete the behavior is what the program can do with its data and our to do list program we can toggle whether or not a task is complete so we can toggle the task as complete or incomplete so in this case we have two pieces of data and one behavior this data and behavior makes up the core of what our program will do to really get a grasp on object orientation as a programming paradigm let's take a look at it alongside two other popular programming paradigms procedural programming and functional programming in procedural programming the data and the behaviors are spread out into the same set of step-by-step instructions when we need to use the behavior in this case toggling a to do task we can manipulate the data directly thus changing the overall state of our application in procedural programming the order of our code matters very much and our data is mutated directly as the program carries out its behavior in functional programming the data and the behavior are kept separate when we want to use our behavior and toggle the to do tasks instead of mutating the data directly our behavior will be abstracted into a pure function which takes our data in as an input and returns an updated copy of our data as an output in functional programming we never mutate the data directly instead we rely on pure functions to return a new copy of the updated data that's a little about procedural and functional programming paradigms now let's see how object orientation handles data and behaviors in object-oriented programming the data and the behavior is grouped together in objects an object is a special entity in our code which combines all of the data or attributes as well as all of the behaviors or methods into an easy-to-use container when we want to access the data or use the behaviors we do so through the object in the case of the to-do app we might create a task object for each task in our program which would keep track of the name and complete status of the task and would also know how to toggle it now that we've taken a look at object orientation compared to other popular paradigms like procedural and functional programming let's dive a bit deeper and talk about the four pillars of object orientation these are four characteristics of object orientation namely encapsulation abstraction inheritance and polymorphism which provide endless benefits to developers and are responsible for object-oriented programming success in the development world the first pillar is encapsulation now you'll recall when we created our to-do list using object orientation we combined all of the data and behavior for a to-do task into a single object in reality we would define a task class and then create task objects based on what's in the class these objects will then keep track of the to-do list description complete status and also the function al toggling the complete status in one simple container the first pillar encapsulation refers to an object's ability to do exactly this to bundle together related data and behavior and limit data scope so not only does keeping the data and behavior together like this make it easy for programmers to make sense of the data and behavior involved in a task it also prevents code outside the object from seeing how it works or modifying it in undesirable ways the task is now encapsulated into its own entity and can choose how it wants to interact with other code and what aspects of its behavior it wants to expose without encapsulation you'd still need to keep track of the data and behavior related to a task but it would be all jumbled throughout your program you might have the tasks name in one place it's complete status at another and the function for toggling it in another and that's just for one task imagine if your program needed to keep track of hundreds or thousands of tasks your code would soon resemble a plate of spaghetti encapsulation bundles data and behaviors together that should be together and makes it easy for that data and behavior to be used by other parts of our code the second pillar of object orientation is abstraction abstraction is object-oriented programming ability to hide complex logic from the user making the code that we write easier to use in other places a good example of abstraction in the real world is sending an email when you send an email you open up your email client type out a message and click send it's as simple as that you don't need to know anything about email servers or the way emails get encoded or formatted or transferred over the internet that entire process is abstract it away from you and you just have to hit the send button by placing all the complex logic of our programs in classes just like with emails were able to abstract it away from the people using our code and make everyone's life easier in our task class we've abstracted away the logic of toggling the is complete state in reality to toggle this value we may need some complex code maybe an if statement to check the current complete status some logic for updating that value or some other checks to like maybe make sure the current user has permission to toggle it but if we're using a task object in our code none of that matters all we need to do is call the objects toggle complete method and we get the same effect we don't need to know anything about how this is getting toggled or the logic behind when it can and can't be toggled we just called the class method and it works in this case we've abstracted away the logic behind toggling the complete status on a task ultimately making it easier for other pieces of code to perform that action now that we've taken a look at the first two pillars let's take a look at the third which is inheritance and object-oriented programming inheritance allows a class to inherit all of the functionality of another class this allows us to share code between multiple classes and reduce the amount of code that needs to be written overall now that we've built version one of our to-do app let's suppose we wanted to add an additional type of task to the app this would work like a normal task except it would have a deadline on it this special type of task can be called a deadline tasks in addition to having all the attributes and methods of the normal task the deadline tasks will also have a deadline attribute which would be a date or a time and an is overdue method which would tell you whether or not it's currently overdue now you might think that in order to create this new deadline task we'll have to copy and paste all of the code from the task class into the deadline task class but thanks to inheritance we actually don't as I mentioned inheritance allows a class to inherit all of the attributes and functionality of another class therefore we can use inheritance to have the deadlines tasks inherit all of the attributes and functionality of the task class this means that we don't have to re-implement or rewrite any of that logic all of that will automatically be available to any of the code interacting with a deadline task therefore users of the deadlines task objects will be able to work with them and toggle them complete or incomplete just like with a normal task without us having to rewrite any of that code generally inheritance works best in situations like this where you have an entity like a deadline task which is just a more specific version of another entity like our task class the final pillar of object-oriented programming is polymorphism the word polymorphism literally means having multiple in object-oriented programming when we use inheritance in our classes we can take advantage of polymorphism when using these classes to help make sense of this let's take a close look at our two tasks first we have a general task defined in the tasks class then we have a deadline task a more specific type of task which inherits all the functionality and attributes of the tasks class let's suppose we had two objects one is a task called my task and the other is a deadline task called my deadline tasks in order to understand polymorphism we have to look at these objects in terms of their datatypes remember that when we create a class we're actually creating a new data type the first object my task is the task data type it's an implementation of the task class which makes it a task type the second object my deadline task is a deadlines task data type it implements the deadlines class but because the deadline tasks inherits all of the attributes and functionality of the task class the my deadlines task object is also a task data type if you think about it this makes sense the my deadlines task object has all the same attributes and methods as the my task object along with a few more specific ones related to deadlines so any code that's designed to work with the task object will also be able to work with a deadline task object this in a nutshell is polymorphism it's what allows us to use either a normal task object or a more specific deadline task object in our programs interchangeably so anywhere a task object would go a deadline tasks could also go so there we have it the four pillars of object-oriented programming we have encapsulation which allows us to bundle together related data and behavior and limit data scope abstraction which allows us the high complex logic from the user and make our code more accessible inheritance where a class can inherit all the functionality and attributes of another class and polymorphism the idea that two or more classes can be of the same type and used interchangeably understanding the four pillars is the first step towards mastering any object-oriented programming language and by taking advantage of the concepts exposed by these pillars you can write software that is scalable clean and as bug free as possible thanks for watching I hope you learned something and I'll see you next time