The Power of Encapsulation and Role of Getter and Setter Methods

                                      ENCAPSULATION 

Encapsulation in Java is one of the four fundamental Object-Oriented Programming (OOP) concepts. Encapsulation helps in hiding the internal state of an object from the outside world and only exposing the necessary functionalities or methods to interact with that state. This concept is implemented through access modifiers such as public, private, and protected in Java. Encapsulation is used for variables.

Example:-

Advantages of Encapsulation👇


Getters & Setters 

Getter and setter methods in Java are used to access and modify the private variables (fields) of a class. They are also known as accessor and mutator methods. 

Example:-


📌Before java 8 we can use with direct private variable but after we can use with set method.

Uses of   🅶 🅴 🆃 🆃 🅴 🆁 🆂 and 🆂 🅴 🆃 🆃 🅴 🆁 🆂 Method📝

☝Allow you to control access to the fields of a class. 

☝Validation and Error Handling

☝By using getter and setter methods, you can change the internal implementation of a class

☝Encourages Good Design Practices

☝Integration with Frameworks and Tools.



Reference-:
https://www.geeksforgeeks.org







Comments

Popular posts from this blog

Unveiling the Trinity of Standards, Objects, and Classes

A Guide to Object Generating, Data Types, Parameters and Arguments

Getting Started with Design Patterns - Part 1