CP213 Lecture 3: Encapsulation

62 views1 pages
School
Course
Professor

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents