Object-oriented Programming (OOP) Permission modifiers

Source: Internet
Author: User

Permission modifiers for the class

The permission modifiers in the Java language primarily include private, public, and protected, which control access to member variables and member methods of classes and classes. If a member variable or member method of a class is decorated as private, the member variable can only be used in this class, is not visible in subclasses, and is not visible to classes of other packages. If you set the access permissions for the class's member variables and member methods to public, you can use them in subclasses and classes in other packages in addition to the data that you can use in this class. If the access rights of a class are set to private, this class hides its own data, so that users can not access it directly, and if it is necessary to make the data in a class or class in other packages available, set the class to public access. If a class uses the protected modifier, only the subclasses or other classes of that class within this package can access member variables and member methods in this class.

Public and protected-decorated classes can be accessed by subclasses, and if the subclass and parent class are not in the same package, only classes with the modifier public can be accessed by the quilt class. If the parent class does not allow access to his member variables by inheriting the resulting subclass, then the member variable of the parent class must be declared with private.

Note: When declaring a class without using the private, public, and protected modifiers to set the class's permissions, the class is preset to the packet access scope, where only a class in a package can invoke the member variable or member method of the class.

Take the rectangular title as an example:

 Package org.hanqi.pn0120; // of public  Public class JuXing2 {            // private  inner  encapsulation     of Class  Chang;         // protected     of protection  Public double Kuan;

Member variables

The properties of an object in the Java language are called member variables and can also be called properties. To understand the member variables, first define a book class, the member variables correspond to the properties of the class object, set the ID, name, and CATEGORY3 member variables in the book class, respectively, corresponding to the library number, book name and book Category 3 book properties.

(1) modifier data type variable name;

(2) The specification is set as private

Open getter or Setter methods

POJO simple Java object ① only properties and Getter,setter methods ② No business logic

Take a rectangle as an example

 Packageorg.hanqi.pn0120;//of public Public classJuXing2 {//internal encapsulation of the private class    Private DoubleChang; //protected of protection      Public DoubleKuan;  Public DoubleGetchang () {returnChang; }           Public DoubleGetkuan () {returnKuan; }                          Public voidSetchang (DoubleChang) {         //Add Validation         if(chang>0)         {          This. chang=Chang; }         Else{System.out.println ("Length value" +chang+ "Invalid"); }     }                    Public voidSetkuan (DoubleKuan) {         if(kuan<=0) {System.out.println ("Width value" +kuan+ "Invalid"); }         Else         {              This. kuan=Kuan; }     }      PublicJuXing2 () {} PublicJuXing2 (DoubleChangDoubleKuan) {        //This.chang=chang; //This.kuan=kuan;Setchang (Chang);    Setkuan (Kuan); }        //Calculate Area    DoubleGetmianji () {return  This. chang* This. Kuan; }         Public Static voidMain (string[] args) {JuXing2 myjx=NewJuXing2 (20,6); Myjx.chang=12; System.out.println ("Long =" +Myjx.chang); System.out.println ("Area of the rectangle =" +Myjx.getmianji ()); }}
View Code

 Packageorg.hanqi.pn0120; Public classTest1 { Public Static voidMain (string[] args) {JuXing2 myJX1=NewJuXing2 (20,6); Myjx1.kuan=10; //Reading property valuesSystem.out.println ("width =" +Myjx1.getkuan ()); System.out.println ("Long =" +Myjx1.getchang ()); System.out.println ("area =" +Myjx1.getmianji ()); JuXing2 myJX2=NewJuXing2 (); Myjx2.setchang (-30); Myjx2.setkuan (0); System.out.println ("Area of the rectangle" +Myjx2.getmianji ()); }}
View Code

Object-oriented Programming (OOP) Permission modifiers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.