Encapsulation is the concept of hiding the implementation details of a class and allowing access to the class through a public interface. For this, we need to declare the instance variables of the class as private or protected.
The client code should access only the public methods rather than accessing the data directly. Also, the methods should follow the Java Bean's naming convention of set and get.
Encapsulation makes it easy to maintain and modify code. The client code is not affected when the internal implementation of the code changes as long as the public method signatures are unchanged. For instance:
public class Employee
{
private float salary;
public float getSalary()
{
return salary;
}
public void setSalary(float salary)
{
this.salary = salary;
}
3/23/2010
Abonați-vă la:
Postare comentarii (Atom)
ANTLR 4 into a Quarkus app
Using ANTLR 4 with Quarkus: A Practical Guide Why combine ANTLR + Quarkus? Quarkus gives you a fast, cloud/native Java framework with ...
-
Monitoring your applications is crucial to ensure they run smoothly and efficiently, especially when it comes to modern microservices a...
-
The CI/CD pipeline, standing for Continuous Integration and Continuous Delivery, plays a vital role in DevOps. Continuous Integration ensu...
-
Example Workflow to Create a Jira Ticket Using Jira CLI To create a Jira ticket using the Jira CLI, you can use the jira-cli tool, which i...
Niciun comentariu:
Trimiteți un comentariu