0% found this document useful (0 votes)
5 views

3. Abstraction & Encapsulation

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

3. Abstraction & Encapsulation

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

3.

Abstraction & Encapsulation


1. Intro > Object Technology

1. Abstraction
Abstraction

Reduce and factor out details so that one can focus on a few concepts at a
time

Objects in reality are complex


-> Need to be simplified by ingoring all the unnecessary details

Note

Only extract related, important information the the problem.

// Options for the wash cycle


type WashOptions = {
dryLevel: 'low'|'medium'|'high'
temp: 'cold'
dur: 'hour'
eco: false
}

// the abstraction
class WashingMachine{
// private instance vars

public startCycle (Options: WashOptions): void{


//Parse the options
// get access to the physical layer
// Convert options into commands
// more low-level code...
}
// more methods
}
Dictionary of Object Technology, Firesmith, Eykholt, 1995

Any model that includes the most important, essential, or distinguishing


aspects of smth while suppressing or ignoring less important, immaterial, or
diversionary details. The result of removing distinctions so as to emphasize
commonalties.

Note

Allow managing a complex problem by focusing on important properties of


an entity in order to distinguish with other entities.

Abstraction is a view of an entity containing only related properties in a


context.

Class is the result of the abs, which represents a group of entities with the
same properties in a specific view.

Class >< Objects

Class Objects
Is aconcept model, describing entities Real entities

A prototype/blueprint, defining common An instance of a class, building


properties and methods of objects from the blueprint

Each object has a class


specifying its data and behavior.
An abstraction of a set of objects
Data of different objects are
different.

Class in UML
Attribute

A named characteristic of a class.

All instances of the class have this attribute.

A class can have no attributes or many attributes

Operation Visibility

Used to enforce encapsulation


Java Modifiers

How is visibility noted in UML

+ - public
# - protected
- - private
~ - package access

2. Class Building
Class Construction

Class name

Specify what the abstraction is capturing


Should be singular, short and clear identify the concept

Data elements

The pieces of data that an instance of the class holds

Operations/Messages

List of messages that instances can receive.


Methods

Implementations of the messages that each instance can receive

Package

PACKAGE

A set of classes that have some logic relation between them.

Considered as a directory, a place to organize classes in order to locate them


easily.

Avoid name conflict : different packages can have classes with the same
name.

Can protect class and their members from outside access.

Example

java.lang , javax.swing , java.io

User defined package:

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.

3. Encapsulation and data hiding


An object have two view:
Internal view - details on attributes and methods of the corresponding
class.
External view - services provided by the obj and how the obj
communicates with all the rest of the system

Encapsulation

Data/attributes and behaviors/methods are encapsulated in a class.

Data hiding

Data is hidden inside the class and can only be accessed and modified from
the methods (getter and setter)

Data hiding mechanism


Data member (with private modifier) can only be accessed from methods in
the class.
Other objects want to access to the private data must perform via public
func

Accessor (getter)

return the current value of an attribute

There are 3 query types:

Simple query - what is the value of var

Conditional query - return true or false based on conditions with vars

Complex query - perform complex manipulation with value of vars


(equations)

Getter should not make any changes to the current

4. Object Creation and Communication


Data initialization

Data need to be initialized before being used to avoid errors.

for simple/basic data typ, use operator =


for creating objects, need to use constructor method

Constructor

Every class must have at least one constructor


A constructor can't use abstract, static, final, native, synchronized
but public, private or default (used in package only)
public BankAccount(){ // default constructor without @param
owner = "none";
balance = 10000;
}

public BankAccount(String str, double d){


this.owner = o;
this.balance = b;
}

Object declaration and initialization

ClassName obj_name = new Constructor(@param);

return a reference pointing to the new object.

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

BankAccount acc1 = new BankAccount("Han", 1000); // create


a new obj
BankAccount acc2 = BankAccount("Han", 1000); // Do not
create a new obj, acc2 point to obj1

Use obj's public attributes and pulic method:

obj_name.attr_name;
obj_name.method_name(@param);

4. Some techniques in class building

You might also like