Monday, 1 May 2023

What is Encapsulation in Java? with example.


Encapsulation Is A Fundamental Concept In Object-oriented Programming (Oop) That Refers To The Bundling Of Data And Methods That Operate On That Data Within A Single Unit Called A Class. In Encapsulation, The Internal Details Of The Object Are Hidden From The Outside World, And Only The Relevant Information Is Exposed. Encapsulation Provides Data Security And Helps To Ensure That The Data Is Used Correctly.


Here's an example of encapsulation in Java:


```

public class BankAccount {

    private int accountNumber;

    private double balance;

    private String customerName;

    private String email;

    private String phoneNumber;


    public void deposit(double amount) {

        balance += amount;

        System.out.println("Deposit of " + amount + " made. New balance is " + balance);

    }


    public void withdraw(double amount) {

        if (balance - amount < 0) {

            System.out.println("Insufficient funds. Cannot withdraw.");

        } else {

            balance -= amount;

            System.out.println("Withdrawal of " + amount + " processed. Remaining balance is " + balance);

        }

    }


    public int getAccountNumber() {

        return accountNumber;

    }


    public void setAccountNumber(int accountNumber) {

        this.accountNumber = accountNumber;

    }


    public double getBalance() {

        return balance;

    }


    public void setBalance(double balance) {

        this.balance = balance;

    }


    public String getCustomerName() {

        return customerName;

    }


    public void setCustomerName(String customerName) {

        this.customerName = customerName;

    }


    public String getEmail() {

        return email;

    }


    public void setEmail(String email) {

        this.email = email;

    }


    public String getPhoneNumber() {

        return phoneNumber;

    }


    public void setPhoneNumber(String phoneNumber) {

        this.phoneNumber = phoneNumber;

    }

}

```


In This Example, The `Bankaccount` Class Encapsulates The Data Related To A Bank Account, Such As The Account Number, Balance, Customer Name, Email, And Phone Number. The Class Also Contains Methods For Depositing And Withdrawing Money From The Account.




The Data Members Of The Class (I.E., The Account Number, Balance, Customer Name, Email, And Phone Number) Are Declared As Private, Which Means That They Can Only Be Accessed From Within The Class. The Public Methods (I.E., The Deposit And Withdraw Methods And The Getter And Setter Methods) Are Used To Access And Modify The Private Data Members. By Doing So, The Internal Details Of The Object Are Hidden From The Outside World, And Only The Relevant Information Is Exposed.


For Example, The `Getbalance` Method Is Used To Retrieve The Current Balance Of The Account. Since The `Balance` Data Member Is Declared As Private, It Cannot Be Accessed Directly From Outside The Class. Instead, The `Getbalance` Method Is Used To Retrieve The Value. Similarly, The `Setcustomername` Method Is Used To Set The Name Of The Customer Associated With The Account. Since The `Customername` Data Member Is Declared As Private, It Cannot Be Modified Directly From Outside The Class. Instead, The `Setcustomername` Method Is Used To Set The Value.

No comments:

Post a Comment

What is Java Permutation and Combination Program?

Here's A Java Program That Calculates Permutations And Combinations: ```Java Import Java.Util.Scanner; Public Class Permutationcombinati...