The course is part of this learning path
In this course, we'll learn about object-oriented programming and its implementations.
- Method Overriding
- Super and This Keywords
- Final Keyword
- Abstract Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we will learn the encapsulation in Java. Encapsulation is one of the fundamental principles in object-oriented programming. It's used to restrict the visibility of the class members to others. In our example, we have Vehicle class, and we define the model and maxSpeed variables as private modifiers. If you define a variable or method with a private modifier, you can access these components from the current class only. So, the only way to interact with class properties is through the class's public methods. We define the public setter and getter methods of the maxSpeed variable, so it can be accessed from outside the class. The setter method is used to set the value and the getter method is used to read the value.
But we have no public setter and getter methods for the model variable, so it can't be accessed from outside the class. Let's make some examples to understand encapsulation. In the ObjectOriented programming project, right-click on the 'Source' folder and select 'New Class'. Specify the Package name as encapsulation, and the class name as Vehicle. Vehicle class is a super class. Let's declare two instance private variables named model with String type and maxSpeed with Int type in Vehicle class. Private means that these variables are visible to the current class only. Let's declare a public constructor named Vehicle, and it takes two parameters: model and maxSpeed. In the constructor method, we assign the parameter variable model to this class's variable model, and we assign the parameter variable maxSpeed to the variable maxSpeed of this class.
We used the this keyword to access the same class variables. Let's generate public getter and setter methods of variable maxSpeed to access from outside. In order to generate getter and setter methods, we right-click on the 'File' and select Source, generate getters and setters. We choose the variable maxSpeed, and then, click on the 'Generate' button. All selected getters and setter methods of variable maxSpeed are generated automatically. We don't want to generate getter and setter methods of variable model now, because we will test it in the main method. Okay, we will save the code. Let's create a new subclass. Right-click on the same package and select 'New Class'. I will specify the class name as Car and click the 'Finish' button.
Our subclass Car extends super class Vehicle. We get a compilation error. If you place the mouse pointer on the Car class, a clip shows a quick fix dialogue. We define the constructor in the super class Vehicle. So, we must define an explicit constructor in the subclass Car, and we must invoke the super class constructor from this constructor by using the super keyword. We can add the constructor by adding add constructor in the quick fix dialogue of eclipse. Eclipse automatically added the constructor with the super keyword. In class Car, let's declare a private variable is automatic with Boolean type. I will add the variable isAutomatic to the constructor of the Car class as the 3rd parameter. And I will assign the parameter variable isAutomatic to the variable isAutomatic of this class. We don't want to generate getter and setter methods of the variable isAutomatic now because we will test it in the main method. Let's create the main test class named Encapsulation Test in the same package. And I will check the check box for the main method and click 'Finish.'
In the main method, I will create a Car. Car myCar = new Car, and I will pass the parameters. The 1st parameter Ferrari represents the model of the car. The 2nd parameter 320 represents the max speed of the car. And the last parameter false represents the isAutomatic of the car. We have now assigned the necessary values to the variables of the Car class, thanks to the constructor. Well, if you want, let's try to change the isAutomatic variable that we defined in the Car class again. In the next line, I write myCar.isAutomatic. But if you notice, we cannot access the isAutomatic variable. This is because the access modifier of the isAutomatic variable is private. So, what should we do if we want to change the value of this variable?
This is where the setter method comes into play. If we create the setter method of the isAutomatic method in the Car class, we can now change the value of this variable. Let's do it now. I open the Car class. I right-click on the empty space here and select Source, Generate Getters and Setters option. From here, I will choose the only setter method of the isAutomatic variable, i.e. setAutomatic method. I click the 'Generate' button. Now, let's save our code and go back to the test class. Now, let's try to access the isAutomatic variable once again. As you can see, we now have access. Now, I will set its value to true. Yes, I think you understand the purpose of the setter method. Now, I will print the properties of the Car class by using print methods. In the first print method, we display the model of the car and we try to invoke the getModel method from super class vehicle.
But we cannot access this method because we have no public getter method of variable model in Vehicle class. Let's open the Vehicle class, and generate the getter method of variable model in the same way. Okay. Eclipse added the public getter method of variable model. If you notice, we can create only the getter method or only the setter method or both methods, if we want. It totally depends on the purpose of our app. For example, if you want to read only the value of a variable you have created without changing it, then you can only use the getter method. Or if you want the value of the variable to be changed but not read, then you can just use the setter method, or you can apply both methods if you don't have any restrictions. Or if you don't want anyone to have access to this variable, then you wouldn't use either method. This is entirely up to you. Okay, now I will save the code again. Let's open the main class again. Let's try to invoke the getModel method.
Okay, we can access this method anymore. In the 2nd print method, we display the max speed of car and invoke the getMaxSpeed method from super class Vehicle. We can access this method because we defined the getter method in the Vehicle class. In the last print method, we display car is automatic or not. We invoke the isAutomatic method from subclass Car. But we cannot access this method because we have no public getter method of the variable isAutomatic in the Car class. We only created the setter method. Let's open the Car class, and generate getter method of the variable isAutomatic in the same way. Okay. Eclipse added the public getter method of the variable isAutomatic. I will save the code. Let's open the main class again. Let's try to invoke the isAutomatic method. Okay. We can access this method anymore. Let's run the test code. You can see the results in the console. Model is Ferrari and max speed is 320. These properties come from getter methods in super class Vehicle. The car is automatic is true. This property comes from the getter method of subclass Car. Okay, I think you understood the concept of encapsulation in Java. Let's take a short break here. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.