#6.6 Java Tutorial | Encapsulation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Oh [Music] and this video we'll talk about encapsulation so we'll divide the video into two parts I mean not the videos but the topic well in the first part of the video we'll talk about what is encapsulation if the second part of the video will talk about why do we need it so in caption so basically means is you'll be having a class and let's say this class is student in this class so normally what happens is class will so object know something object does something right and we have done we have we have seen that from a long time so object knows something with the help of variables object does something with the help of methods so whenever you create any variable let's say we have roll number and we have name we have to make sure that those variable data so those variables need to only be changed or fetch with the help of methods so if you want to do anything with the variables you have to use methods okay example if you want to set the value of the roll number what we can normally do is we can create the object of student we can say student s1 equal to new student and we'll say s1 dot roll number is equal to 2 and we can say s1 dot name is equal to let's say Naveen so when you have when you are signing the value of s1 roll number two and I mean so we are trying to access that from main method so it is not right so the only way to access the roll number and name should be through methods so normally those methods are basically called as ghettos and setters know what what exactly it is so to set the value we have to use setters and to get the value we have to use gaiters example so let's say I want to set the value for roll number I will say public void since I want to set the valuable name I will use a method named as set roll number we have to make sure that is our is capital and will accept or roll number they will say roll number or we can also say our and now I can say a roll number is equal to R so we are assigning the value of R to roll number that is one way how to fetch the value of roll number so we say public now since we want to fetch will should return int and we say get roll number and now this will not accept any value right but this will return a value we'll do it it will return a roll number and now if you want to access that we have to say s1 dot set roll number by specifying two so we are not assigned the value directly right we assigning the value through a method that means the only way to use the variables is through a method and we can also fetch it now so let's say let me fetch the value I will say s1 dot get roll number so show up saying s1 dot roll number I'm saying s1 dot get roll number and if I'm this code you can say it will work so you got to so the only way to access the variables is through methods and since you accessing the data through methods we can also make this variables as private so you have to make sure that your variables are always private so variables should be private and the methods who will access those variables should be public that means you are binding your data within methods and whenever you bind your data with methods the concept is called as encapsulation so the concept of encapsulation is it is binding data with method there is binding data with methods so we have a data here and we have some methods here so the only way to access the variables is through methods but we have done that for the whole number guide we have not done that for name the thing is if I don't do this for names how to type it right but encapsulation is so famous you just you can simply ask your Eclipse to give it for you so you just try to right-click you have to say so so you have to click on generate getters and setters can see that and select both the variables and click on ok as we have seen in the Constructors so you can see it is giving you both the both the methods here public eight roll number and set roll number and get name and set name and we have also seen how to use this keyword right so you know about that so instead of assigning the value now so you can see we are getting error on this line because we have made name as private field right so if you see the if you see this it is giving you an error which is student dot name is not visible so what we can do here is we cannot use name now we can say it's dot set name and we can pass the name there so that's how it works if I run this code we got to because printing only the roll number let's also bring the name is a get name and if you want this code record roll number and name both so this is this is how you can use your getters and setters and this is encapsulation so that was the first part the second part we have talked about is why we need it the need for encapsulation is we have to make sure that your data is safe right so this data need to be saved see if I want to assign the value if you want to assign the value using to assign the value to rule number the only way is with the help of methods right we have to use set roll number now let's say if you make it if you make this variable as public anyone can access the variable right what we want is even if people is - people are trying to change the value I can simply print a log file right I will say I can simply maintain a log file we can say value of roll number roll number change I can maintain a log file because when you talk about several programming we always maintain a log file if you on this code now it is assigning a new value but it is also printing value of roll number change at least you can maintain a log file so you can do that methods right you cannot do that tightly with variables if I if I if I say s1 dot name equal to Naveen and if I have a public public variable there no I mean how can I meant analog file changing the values of the variable or fetching the value of the variable you can also maintain a log file that user has sympathy value we can simply say a user is accessing the value at this you can it will maintain log file for you you can see that it is saying that user is accessing the log accessing the value so you can maintain log file you organize one use second use is before fetching the value of roll number you can also check for the login of the user if user is on a log Indian system are going to do that in this line in this code you can write those statements so it's always better to access the variable with the help of methods because that's the only way you should be able to access your variables you have to make sure that your variables are private and then you need to have our gauges and setters who will access those variables so that's it that's about encapsulation that's it about this video
Info
Channel: Telusko
Views: 225,273
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, encapsulation, example, practical, implementation, what, why, important, need, object, class, method, variable, getter, setter, fetch, run, code, private, public, access, through
Id: 4VBdNbWeTZw
Channel Id: undefined
Length: 6min 21sec (381 seconds)
Published: Mon Feb 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.