18 July 2023

Encapsulation In Java - learngreen.net

 Encapsulation is one of Java's four fundamental principles of object-oriented programming (OOP). It is a mechanism that combines data and the methods (or functions) that operate on that data into a single unit called a class. Encapsulation provides data hiding and abstraction, allowing better data control and protection.  


 Here are the main aspects of Java encapsulation:-

 

 1. Data Hiding:-  Encapsulation hides the internal data of an object and exposes only the necessary data through public methods. By marking internal data fields as private, you restrict direct access to them outside the class. This prevents unauthorized changes and ensures that data is only accessed and modified using controlled methods. 


 2. Access modifiers:-  Java provides access modifiers like public, private, protected, and default (no modifier). You can use these modifiers to control the visibility and accessibility of class members (fields and methods). Private members can only be accessed within the same class, while public members can be accessed from any class. Protected members have limited access to the same package or subcategories. 


 3. Getters and Setters:-  You can interact with private data fields by defining public methods called getters and setters. Getters provide read-only access to data, allowing other classes to retrieve values. Setters allow write access, allowing other classes to modify the data. By encapsulating the data in these methods, you can apply additional logic, validation, or handling as needed.  


 4. Data validation and consistency:-  Encapsulation allows you to enforce validation rules and maintain data consistency. By encapsulating data in methods, you can perform checks, validations, and transformations before receiving or returning data. This helps keep the integrity of the object's state and prevents incorrect or inconsistent data from being stored.  


 5. Abstraction:-  Encapsulation supports abstraction by exposing only the essential details of an object while hiding complexity. Users of an encapsulated class interact with a well-defined interface of public methods without knowing the details of the internal implementation. 


In general, Java encapsulation promotes safe, modular, and maintainable code by encapsulating data and behavior within a class, controlling access with accessor specifications, and providing a well-defined interface for interacting with an object.


Encapsulation In Java




 package Java;
 class Bike {
 // Private data field for the bike company	 
 private String company;
 //Private data field for the bike's cc
 private int cc;
 //Getter for the company field
 public String getcompany() {
 return company;
   }
 //Setter for the company field
 public void setcompany(String company) {
 this.company = company;	 
   }
 //Getter for the cc field
 public int getcc() {
 return cc;	 
   }
 //Setter for the cc field
 public void setcc(int cc) {
 this.cc=cc;	 
   }
 }
 public class MainCode {
 public static void main(String[] args) {
 // Creating an instance of the Bike class	 
 Bike obj = new Bike();
/* Setting the company and cc
 * values using the setter methods
 */
 obj.setcompany("Royal Enfield");
 obj.setcc(350);
 System.out.println
 ("Bike Company "+ obj.getcompany());
 System.out.println
 ("Bike cc "+ obj.getcc());
 //Output-> 
 /* Bike Company Royal Enfield
  * Bike cc 350
  */
  }
 }

 


 package Java;
 class Machine {
 //Use private to hide data	 
 private int price;
 //Getter for the price field
 public int getprice() {
 return price;	 
  }
 //Setter for the price field
 public void setPrice(int price)
 {
   this.price = price; 
   }
 }
 public class LearnGreen {
 public static void main(String[] args) {
 Machine obj = new Machine();
 obj.setPrice(150000);
 System.out.println
("The price of the machine is "+obj.getprice());
 /* Output->
  * The price of the
  * machine is 150000
  */
   }
 }

 


 package Java;
 class Addition {
 //fields to calculate addition	
 int first;
 int second;
 //constructor to initialize values
 Addition(int first, int second) {
 this.first=first;
 this.second=second;
 }
 //method to calculate addition
 public void getaddition() {
 int Addition = first + second;
 System.out.println("Addition is "+Addition);
   }
 }

 public class MainClass {
 public static void main(String[] args) {
 //Create object of class Addition	 
 Addition obj = new Addition(8,4);
 //call method getaddition()
 obj.getaddition();
 /* Output->
  * Addition is 12
  */
 }

}

 



No comments:

Post a Comment