Java Encapsulation In Hindi (Java Encapsulation क्या है ?)

 

Java Encapsulation In Hindi


Java - Data Encapsulation In Hindi :

Data Encapsulation में data members और methods एक ही unit पर या class पर wrap करके रखा जाता है |

Data Encapsulation class के attributes और methods को combine करके रखता है |

Data Encapsulation को Data Hiding भी कहा जाता है, चूँकि इसमे जो instance variables होते है, वो private होते है | इसका मतलब ये है , अगर इसे outside से access किये जाए तो ये accessible नहीं होते |

Normally Encapsulation में जो methods इस्तेमाल की जाती है उनका उल्लेख getter और setter होता है |

Encapsulation में data members और methods की accessibility(Access Modifiers) भी काफी प्रभावित करती है |

Program पर देखे तो , तीन private data members किये गए है | ये members सिर्फ अपने class के लिए ही accessible होंगे, ये outside से या किसी दूसरी class से access नहीं किये जा सकते | बाद में तीन public getter और तीन setter लिए गए है | ये public होने के कारण outside से भी इन methods को access किया जा सकता है | जो private data members वहा पर दिए गए है, वो public methods के माध्यम से ही accessible रहेंगे |


Source Code :


//Employee.java
class Employee
{
    private int id;
    private String name;
	private double salary;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
	public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    public static void main(String args[])
    {
        Employee e = new Employee();
        
        e.setId(99);
        e.setName("Maxon");
        e.setSalary(46000.65);
		
        System.out.println("Employee Id : "+e.getId());
        System.out.println("Employee Name : "+e.getName());
	 System.out.println("Employee Salary : "+e.getSalary());
    }
}

Output :

Employee Id : 99
Employee Name : Maxon
Employee Salary : 46000.65

Data Encapsulation के फायदे :

  • Encapsulation का code flexible होता है चूँकि Requirement के हिसाब से code को change किया जा सकता है |
  • Encapsulation में data की accessibility को control किया जाता है |
  • Encapsulation में code की maintainability और reusability बढाई जाती है |



अगर आपको यह पोस्ट 📑 पसंद आई हो तो अपने मित्रों के साथ जरूर शेयर करें। धन्यवाद !

Maxon

Hello there! I'm Maxon, a dedicated UI/UX designer on a mission to transform digital experiences into intuitive, user-centric journeys. With a keen eye for detail and a passion for crafting aesthetically pleasing interfaces, I strive to create designs that not only look stunning but also enhance usability and functionality.

Post a Comment

Previous Post Next Post