Object Oriented Programming - The Four Pillars of OOP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
object-oriented programming is one of my favorite computer science topics you could think of object-oriented programming like a PI no not that kind of pie a pie is an acronym for abstraction polymorphism inheritance and encapsulation with their powers combined they create the four pillars of object-oriented programming now the definition of object-oriented programming and instead of saying object-oriented programming the whole video I'm just gonna say Opie Opie is a programming paradigm based on the concept of objects objects can contain data in the form of attributes or properties and the actions in the forms of functions or methods I got that from a quick google search basically what it means is we're taking real world objects and representing them within code let's take a computer monitor for example some of its attributes could be the size or the resolution these would be its properties some of its actions could be turning on or off the monitor changing the brightness these would be functions or methods now the prerequisite for this video is that you've worked with one language at least that has the concept of classes the most popular ones being Python C++ Java so let's take a look at the first pillar of oo p which is abstraction abstraction means to only show the necessary details to the user of the object for instance say we go back to our computer monitor example when a user is turning the monitor on or off they don't really care about the inner mechanisms of what's going on they just want to push a button and see the screen turn on and that's really what abstraction is you're only exposing the necessary details needed by whomever is using the object and the best thing about abstraction is that it really decouples the user from the underlying implementation let's take a look at a coding example so over here we have an empty Java project so if we want to go ahead and create a new class and say we're building some type of video game and we want to have an object for enemies so let's go ahead and create something called enemy and right now it's completely empty but what can we what attributes can we give to an enemy you know maybe an enemy can have health and here let's just give them the ability to say something let's just have him say I am an enemy you better run so if we go back to our main class here let's create an enemy object and let's just go ahead and invoke that talk method so if we go up here we see that as a user we only care about calling this talk method we don't care about the underlying implementation here about you know I mean it is pretty simple in this case but either way we don't really care how this gets printed out for example we could have split this into two print statements but from a user point of view when we call talk it still prints out the exact same thing so this is the idea of abstraction is that we only care about calling the method we don't care about the underlying implementation the next pillar of olp is inheritance [Music] inheritance is a very powerful feature and it basically allows you to have code reusability inheritance is useful when you have an existing class and say you want to build a new class that uses the stuff from the previous class but you want to add additional features onto it classes which are derived from an existing class are either called subclass extended class or child class and the class from which that subclass is derived from can be called either the superclass the parent or the base class so right now we just have this enemy class but let's go ahead and create a new class here say anyone have an enemy that's like a vampire so what we want to do is we want to take that enemy class and we want to expand upon it with our vampire class so what we can do here in Java is just going to be extends enemy and the language here can vary from language to language but the idea is going to be the same so if we go back here we see that this class here is completely empty but if we go back to our main and we want to go ahead and create a vampire object we see that V dot talk exists so if we go ahead and call that we see that down here it prints up I am an enemy you better run and if we go back to this vampire class we see that this is completely empty but because the fact that it extends the enemy class it has the existing code here so we don't have to rewrite it so it reduces the amount of code that we have to write now obviously we don't want the vampire class to say I am an enemy you better run so what we can do is we can override the talk method and the way to do that is basically just by implementing its own talk method so we can have the vampire class say I want to suck your blood now we see that the vampire class inherits that talk method from enemy but the program is gonna say hey since this already has its own talk method I'm actually going to go ahead and use that so without changing anything in the main method if we go ahead and run that we see that it does in fact change - I want to suck your blood so that right there is the idea of inheritance all right so now that we're halfway through that leads us to the question of the day which is what is your favorite oriented programming language and why let me know in the comments down below so this leads us to the third pillar of object-oriented programming which is polymorphism now polymorphism is probably the most advanced topic out of these four but I'm gonna try and explain it to you guys in a way that makes sense so polymorphism comes from the word poly which means many and morph or morphism which means forms but I think that's kind of a confusing way to look at it basically it allows you to determine what kind of function to run while the program is running which will make a lot more sense when we look at the code so if we go back into our program here let's go ahead and create another class and let's create a werewolf object let's go ahead and have the werewolf class extend enemy and let's go ahead and override the talk method again and let's go ahead and have it say I don't know what werewolf say let's just have it say I'm gonna bite you so now if we go back to our main class and let's refactor this a little bit let's call this vampire so let's go ahead and invoke the talk method on both of our objects and we see that they do call their respective talk methods so at this point our hierarchy is we have an enemy as the parent class and we have werewolf and vampire which are both child classes so werewolf and vampire are sibling classes so now let's go ahead and comment this out so let's go ahead and create an enemy pointer and let's set it equal to our vampire object and now let's go ahead and call enemy dot talk so at this point what do you think is gonna happen is it going to call the talk method on the enemy class or on the vampire class what do you guys think let's go ahead and run that and we see that it calls the vampire class's talk method so an object-oriented programming you can call methods on a class if what's calling it is a pointer of the parent object as we just saw in this example here so let's go ahead and look at something a little bit more advanced so at this point we have an array of enemy and inside of that array we have our vampire object and our werewolf object now we want to invoke the talk methods on the vampire and werewolf objects so if we go ahead and run that we see again that it does call the respective talk methods and this is the power of polymorphism is that the talk method was figured out during runtime it looks at what kind of object it's pointing to it looks at the method on that and if it's overridden it'll call the child objects method the fourth pillar of olp is encapsulation encapsulation is built on the idea of data hiding this is where we restrict access of certain properties or methods of our object to whatever is calling that object so for example if we go back to our code here let's go ahead and delete everything up until our vampire object and as we see here we have this enemy class here let's go ahead and make this public meaning that we can access it from basically when we're calling it so when we have vampire dot health let's go ahead and initially set that to like 25 and then we can go ahead and print that out so if we print that out we see down here that it does print out 25 so we can directly access health from our vampire object now generally this isn't good practice you generally don't want whatever's calling your object to be able to change the properties within that object so what we could do is we can go to our enemy class and instead of making this public we can go ahead and make this private now if we go back to main we see that we get an error here saying that health has private access in enemy so this is mostly for safety basically you don't have something that inadvertently changes the property of an object but say for some reason you do need to change the health we can use what are called getter and setter functions for example we would go back down to our enemy class and we would implement these like this so right now we have two methods we have something called get health which simply prints out the health and we have another one called set health which takes in a parameter and sets the health to that parameter so if we go back here we can just say instead of setting the health directly which call dot set health 25 and we can delete this part here and if we want to print it out well we can simply call vampire dot get health and if we go ahead and run that we should get the same result and we do so that's really the idea of encapsulation is that we are encapsulating our properties within the object and we can do that by setting our properties to private so instead of just giving everyone access to our properties we can later on set getters and setters to give the user the control over what we want them to be able to set and get so those are the four pillars of object-oriented programming abstraction polymorphism inheritance and encapsulation I hope you guys found that video helpful like I mentioned it's really one of my favorite subjects in computer science I've even had interviews where the entire interview revolves around object-oriented programming and object-oriented design so it's definitely an important subject it's definitely something that I've used in the real world and I think that it's a concept that really every programmer or every software engineer should have a good grasp on but yeah make sure you guys smash the like button it definitely helps the video move up in the rankings and please subscribe to the channel if you're not subscribed already but other than that I hope you guys have an amazing day thank you guys so much for watching and I'll see you guys in the next video [Music]
Info
Channel: Keep On Coding
Views: 102,631
Rating: 4.9672747 out of 5
Keywords: object oriented programming, object oriented programming java, object oriented design, object oriented programming concepts, oop java, oop, object oriented programming python, object oriented programming interview questions
Id: 1ONhXmQuWP8
Channel Id: undefined
Length: 11min 22sec (682 seconds)
Published: Wed Feb 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.