DEV Community

Saravanan B
Saravanan B

Posted on • Updated on

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

Top comments (0)