Core Java - Encapsulation.

saravananb15

Saravanan B

Posted on February 16, 2023

Core Java - Encapsulation.

Encapsulation is a process of binding data and code together. 2 Major advantage of encapsulation is security and easy enhancement.

public class Customer {
    private String firstName;
    private String lastName;
    private String debitCard;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getDebitCard() {
        return debitCard;
    }

    public void setDebitCard(String debitCard) {
        this.debitCard = debitCard;
    }
}
Enter fullscreen mode Exit fullscreen mode
public class Test {
    public static void main(String[] args) {
        Customer c = new Customer();
        c.setFirstName("dev");
        c.setLastName(".to");
        c.setDebitCard("1234");
        System.out.println(c.getFirstName()+" "+c.getLastName()+" "+c.getDebitCard());
    }
}
Enter fullscreen mode Exit fullscreen mode

Output - dev .to 1234

💖 💪 🙅 🚩
saravananb15
Saravanan B

Posted on February 16, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related