Detailed explanation of the concept of Java encapsulation

1: Concept of encapsulation

Java is an object-oriented language. Object-oriented is an idea of code development in component mode. You need to learn a complete course to feel object-oriented understanding

Three basic characteristics: encapsulation, inheritance and polymorphism.

What we are learning today is encapsulation. Encapsulation is a very broad concept, ranging from the encapsulation of an attribute to the encapsulation of a framework or a project. For example, the framework we will learn later is the third-party encapsulated API (Application Programming Interface). Today we look at the smallest concept of encapsulation (encapsulation of attributes).

DEMO: Observation Code

  package com. bai . demo;
public class Hello {
   public static void main(String[] args) {
      Person per=new Person( );
      per . name="Li Si";
      per .age=-10;
      System. out . println("My name is:"+per . name) ;
      System . out . println("My age is:"+per.age) ;
   }
}

 

There is no syntax error in the code at this time, but there is a logical problem because a Person's age is different It may be a negative number. The root cause of this problem is that external programs can directly access the attribute age in the Person class. If we want to make its attribute inaccessible externally, we can use a keyword to encapsulate the attribute. This keyword is "private", which means private.

DEMO: encapsulation properties

   package com. bai . demo;
//Defines a class that regulates human information
public class Person {
    //name
      String  name ;
      //Age
      private int age;
}

 

At this time, it is found that the external program cannot access the age property, and the error prompted is "this property is invisible". Then if a property cannot be accessed, it will lose its own meaning. We should provide internal methods (interfaces) for the encapsulated property to access the property. There are two methods: one is to obtain the property (getter method) and the other is to assign a value to the property (setter method)

DEMO: provides setter and getter methods to access the age property

 package com.bai . demo;
//Defines a class that regulates human information
public class Person {
    //name
      String name;
      //Age
      private int age;
      public void setAge(int myage) {
        age=myage ;
      }
      public int getAge() {
       return   age;
   }
  }

 

 package com. bai . demo;
public class Hello {

 

 public static void main(String[] args) {
    Person per=new Person( ) ;
      Person per=new Person( ) ;
      per. name="Li Si";
      per. setAge(-10);
      System. out. println("My name is:"+per . name) ;
      System. out . print1n("My age is: "+per. getAge());
  }
}

 

}

Some students said: after tossing for a long time, it can still be negative. Isn't it in vain? In fact, when we access properties through methods, we can make some logical judgments in the methods to make the data more complete. For example, we can judge whether the age you enter is reasonable in the method.

DEMO: age determination in setter method

 package com. bai . demo;
//Defines a class that regulates human information
public class Person {
  //name.
   String   name ;
   //Age
    private int age;
    public void setAge( int  myage) {
     if(myage<=0||myage>200) {//If the incoming age is negative, the default value is used
        age=10;
     }else {
       age=myage;
  }
}
    public int   getAge() {
      return   age ;
  }
}

 

 

package com. bai . demo ;
public class Hello {
  public static void main(String[] args) {
      Person per=new Person( );
      per. name="Li Si";
      per.setAge(1000);
      System. out . println("My name is: "+per . name);
      System. out. println("My age is: "+per. getAge());
   }
 }

 

The above is the encapsulation of attributes. In development, the attributes of a class basically need to be encapsulated. In particular, simple Java classes (JavaBeans) need to be encapsulated. After encapsulation, setter s and getter s should be provided for the encapsulated attributes.

Summary:

1. Benefits of encapsulation:

● it can make the internal structure of the program more secure

● the repeated code can be encapsulated and then called where needed, which solves the duplication of code to a certain extent

2. Today we are learning the smallest concept of encapsulation (encapsulation of attributes)

3. The encapsulated properties should provide setter and getter methods

4. Writing rules of setter and getter methods:

● setter method: set + attribute name with initial capital letters. There is no return value (void) and parameters.

● getter method: get + attribute name in uppercase, with return value and no parameters.

 

More previous videos can be viewed on my Bili Bili: Codebond master's latest Java video tutorial in 2020

 

 

Posted by zeb on Wed, 04 May 2022 14:02:17 +0300