Encapsulation in Java - ProgrammerTech
Language:

Search

Encapsulation in Java

  • Share this:
Encapsulation in Java

Concept Encapsulation

Access degrees (encapsulation) in Java, which means in literal translation, encapsulation. In the previous lesson, we got acquainted with the concepts of object-oriented programming and what is Package and class and the benefits of them, and we got to know the object and the creation of an object and the benefit of the object. In this lesson, we will learn about encapsulation, or what is known as encapsulation of data inside the class.

And the meaning of the degree of access are some words that we can add when we add new things such as functions, classes, and variables in order to define how to access them. We will need to use encapsulation if we are working on a large program with a software team. That is, if you are working on a specific program with a software team and want to ensure that you or one of the programmers do not abuse a specific code. Here we specify the degree of access to him and the powers restricted to access.

We touched on the word “data” in the previous lesson, and we said that when we mention the word “data” in object-oriented programming (oop), we mean by it variables and functions. We can when we determine the degrees of access to these variables or functions and we can make the variable or function protected at the level of the class. It means that we cannot access that function or variable unless we are in the same class, or we reduce the degree of protection and make this variable or function protected at the package level. That is, we can access that variable or function only from the same package or from any class or remove. Protection permanently so that we can access the variable or function from the same package or outside the package.

There are three types of encapsulation degrees:

  1. private.
  2. protected.
  3. public.

The general form of defining access degrees in Java

privaty int x=5;
protected int y=23;
public double z=28;
  1. First: We cannot access the variable except through the same class.
  2. Second: We can access the variable provided that we are in the same package only.
  3. Third: Available to access either from the package or outside.

 

Accessing an unprotected public element in Java

It means that the public defined class, function, or variable can be accessed directly, i.e. the properties of this object can be accessed either inside or outside the package from anywhere in the program.

package oop;
class a
{
    private int x=5;
    protected int y=23;
    public double z=26;
}
public class Oop {
    public static void main(String[] args) {
        a ob=new a();
        System.out.println(ob.y);
    }
}

We created a Package named oop and then created a class named a and it had variables inside it containing the three access degrees. Each variable has a different access point, a different value, and a different data type. Then we created another class called Oop. And we entered the main program, we derived the object ob from the name of the class a and then we put new, which means creating a copy of the class and storing all the variables inside the ob variable. And when we come to print when we write ob object and put a dot it will suggest to us the variables y and z only because the variable x is protected and unable to access it.

 

Access to a protected object private java

In this section, we will learn how to access a private variable or function. And the benefit of accessing the protected variable is that we always have to access the protected variable for two things, which are modifying the value of the variable or getting a value stored inside the variable. For example, we will create a protected variable of scalar type and we want to access it. To access the protected variable, we must use an argument function, and we have two goals to access it.

package oop;
class a
{
    private int x;
    public void setx(int r)
    {
        x=r;
    }
    public int getx()
    {
        return x;
    }
}
public class Oop {
    public static void main(String[] args) {
        a ob=new a();
        ob.setx(5);
        System.out.println(ob.getx());
    }
}

We created a Package with the name oop and then we created a class named a and it had a scalar variable named x and it doesn't have a value at the moment. And we built a public void setx(int ​​r) argument function whose job is to modify the value of x or store a value in the variable x. We set it void to not return a value because we want to send it the value we want to store in x and then we created a variable x=r to store the value we received. And we created the second task, public int getx, and its task is to get the value of x using the getx function.

Then we went into the main program and created an ob object from the variable a and then started sending the value of the variable x through. ob.setx and we gave the value sent is 5 then we wrote the print command by typing the object and putting the getx function to print the value.

One of the benefits of encapsulation is that things in classes can be made readable or writable by external classes.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com