3. Abstraction & Encapsulation
3. Abstraction & Encapsulation
1. Abstraction
Abstraction
Reduce and factor out details so that one can focus on a few concepts at a
time
Note
// the abstraction
class WashingMachine{
// private instance vars
Note
Class is the result of the abs, which represents a group of entities with the
same properties in a specific view.
Class Objects
Is aconcept model, describing entities Real entities
Class in UML
Attribute
Operation Visibility
+ - public
# - protected
- - private
~ - package access
2. Class Building
Class Construction
Class name
Data elements
Operations/Messages
Package
PACKAGE
Avoid name conflict : different packages can have classes with the same
name.
Example
Separated by “.”
Convention for naming package: use lower letters only
Caution
Each source file can have only one package declaration command at the top.
If we use not use this command, the file is in default package.
Encapsulation
Data hiding
Data is hidden inside the class and can only be accessed and modified from
the methods (getter and setter)
Accessor (getter)
Constructor
Caution
If there is declaration have the same @param without the new
keyword -> do not create a new obj, just another ptr to the same
object
Example
obj_name.attr_name;
obj_name.method_name(@param);