Day 14: Encapsulation

Day 14:

Encapsulation:

Encapsulation in Java is integrating the data (fields) and the methods that operate on the data into a single unit and restricting the direct access to some of the object's components.

Encapsulation is a way of hiding the implementation details of a class from outside access and only exposing a public interface that can be used to interact with the class.

In Java, encapsulation is achieved by declaring the instance variables of a class as private, which means they can only be accessed within the class. To allow outside access to the instance variables, public methods called getters and setters are defined, which are used to retrieve and modify the values of the instance variables, respectively. By using getters and setters, the class can enforce its own data validation rules and ensure that its internal state remains consistent.

It helps in two key things:

  1. Data binding
  2. Data hiding through access control.


Data Hiding: The fields of a class are declared private to restrict direct access from outside the class.

Access Control: Public getter and setter methods are provided to access and modify the private fields in a controlled manner. Data Hiding: The fields of a class are declared private to restrict direct access from outside the class.

Example:

  • Private Fields: In below program accountHolderName and balance are private. They cannot be accessed directly outside the BankAccount class.
  • Public Getters and Setters: getAccountHolderName() and setAccountHolderName() provide controlled access to accountHolderName.




  1. Methods for Behavior: The deposit and withdraw methods allow specific operations on the balance while enforcing rules like preventing invalid amounts.
  2. Data Integrity: By using encapsulation, we ensure that balance cannot be directly manipulated.

Happy Learning :)

要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

  • DAY11 Multiple Inheritance and Interfaces:

    DAY11 Multiple Inheritance and Interfaces:

    4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the…

社区洞察

其他会员也浏览了