image
Encapsulation
Start course
Difficulty
Beginner
Duration
1h 46m
Students
22
Ratings
5/5
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.

Learning Objectives

  • Inheritance in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello there. In this video, we will learn about Java Encapsulation. So, let's begin. 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 classes 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 be accessed from outside the class. Let's try and understand encapsulation with the help of some examples. In the object-oriented programming project, right click on the 'Source' folder and select 'New class.'  Specify the package name as encapsulation and the class name is vehicle.

Vehicle class is a super class. Let's declare two instance private variables named model with string type and maxSpeed with int type and vehicle class. Private means that these variables are visible to the current class only. Let's declare a public constructer named vehicle and it takes two parameters: model and maxSpeed. In the constructor method, we assign the parameter variable model to this class as variable model and we assign the parameter variable maxSpeed to the variable maxSpeed of this class. We use 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, let's save the code. Let's create a new sub class.

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, Eclipse 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 selecting 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 is automatic to the constructor of the car class as the third parameter, and I will assign the parameter variable is automatic to the variable isAutomatic of this class. We don't want to generate getter and setter methods of the variable is automatic 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 first parameter, Ferrari, represents the model of the car. The second parameter, 320, represents the maxSpeed 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 the source  to generate getters and setters option. From here, I will choose the only the setter method of the isAutomatic variable i.e. set automatic method and 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. All right, 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 get model 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. All right, Eclipse added the public getter method a 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 only 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, great, we can now access this method. In the second print method, we display the maxSpeed of car and invoke the get maxSpeed method from Super class vehicle. We can access to this method because we defined the getter method in the vehicle class. In the last print method we display is car 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. So, let's open the car class and generate the getter method of the variable isAutomatic in the same way. All right, 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. All right, great. We can now access this method. Let's run the test code. You see the results in the console, model is Ferrari and maxSpeed is 320. These properties come from getter methods in Super class vehicle. But cars automatic is true, this property comes from the getter method of subclass car. Okay, I hope you understand the concept of encapsulation in Java a little better now. Let's take a short break here, my friends, and I'll see you in the next video.

 

About the Author
Students
3925
Courses
64
Learning Paths
5

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.