‫Welcome back.
‫In this video, we're going to learn about encapsulation in Java.
‫Imagine you have a special toy box that you can only open and play with.
‫You can put your favorite toys inside the box and take them out whenever you want to play with them.
‫But your friends cannot open and the box or take out any toy because it's locked and only you know the
‫secret code to unlock it.
‫This way your toys are safe and protected from others and only you can access and play with them.
‫In Java, encapsulation is like your special toy box.
‫It's a way to protect the data, the variables and actions, the methods of class from being accessed
‫and modified directly by other classes.
‫It helps keep your code organized and secure, just like how your toys are safe in your special box.
‫Let's see an example in Java.
‫In the previous video we learned about access modifiers.
‫So when we tried to access year, which is a private in my class or the test, we have an error because
‫it's a private but how to modify the year, how we can create a way to modify this year not directly
‫through methods.
‫So in order to interact with these variables or these private variables, if I change it to private,
‫we have defined public methods called getters and setters.
‫These methods are like the secret codes to access and modify the private data.
‫So, for example, let me show you how to create the getters and setters.
‫Let's start with the getters.
‫We use public methods, getters and setters to interact with the private fields or the states.
‫So so to create the getter, we will follow the syntax with public as an access modifier.
‫If I'm I am deciding to create a getter for year here, which is an int, I need to make the returning
‫type of this public method as an int.
‫So here get year by default we can name the getters at get and here we put the name of the variable,
‫then return year.
‫This is the returning statements for the year.
‫So this is the getter method we created using the public access modifier and which is referred to the
‫type of the year and the get year, which is the name of the method and returns the value of year.
‫How to create the setter.
‫Public
‫void set year.
‫And this method will take parameter in order to modify the year.
‫So it took in the same type data type of the.
‫Valuable year.
‫Here New Year.
‫Now, I need to set this year equal to the new year, which is the past parameter in this method.
‫Again, guys, since the year is a private field, a private variable, we can't access this variable
‫directly from the main class or my class or test or other classes.
‫We need helper methods to access it like getters and setters.
‫The getters are used to retrieve the year, while the setters are used to modify the year of this car.
‫Similarly, I'll do with the speed, but thanks for Android studio to make it more and more easy.
‫If I press alt plus insert, I can access this menu if I need to generate constructor, getter setter
‫or getter and setter.
‫Both, I can click on them.
‫Click okay.
‫And here is the magic.
‫This is the getter for the speed and this is the setter for the speed.
‫So in this way we can create the getters and setters for the private fields in the car to access the
‫private key fields in any package or any class.
‫We can go use the object name and you can see now I can set the year for the yellow car and set the
‫speed for the yellow car.