Java Programming - OOP Practices

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi in this tutorial I'm going to be talking about object-oriented programming in Java before proceeding further I just wanna let you know that I have already created few projects and few other tutorials about basics of Java programming if you don't know OOP already I would recommend you to go and check this out Java programming salt programming problems Java programming and solve recurrent problems recurring problems are a bit difficult but you can check this out other than this I have already created tutorials on game development if you want to learn game development I just check that out in which I have used basic concepts of ooh alright other thing that I want to know that I've created a group on Facebook make sure you go and join this group for sharing knowledge about programming for getting more understanding of programming by sharing with different peoples around the world so what is poop poop is stands for object-oriented programming and that is a model that is to represent real-world objects and the relationships in programming it provides components to transform objects containing properties and functions almost every modern language like Python Ruby C++ C sharp and Java follow a model there are mainly four components of all first one is encapsulation second one is inheritance third one is abstraction and fourth one is polymorphism and I'll be talking about all these four components and I'll be practically showing you by doing code that how to use these four components when to use it what are the benefits of using these components in programming so do you think the other thing that I'll be covering in this tutorial first I'll be talking about what is class and inside a class I'll be talking about constructor different variations of constructor what is the purpose of constructor in our class and then I'll be talking about objects like we can have multiple objects multiple diffidence of one class and then I'm talking about access modifiers like public private protected and what are the usage of these excess modifiers I'll be talking about static keyword in variable as well as static keyword in function and then I'll be talking about method overloading method overriding and then I'll come to the inheritance polymorphism abstraction interfaces and capsulation and lastly I'll be building classes what are the benefits and what are the usage of these components in object-oriented programming so make sure you go and join that group and if you don't already what about anything about OOP then I would recommend you to go and check out the basic tutorial that I have already created I hope that you're going to like this tutorial and follow me on twitter as well I've given the link in the description below so now let's start working on class first so I have at least open with an empty project and the name of this package is o and I have this main class inside this package and inside this class I have an obvious main method that has to be in every project of Java all right so now I'm going to create a class but before that let's discuss about what is class what is the usage and benefit of creating the class if I talk about the real-world examples you can take a class as a person a student a mobile phone a bottle a car or an aeroplane a bike all these things are models so this can also be called as class if we divide all these models into properties and the functions then we can say that a car has a color it it's his property a car has four wheels it's the property a car has a model a car has a name a car has a manufacturer so these are the properties so now these are the properties what are the functions of that car functions can be defined as a get speed of the car this is the function car move on the road so this is a function of the car if we talk about the function of a student then student function would be a get name student study study is a function and the property of student can be roll number can be phone number can be his name can be his degree these are the properties so we see that all these classes have properties and functions if you want to create multiple students from the university database then we don't want to create each student and give the properties to individual student again and again what we are going to do is we are going to create a class and give it its default properties and functions that these are the properties of each student whenever we need to create any student object we will ask that class to construct a student for us and that class will construct a student off for us based on the properties and the function that we have given to that class but now right now I'm going to create a new class called wickles because i want to work with wiggles so let's go and create a new class i am going to give it a name ricky'll and i'm going to create it so you can see that i have this package ooop all the classes and in the one project must have inside them same package name all right so I have this class vehicle and I'm going to use this class from this main method or this is going to be our tester all right this is where our application starts so what are the different properties of the wickles if we think about we go has a name so for name right now I'm going to give it a public access modifier because I I will talk about later about an capsulation that what is encapsulation so that we may change this public access modifier into encapsulated version of variables so let's keep it public right now all right so I'm gonna give it public string name and I'm going to give it an empty string for now because I want to give it a name inside my constructor I'm going to talk about what is constructor in a bit so now let's create another property a pickle has a color again string type so color all right and one or two more I want to give it to us so this is the model all right and last one is actually it's not blue let's change it to e all right so the last one is it has arm manufacturer right let's name it a company all right now I have given this class its properties there can be more properties but for now let's stick with these for property these properties are common in all vehicles whether it's a bike whether it's a plane whether it's a car truck so these are the properties of wickles so now what if I want to create this vehicle I can create ad with the help of a new keyword all right so what if let's say we call one equals two new and I'm gonna use new keyword and I'm gonna write the class name and I'm gonna give it an empty braces all right so this is how I have created a new wickel with the name we call one I can give it any name this is just like a primitive type variable like we write int a int B our double BB and we can also write int age and count so name should be understandable okay so I have given it a name we can 1 so this is the object what if I want to create multiple vehicles with that class I can do that again and again like this I don't need to create all these properties or individual vehicles so that's why I've created this class but right now I can't do anything with it I can't get the color of it I can set the color of it I can't run this car on the road now let's modify it further so let's see actually what is I want to just let you know that what is this one it's giving me an error because all those names are saying it should be different and also right now let's remove these things it's going to call a constructor in Java if we do not define a constructor inside a class it's already gonna create a default constructor I'm going to show you that what is the how to write constructor in Java so this is an empty constructor this was the code that was actually created by the compiler of Java for us if we didn't create it it already created it alright so it has created a constructor and constructed a new object on vehicle class or with those properties and it has stored the reference of this object inside this variable and this variable is of data type we come alright so now I have created an empty constructor I want to give it some values because each week I'll have different values it will have different color different model different company so I want to set I want to ask this class that these are the properties you need to set for this particular vehicle alright so I have created an empty constructor let let's leave it an empty constructor I'm gonna create a new constructor with parameters if you check if you see that the name of constructor should be same as the name of class if it did if it is isn't the same then it becomes a function alright a constructor doesn't have a return type alright so these are the things that need to be considered for writing constructor the name is same and it has no return type all right it's giving the error because I have created two empty constructors but I need to give it a name so name I want to give it color and I want to give it model I want to give it company all right so I want to call this class and I want this class to construct a wickel with these parameters that I'm gonna send with this tester all right but let's store these values that I'll be sending and adding up these values in these properties all right so I'm going to write this dot name equals to name all right the name of these variables can be different but I have written the name a same the color same as this one the model as this one because I want you to show you what is this keyword this refers to this particular class it's mean that go to that class and find out this variable inside it and store the value that we have sent as a parameter to this constructor inside the property of this class I hope that you've got the idea now let's fetch the property with named color and store that property with the value color that we have sent with the parameter all right so I'm gonna give it model model this dot company equals two company all right so we have stored all the values that we have sent as a parameter to these properties all right now I'm going to create a new wiggle first one is the empty vehicle I haven't given it any values because once it was constructed with this empty constructor it all contained empty strings all right so to show you that I just want to print out system dot print line and I just want to use this Wickle 1 dot and I just want to print out color all right if I run this you can see that it hasn't printed anything it has actually printed an empty string because color is empty I haven't give it an empty any value all right now let's create a new wickel all right and I'm going to name it vehicle too but I'm going to call give it a parameters it has to be same in a same sequence as these so first I need to give it a name color model and then the company so I'm going to give it a name and the name has to be let's say it's old our Civic all right and the color is black and the next parameter is model and company let's change it to Civic and now I need to give it a model 2012 model and a company is Honda all right now I'm going to use this variable because this variable is being constructed with the help of these properties all right now if I use this variable and fetch the color it's going to print black all right you can see that had printed black because I constructed this object from this class and I gave this parameter to this constructor it has constructed a new vehicle with all these properties that I have given it right here all right and I can set all those values with the help of this variable because this variable is having the reference to this object all right and I can fetch any of those or variables all right so now I'm going to create functions let's create a function public void set name alright and I'm going to give it a name and this dot name equals to name you can see that I have created a new function it has a different name as compared to this class so it becomes a function it's not a constructor this was a constructor because name is similar to the name of glass alright so now I'm going to give it you can see that I created an empty wickel but once a wickel is created later on if i want to change the any value of its property i can change it with the help of functions if i want to get specific value of any wickel I can get that value now you can see that it has got nothing because it was empty all right let me add a name and if I run it again you can see that it is empty again all right now I want this name to have some value for this particular vehicle that has a name we call one I can assign it a value with the help of this function so let's give it a value all right so I'm going to use this function and I'm going to use set name and give it a value so let's give it a name city all right and now if I print the name of vehicle one it's going to print the name so if I run it you can see that just printed city earlier it created this becomes one with the help of empty constructor so it wasn't creating anything it used the default values that was here if we write any default value in here that would be there or rather than empty string but later on I want to change any value of specific property I can change that with the help of that function so this is how we can create multiple functions for individual properties we can set all the properties with the set and the name of that a property so I'm going to copy it and paste it three more times I'm going to change the name of the set color alright and the third one is model model make sure you follow the naming conventions it has to be a camelcase naming convention all right so I'm going to change it company to company all right but for the color I need to change the color property and give it this value alright for the model I need to give it the model value for the company I need to give it the company value right so I have set all the four functions for the properties it's always a good practice to write that much functions for set the properties that I have defined right here all right so now I got the color but if I set the color right here but later on if I want to change its core I want to change the color of this Civic all right I can change that with we go to dot color no I can use this function set color let's go and see so this is a function all right and I'm going to give it a green all right now if I'm going to print this value you can see that the same statement is printing green earlier it was printing black so we have seen that we can change any of the properties that we want whether it's been constructed with an empty constructor right here or it's been constructed with this constructor with having a parameters we can define multiple constructors as we want a with different sequence of parameters with one parameter two parameter three parameter and so on all right so this is called constructor overloading that we have defined a constructor with the same name but it has different parameters since same parameters but different sequence of parameters so I have set third functions now one thing more that I need to get named as well I need to get color as well I need to get model as well I need to get company as well so while getting something I don't want to give it anything because I want to get the previous set value of specific properties but the while getting the value I need to set the return type if you know already what are functions then you must know that we need to set the return type if you are we are getting something all right so I need to set return all right need to change its return type let's first change the return type of all these four functions all right and I'm going to remove this part of gold and I'm going to add return before each of these all right so now one thing I mentioned that you can see that I have added this public access modifier it means that we can use these functions outside this class like this main matter is outside this vehicle class so we are using these functions right here if that were protected even then we were able to use these functions but it has to be inside the same package if we have said the excess modifier to protect it and it's not in the same package this both of glasses then we couldn't use those functions are right here in this class in this main method all right but I'm going to just want to show you that what is the purpose of private functions but before that I just wanna let you know what is encapsulation right here make sure you are doing it yourself as well with me I just want to let you know that let's check out these functions first rather than directly accessing the properties I'm going to use these functions to fetch these properties all right it's not a very good practice to directly access these properties right here we need to add a function inside a class that I have just added in order to get any specific values for what I'm gonna do is rather than dot name this is the property I'm going to use this function and this is going to return with that name for this particular object alright so I'm going to write get name alright and copy it and I'm going to paste it right here so if I run it you can see that has been it printed the same result it has used that function it actually recommended practice for the security reasons so let's right here get color and get color as well all right these are the functions I added there if I run it again you can see that it has printed the same result now we are still able to access these variables we want to restrict user that you are not allowed to use these variables if we are about to use these properties directly without any functions we need to get an error so for that I'm going to change this public access modifier to private all right now if I go and run it you can see that it has printed the same result now if I remove it let's press ctrl Z to get it back you can see that it is giving me an error it's showing me that this field recalled odd color is non visible alright because earlier it was visible because this property was public alright and now I have changed to private I don't want to fetch this property directly from this object alright so I want to get this with this function alright so this is how we can restrict any specific access to any property of the class we want to add a function for setting any value to any property or getting that particular value inside that property with the help of getters and the centers all right so I'm gonna just press con law get color and get color all right let's run it again you can see that I have run this again all right so now what is the purpose of private function let's say coding example for that I need to add few more property let's add a private and I'm gonna give it us I want to find out the speed and forgetting the speed I'm setting out the criteria of engine that it's a thousand cc engine it's a 2000 cc engine it might not be a good idea but for just just for letting you know that what is the purpose of private function inside a class that would be a good example so I'm going to write string and I'm going to give it engine and AD start I can give it a default value or let's give it a default value it let's say it's 800 all right so an empty function would click empty constructor would create all these properties including the engine value 800 because it has already set to 800 now change this constructor string engine all right and give it to the engine equals to engine all right we also need to create the setter and getter for engine so set engine engine I'm going to change its name right here as well and for the Gator as well engine get engine so you can see that I have added few more gold lines of gold right here but I want to get a speed alright let's add a public function and I'm gonna return typewriter return type paint and get speed all right and for getting the speed I'm gonna use this engine value with the speed all right so now our let's change it to private because I don't want this function to be accessible outside this class all right and I'm going to call this get ancient function you all right so I have set this primitive type with the string and then this is the end from outside this class I'm going to call this function alright and this is going to call this function all right this function is only for utility purpose it's not going to be used outside this class and so the purpose of writing a private function is that we don't want that particular function to be used for applying or making change to any property of that glass all right in fact it should be doing any specifically logic many complex mathematical logic that doesn't need to do with that function that doesn't need to be called from that tester class all right if we are having a function that has bunch of lines hundreds and to hundreds of line it it's always a good practice to break down that function into multiple functions so the multiple functions should be private only that function should be public that has to be called outside that class that has to be tested all right so just this is your fourth illustration so I'm gonna check f a equals to 800 then I'm gonna return speed of 90 you all right and return 120 all right so let's go and I'm going to give it a value 800 alright was giving me an error because there were five parameters in that constructor I was sending four parameters so I need to add another parameter to that constructor now what if I want to I get the speed of this vehicle now I have added this property to the constructor and now I want to check out the speed with respect to the engine all right now let's go a print line wickel to dot get speed all right and if I run it you can see that it has returned me 19 so what it has done I have called this speed function alright and it called this get engine function from inside this function alright I'm not using this function from this tester class because I don't want to use it all right and it is using this function from here and inside it it's checking the value that it will return if it is equal to 800 then return 90 all right if it's not 800 then return 120 all right we can check out multiple conditional statements right here but Jesus just for illustration purpose I have just added a an else now or if I change it to 900 it's going to print 120 all right let's run it you can see that it has printed 120 because the return value was not 800 it was actually 900 all right so I have used this function from inside so anything that we need to use from inside a specific function that has to be private all right so now Allah a last thing for this class a tank up is that how to use static keywords for the variables right now you see that individual properties have its own values for individual variables what if I want to keep all these wickles have same value if I change any particular property the value of any particular property with the help of vehicle one it should also reflect that change in vehicle - if I change any property value with the help of we can do like we go to the odd set color green it should also change the color to the vehicle one all right so it has some benefits so now let's go and I'm gonna write public they take eight count equals to zero all right and inside the empty constructor I'm gonna add count plus plus alright manometer an empty object is created empty because it's created count has to be incremented alright and inside it as well I want to add count plus plus alright as I have set it to public and I have said it to static all right now let's check out and as know that one can directly access this variable because it's public I have created this object I just want to print the value of count from here all right so I'm going to use this wickel one dot count all right if I run it you can see that it has printed one all right now let's copy it and paste it right here all right let's run it again you can see that it has printed - all right so what is snowing I have created this we call one with the help of this constructor it went to this empty constructor it incremented count 1 so it becomes 1 so this is very understandable that it has printed one after that I have graded a new object with the help of second constructor all right and I have stored the reference of that stacking constructor inside this object all right so it again count it count plus one all right but you can see that I have printed out we kill one calm I haven't printed out we can't do that count all right I constructed of equal to but first we can one printed count equal to one and then after that we can one printed count to instead it should have printed beagle 2 equals to two where it has printed to four Huichol 1 so the thing that we have got the idea we got from it is that if we are changing value with the help of we can do it's also going to reflect that particular value for specific property for all other objects that was created with a new keyword all right so I hope that you've got the idea of the purpose of using static keywords is not to actually children be a part of specific object but it is beneficial to keep track of how many because we have constructed in our factory how many employees we have hired in a company how many Mobile's we have manufactured in our company so the other thing this this is a thing that is very helpful for the company that is actually constructing or developing that specific product it's not going to affect that individual object that we are constructing with the help of constructor alright so the another thing I want to show you is the static function so what is the static function right now we have created these functions setters getters but I haven't used any static function all right so what is the purpose of static function all right let's create public static and get count all right and I'm gonna return it this dot count actually uh it's not gonna it's showing an error because static function should be inside should be called from a static function and if I use to call this function from here it should be static as well and I'm returning a static property from here and the reason it's showing an error because it is it's not recognizing that if this variable belongs to any of these objects it is equal to all the objects it doesn't belong to any specific object because its value is same for all the objects and this refers to that particular object this refers to a particular scope that we are in so it shouldn't be used right here so static keyword of let's add get Wickle all right and I'm gonna return pew values all right so I'm going to get name cat color so I'm gonna write a strength that have you have set the name and color of that wickel alright and I'm gonna give it a string as a return type because this is the string all right so now if I want to call this function is because it's a static how can I call this function I don't need to use these variables these objects I can directly use wiggle dot get wiggle all right I'm directly using the class name to call this function it again it doesn't belong to the construction of an object it doesn't depends upon that if we want to call any specific object and a specific function without constructing an object for that class I can just add this static keyword before the return type of that function all right so if I run it you can see that it hasn't printed anything because I need to write it inside it alright so you can see that it has printed this strength so you have got the idea that I didn't use these variables I directly use these classname to fetch the return value it can it can contain multiple lines of code but just for illustration I have just returned this string so I hope that you've got the idea that how to use static keyword inside a class how to use public private functions inside a class what are gathers water setters what are the different very variations of constructors with the parameters without parameter what is the static keyword of using with the variables and what are the private functions private properties to enable the encapsulation to enhance the security of our application so next we will talk about inheritance so let's do that so now let's talk about inheritance inheritance is very important in object-oriented program if we talk about inheritance in real world then we see that every child must inherit some properties of explain it like we might have inherited the properties like hair color eye color height weight from our parents so I have defined this class vehicle and I have defined some properties and the functions for manipulating these properties I have said this class as a parent class of car bike and other automobiles and I have defined these properties that are common to all the wickles dad are common because main a bike has a name God has a name bike has a color or has a color bike as a model so these are the things in the child classes all these properties will be inherited by the child classes all right and the child classes will have their own properties as well which are different from other child classes like a of God has a speeding bite doesn't have a speeding or the God has the unique property so that's why I haven't added a stating in the parent graph because these are specific for the car but not for the bike so now let's create a class called car all right and right now I need to inherit all the public properties from this class all right right now these are the private it has to be private because I want to fetch these properties with the help of public functions all right so now let's go and in order to inherit the properties of the parent class I created this car as a subclass and I can use extends keyword and then I can write the name of parent class now it has inherited all the public properties of parent class all right and it must have a constructor but before that let me have its own properties that are unique but not in the parent class so private boolean and I'm gonna name it power-steering all right let's add a default value false a default value is that it has doesn't have a power spinning another thing I want to add is boolean add LED screen true all right there can be more properties but just for illustration I just added two unique properties that are different from other child classes at the parent classes as well now let's add a constructor for it so I'm going to write public car and I need to write all these properties from the parent class all right because whenever I want to construct an object of the car I need to send these parameters as well all right plus those parameters which are included in this particular class of boolean power-steering pulling in let me check out the spell all right so now I have created a constructor all right but I don't need to add this kind of code right here in this constructor for instantiating the values for the properties that are written in the parent class what I can do is I can use a keyword called super and then I can send these properties these values to the parent class order company engine all right so I have sent the parent class and now in this constructor I also need to add the values of these parameters into the properties of this class so this dot power-steering equals to power steering alright this dot LED screen equals to LED screen I make sure I they explained you earlier that this refers to this particular class so this dot any property means that the variable that I have defined inside this class and the variable that I've defined on the right side of this equal sign is the variable that I have sent as a parameter right here again it can be another name but I've just added the same name as the property name because I wanted to show you how to use this keyword alright so I'm going to copy those functions getters and setters so it doesn't need to add getters and setters but I'm just adding it just to not do with any problem later on for getting or setting the values all right so set name I'm going to write set overstraining and I'm gonna add a variable right here all right and I'm going to change its name okay an LED screen let's change these things and rather than set color I'm going to write set LED screen alright it's giving error because that type mismatch because this is the string so I need to change it to boolean because the properties are the boolean type all right so I'm done with it let's copy it and convert it into gathers all right and for the gathers it has a return type and it doesn't have any parameter all right because while getting something we don't want to send anything we just want to fetch the previous value that was already stored in this property all right so I'm going to remove it but remove this as well and add a return keyword before that all right so I've added the getters and setters all right now let's go and create an object of this class all right so I'm going to comment this gold goes up I want my output to be look a bit Oh oh dear all right so I have commented it now let's go and create an object of the car class all right and inside the car glass I need to send all these properties plus the first one was the power-steering so I'm going to send it to true an LED screen has to be false all right so I have created the object of this guard class and store the result store the address of that object in this variable so with the end of this variable I can access to all the public functions and the public our properties if I have added okay so print line I'm going to God one dot you can see that it is giving me those functions which are not in the child class but in the parent class so because I have extended the parent class in the car class while showing the gate color get company get model get speed and it's also showing me the all those functions that are in the child class which is our guard class so I'm gonna edge LED screen all right so if I run it you can see that helped bring pitfalls because for the LED screen I sent false all right now let's copy this line and I want to fetch the name of this car actually I needed to let's go oh well I mistakenly didn't set this to get I added the set LED screen so it writes the result actually it should be get right here alright so I'm going to change it get now this function would be giving an error that said LED screen doesn't exist I'm going to change it get right here as well so now with the help of the object of child class let's fetch the property value of the parent class all right because a child class then use the properties of experience okay so let's fetch a name okay so get name alright and now let's run it and let's see doubt where you can see that it has returned me that this car has a name Civic alright because I set the name Civic right here okay and it sent out all these property values to spirit all right so I hope that you've got the idea how to work with child classes there are a few more things that are needed to be discussed right here inheritance what if I want to over write any specific function in the child class but like I you can see that I have set the name all right I'm getting the name of specific week a specific God or specific bike but I want to add a string before this name like the name of your vehicle is this alright but I don't want to change this gold right here I'm going to overwrite this old function inside the child class and whenever I will call this function it's going to call it child class function because giant class function have a higher precedence all right that will be called first and if the child class have doesn't have this specific function then the parent function will be called so I'm gonna copy this function alright and I will come right here and paste it all right so it's it's showing me that you doesn't have this specific variable all right so now what I need to do is let me add a string name of your vehicle is and in order to concatenate this string with a specific property as you see that is showing an error if I write this dot name so I need to call the public function of the parent class so it's going to return me this name and I will then concatenate the value of this name in the child class so I will call super functions super dot get name all right same as we call the super keyword for calling the parent constructor and I'm using the super keyword for calling the parent function right here in java is called method okay so now if I run it you can see that it has called this function alright although this function were also existing in the parent class but it didn't call the parent function so I have overwrite all the functionality of the parent class in the child class so this is called method overriding in object-oriented programming it actually exists in inheritance in object programming and it is also very important for working in polymorphism which I will be talking about after discussing or inheritance and abstract classes and interfaces all right so I hope that you have got the idea of all the inheritance work I'll talk about further about inheritance later on for now make sure you practice yourself and create different classes of different models of these things are so attach it for inheritance next let's talk about polymorphism by 1/4 extending these things ok that's for well before talking about polymorphism I just wanna let you know that there can be multiple ways we can in one class another class and we can inherit car as well that is called multiple inheritance like car is inheriting or wickel and some other class is inheriting class and some other third class is inheriting that second class so this is called the multiple levels of inheritance there are diamond inheritance there are the multi-level inheritance like more than one class are inheriting one parent class like I just I'm just going to do it for illustrating the polymorphism all right but that is not the core concept of object-oriented programming this is called the design patterns of object-oriented programming or the design and analysis of object-oriented programming I have been to create the tutorials on those two things or one is the design patterns and other thing is the object oriented analysis and design so for now just leave it and let's talk about polymorphism from now you can see that I have already created two classes I need to create another class bike that will again be inheriting this little class the two classes will be the child of one parent class I'm gonna right click on it and create a new class and I'm going to name it Mike alright again I am going to use extends keyword and set the parent of this by class to the wickham pit so now again let's add a property which is not in the parent class it can be same as the property of car because they these are the parallel classes both classes are siblings but not the parent-child class of one another right so there can be same things right here so now let's add private boolean or' what it should be let's add F for stroke it's true all right I for now just add a one property right here it shouldn't start with a digit let's add right here okay now let's add a constructor bike inside a constructor again we're going to add these properties from here up to here all right and the last one should be four-stroke all right again I'm going to use uber keyword or sending these values to the parent class all right and using this keyword I'm going to write four-stroke and set the value of this property to the parameter that we have seen in this constructor and for the setter and getter and let's go and copy this code list remove it so set for stroke alright and for getting it let's just change it to four stroke and get four stroke alright that is the boolean variable I'm checking if it's opposed or not if it's true it's four strokes if it's false it's not for stroke now I need to add one more function in all three classes to illustrate to you what is polymorphism okay polymorphism means one object has different forms like a car is a car as well and a car is a vehicle as well a bike is a bike as well we can call it a bike but we can combine all these wickles in a category of wickel alright we can let's like we talk about electronics we can talk about refrigerator we can talk about washing machine washing machine can be called as a washing machine as well but all these things can be called as machines ok refrigerator is a machine watch is a machine computer is a machine and we can further categorize things into subcategorized but overall we can combine all these things into a large main category alright so what I'm going to do is just for illustrate you I'm going to add one more function in all these three classes and that is T let's add public and I'm gonna add get info alright and I'm going to return a string this is our wickel and i'm going to copy this method and i'm going to paste this mastered in this class and i'm going to change it a car copy this code and paste it right here as well right and i'm going to change it to bike alright the definition of this function is same it has no parameter and it has a return type and all these three classes have the same definition and the structure of this method except this drink all right this is a bike this is the car and this is a wicked all right now the concept of polymorphism is that the object the variable of vehicle Clause can take the reference of the objects or its child classes but not the opposite of it what do I mean by it let's first create wickel one I'm going to copy and comment this code we can one and I'm going to create new wiggle I'm just going to copy this line and paste it right here alright why I'm created Jason likkle one all right I'm not going to fetch any specific property value but I'm just going to use that in get info function so let's add system dot out dot print line we can 1 dot get info all right let's run it all right so it has printed me the string all right it's not because that I have set the variable as a name very vacant one all right it is not also because that this is the variable of type vehicle but it depends upon that what kind of object it is reffering it is referring to the object of this class all right this class when we are constructing the object of this class it is storing and setting up the space in the RAM and it is it has an address and the address of that space is being referred by this variable so whenever I call this method with the help of this variable it's gonna refer to that space in the RAM get info and it's gonna rain their strength now I'm going to use the same class and the same variable to refer to the objects of its child classes so I'm going to copy this code all right and I'm going to change the name of it we could do but I'm going to create an object of child class rather than the parent class all right so I'm going to call God all right and the card constructor must have two more parameters so I'm going to write true/false all right so now this Michael - which is the parent the parent is containing the object of the child class we'll talk about real world it's obvious that a parents give birth to try okay so parents can contain the reference to the child objects okay so now let's use we could do and get info all right so now if I run it let's check out the output you can see that I'm using the variable of type vehicle but it is printing this is a car because this parent variable is referring to the object of child class and this variable is referring to the object of itself okay so this is how the polymorphism works the parent class variable can refer to the child clock variable and it shouldn't be the first child it can be our grandchild great grandchild multiple level inheritance can work with it all right and we have also done the method overriding because the parent have getting four children have getting four so we have overridden those methods in the child classes all right when I would call getting four it's not going to call the parent class function it's going to call the child class function now let's check off the bike one all right so I'm going to just copy a now let's add bike we code three all right and the last thing is for stroke it's a four stroke bike okay I'm going to use the same function but with the variable we call three this variable is referring to the object of the bike class all right now if I run it you can see that it has printed this is a bike because this variable was referring to the child class which is the bike so this is how we can work with polymorphism it is very important in real life and in programming it depends upon the requirement that how we should design our classes how we should design the relationship in our classes this is another course object oriented analysis and design I'm going to create that course for you people that how to think of where to add that particular class rich class would be the parent of which class there should be a multiple inheritance or multi-level inheritance or diamond inheritance and so on alright so for now you just need to get the idea that how polymorphism work and what is the polymorphism in one line polymorphism means that an object have different forms we can categorize different from the objects into one form and multiple form as well in this case a car is a car this is one form bike is a bike this is one form but a car is a wickel as well and the bike is a vehicle as well so we have categorized car and bike into a category of vehicle and we're storing the object of car and bike in a variable called we call that we've got the idea of polymorphism next we will talk about abstract classes and interfaces which are also very important for unspeaking huge effect glasses so the thing is that I have taken a shaped glass with the shaped glass I have this sheet constructor and a function a draw function and inside a drop off that I have set shape drawn inside rectangle Abed rectangle drawn inside circle acts that circle drawn all right these are two glasses are extending the parent glass which is the shape and overriding the function draw inside it let's just check this out with walls then we will convert this structure into a flight glove I'm going to convert this shape laughing website clock and see what is the difference between tack your glass and abstract class or first let's check out but I'm going to create shape shape one equals two new and I'm going to create an object off circle all right and with shape one I'm going to call draw actually I need to add it inside system dot out dot print line to check this out if the spring is actually printed Arnaud giving an error-free Moondog you mad alright it's giving you an error because it's expecting this function to return somewhere so that that value may be printed inside the system dot out dot print line all right but if you go to circle it's not returning anything it's just printing it out oh I'll need system dot out of midline I miss to that ok on let's run it I can see that circle is drawn because the shape one object this variable was referring to the object of circle or is it working fine now let's convert this shape large if you act like love so abstract class we need to add abstract keyword before the class keywords now this class has become the abstract class whenever the any class becomes abstract we need to add an abstract function inside it what is abstract function abstract function contains nothing inside it its body it's empty we don't need to add anything whenever we add an abstract class dad class must have at least one aspect method and that abstract method must be implemented in the child lock means a child class must override the parent unimplemented abstract method that is hinder after class so now I'm gonna just remove this out all right and I'm gonna add abstract before it alright it will give me a router because this class website and there is no abstract function inside it I haven't added anything int edit body because abstract function doesn't contain anything now let's come to the rectangle class that we call rectangle classes are extending those parent class right now I am implementing the parent class function which is draw and printing this out alright let's go and run this you can see that circle drawn is printed now let's remove it it has two focal clip now if I remove this function it's gonna give me an error because this class is expanding the glass shape and the shape class have abstract function and whatever the function is abstract that must be implemented in the child class right now in the child class if I have implemented draw function in the rectangle class I want implemented draw function for the benefit of abstract class is that whenever we are working in a team and we want what colleague we implement that particular function in the child classes that has to be their right to write a guide cloth all right we write a parent class that has to be abstract and write down all the non abstract functions as well as the abstract function and give that gas to our colleague and then colleague a really extended glass and we have to override all those functions that are abstract in the parent class so that can be helpful for our colleagues to know that word functions who really needs to be implemented really these can be overridden in the child classes and one of the function that are not actually required for implementing or overriding in the child classes because they are non abstract so the abstract class can have at least one abstract function it must have at least one abstract function can have more and it can also have properties you can also have constructor it can also have non abstract function that only belongs to this class and can only can also contain non abstract functions of this walls third shape abstract class now let's talk about interface before the one interface let's discuss some problem right now I have extended this class in Java we can't extend more than one classes that has existing single class or abstract class we got extent shape or we add up gamma and add another parent class each our children can't have one children can't have two minutes all right one children can have only one minute or multiple children's can have one parent as well so the interface comes in handy - one class can implement multiple interfaces it can be five ten fifteen and another interface can implement can explain other interfaces as well so right now I am going to convert this class into interface interface all right and now it's giving me an error because interphase can't have constructor constructor should only be off after class or a simple class all right and for the interface I don't need to actually write abstract keyword before a function all right but this is the interface interface can't have property interface can't have constructor interface card have non-abstract functions right now this function is abstract and god i haven't added any body inside it and I don't need to tell it explicitly that this is the exact model it on my nail convert this function into effect method it can't have known after it functions as well so interface is fully abstract interface that can't have any other properties rather than this abstract functions and all the functions that we add inside interface must be abstract all that no function should have a body inside it and you can see that these classes are giving me errors that is it is not but there are different ways to implement the shape interface for the circle and the rectangle and to do that we need to add a keyword implements all right right here and right here as well all right so you can see that we have implemented that draw function that works in the interface inside the circle inside the rectangle as well all right so there is a lot of benefit of interface that we can extend more than one interface as well but we could extend more than one class in the other class every well so but we need to add implement right here so we have implemented that interface if we had another expect function we also needed to add and implement that function including the body of that function in the child classic all right now if I run this you can see that the circle is drawn again in abstract classes in interface in the polymorphism is working the variable of parent interface whether it's a interface or a class is referring to the object of child class all right interface can implement other interfaces as well but it is not using the keyword implements if this is interface and this is interface then here needs to be accents all right interface not extend can't implement other interface but interview can extend other interfaces as well all right so the benefit of that if you want to actually implement all the functions that we have with any known interfaces we arrived at the face given to our colleague and they need to implement all those address functions that are written by me in that interface so this is the benefit if you are working on life for the X Games I'm Dean if you're working on lon it should practice write an interface for that we structure everything at a proper place but it doesn't require because individual person already know that what he needs to do all right again it's a dog of the object oriented analysis and design that I'll be creating later on so I hope that you have got the idea of core object oriented programming except that I told you that I'll be talking about those things I think that I have covered all those things that you have got the idea how to work with inheritance how to work before you more freedom static keyword public private protected access modifiers I have explained you the child classes parent classes different levels of inheritance I have told you about the interface with abstract classes there are a bunch of other things but this is that is not the core object-oriented programming concepts if you liked this video make sure you click on thumbs up button and comment below if you have any question and make sure you join the Facebook group I have given the link in description below and follow me on Twitter as well thanks for watching
Info
Channel: Awais Mirza
Views: 253,989
Rating: undefined out of 5
Keywords: java, programming, java oop, oop, object oriented programming, java tutorial, learn java, learn programming, class, object, inheritance, polymorphism, constructor, programming tutorial, java se, java ee, oracle java
Id: T60YnN_Zmn8
Channel Id: undefined
Length: 82min 21sec (4941 seconds)
Published: Sun Jan 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.