CP213 Lecture 3: Encapsulation
Document Summary
There are 4 core concepts in oop: encapsulation, inheritance, polymorphism, and abstraction. The idea behind encapsulation is to ensure that implementation details are not visible to users. The variables of one class will be hidden from the other classes, accessible only through the methods of the current class. To achieve encapsulation in java, declare the class" variables as private and provide public setter and getter methods to modify and view the variables" values. For example: class bankaccount { private double balance=0; public void deposit(double x) { if(x > 0) { balance += x; This implementation hides the balance variable, enabling access to it only through the deposit method, which validates the amount to be deposited before modifying the variable. Control of the way data is accessed or modified.