Java modifiers and encapsulation with set get method

Java modifier:
No restrictions on Public access
private can only be accessed in this class, and subclasses are not visible
protected can be accessed in this package and in subclasses of other packages. Subclasses of this package are visible
default can be accessed in this package. The subclass of this package is visible

Test default:

public class TextP2 {
    int d=123;
}

class TextP3{
public static void main(String[] args) {
        TextP2 p2=new TextP2();
      System.out.println(p2.d);
   }
//    public void f2(){   
//        System. Out. Println (d); D cannot be called
//   }
}
class TextP4 extends TextP2{
    public void f2(){
        System.out.println(this.d);
        System.out.println(d);
    }
}

TextP4 can access d, which shows that default subclasses can be accessed, and d is invoked in class TextP3, which indicates that default can also be accessed in other classes in the same package.

(the following paragraphs are my own doubts, which can be skipped)
During the test, I had a question: since all the classes in the package are accessible, why can't d in TextP3 be called (comment part in the code)?

The explanation should be as follows: d can be accessed in this class, needless to say, it can be accessed in a subclass because the subclass inherits the property of age, while in TextP3, age is not its property. How can a class directly access something that is not its property?

If there is any doubt, it can still be understood through memory analysis. In the heap, a class is equivalent to an independent unit. P2 and P4 have an inheritance relationship, and age also become the property of P4. P3 has no connection with them, so it cannot be called. We can use our own things at will. It is most appropriate to understand the access of subclasses through this sentence (private is special). Other people's access to your family's things is through objects, and there are access restrictions, so they need to comply with the above specifications.

Test private:

public class TextP2 {
    public static void main(String[] args) {
        TextP2 p2=new TextP2();
        System.out.println(p2.d);
    }
    private int d=123;
}


class TextP4 extends TextP2{
//    public void f2(){
//       System.out.println(d);
//   }
}

Test result: private can be accessed in this class, but not in the subclass. Let's say that age is your own thing for P4, but it can't be accessed. For example, your father's mobile phone is not yours, and it can't be seen in general. If the subclass can't be accessed, we won't talk about other classes

Test protected: only non inheritance of other packages cannot be accessed

public class TextP2 {
    public static void main(String[] args) {
        TextP2 p2=new TextP2();
        System.out.println(p2.d);   //Other classes in the same package can access
    }
   protected int d=123;
}


class TextP4 extends TextP2{
    public void f2(){
        System.out.println(this.d);  //Subclass can access
    }
}


//Another package
import Text.TextP2;

public class TextP5 extends TextP2 {
    public void f(){
        System.out.println(this.d);  //Subclasses of different packages can access protected
    }
}

Test public: this will not hang the code, and public will go all the way
To summarize:
The restrictions on other classes are all for objects, so the location of calls to these properties and methods are all within the methods with objects (main methods or reference methods passing to get objects)

private encapsulation usage:

package Text;

import java.util.Scanner;

class Text1{
    private int age;
    private String name;

    public String getName() {
        return name;
    }
    public int getAge(){
        return age;
    }

    public boolean setAge(int age) {
        if(age<0||age>120){
            return false;
        }
            else{
            this.age = age;
            return  true;
        }

    }
    public boolean setName(String name){
        if(name.length()>4){
            return false;
        }
        else {
            this.name = name;
            return true;
        }
    }
}


public class TextP {
    public static void main(String[] args) {
        Text1 t1=new Text1();
        Scanner scanner=new Scanner(System.in);
        System.out.println("input the age");
        while(!t1.setAge(scanner.nextInt())){
            System.out.println("please input true age");
        }
        scanner.nextLine();
        System.out.println("input the name");
        while(!t1.setName(scanner.next())){
            System.out.println("please input true name");
        }
        System.out.println();
        System.out.println(t1.getAge());
        System.out.println(t1.getName());
    }
}

Through the gate of set method, the whole class is encapsulated to protect the attributes in the class and improve the maintenance performance of the program, so as to avoid the bug of 1000 years old. Moreover, the class coupling is small and the portability is strong.
Over.

Published 5 original articles, won praise 0, visited 55
Private letter follow

Tags: P4 Java Mobile

Posted on Tue, 10 Mar 2020 03:47:13 -0400 by krupapatel