ตั้งค่าการอ่าน

ค่าเริ่มต้น

  • เลื่อนอัตโนมัติ
    Java

    ลำดับตอนที่ #14 : Java IV - Encapsulation

    • อัปเดตล่าสุด 31 ธ.ค. 61


    Information Hiding

    Encapsulation

              For the next part of this lesson, we will learn about encapsulation, one of the most important concepts in object-oriented programming. Here, encapsulation means to hide the information of the class.


    How does Encapsulation Work?

              To get an idea of how encapsulation works in the real world, think of a computer. The internal parts are hidden inside, and the person using it only interacts with certain parts. The user cannot freely access the internal hardware, preventing them from breaking the PC accidentally.


    Class Encapsulation

              When making a class that is public, we hide features that we don’t want others to use. That is, we limit the parts of the class available to others. By doing this, we make it easy for others to know which features to use. As a result, encapsulation allows users to use the class safely.


    Access Restriction to Fields and Methods

              To be more precise, encapsulation restricts access to fields and methods. We use public to make them accessible and private to hide them from the outside of the class. The examples below show how a private field cannot be accessed from the Main class.



    Getter

    Access from Outside the Class

              If you set the permission of a field to private, the field cannot be accessed from outside the class. On the other hand, it is possible to access it from within the class even if it's private.


    Getter

              Once we make a field private, in order to get the field value safely from outside the class, we have to define a method that only returns the value of the field. This method is called a getter. It's common to name a getter as follows: getFieldName().



    Setter

    Updating Field Values

              If you set the permission of a field to private, you can no longer update the value of the field from outside the class.


    Setter

              So, we need to define a method to change the value of the field. Such a method is called a setter. It's common to give a setter a name that includes the name of the field it provides access to, like so: setFieldName().


    Encapsulation Standard

              It may seem hard to tell which parts to make private or public, but there is a standard. Basically, we will be fine if we make (1) fields private and (2) methods public. Until we get to more complex programs, let's stick with these conventions.

     


    Adding a Job

    Let's Add a Job!

              Finally, let's add an instance field called job to store a job title for each Person instance. Let's try to complete the following steps all in one exercise: (1) declare a field; (2) change the constructors; (3) define getter and setter; and (4) modify a method.






    ติดตามเรื่องนี้
    เก็บเข้าคอลเล็กชัน

    ผู้อ่านนิยมอ่านต่อ ดูทั้งหมด

    loading
    กำลังโหลด...

    อีบุ๊ก ดูทั้งหมด

    loading
    กำลังโหลด...

    ความคิดเห็น

    ×