6 Encapsulation
6 Encapsulation
Objectives
• Class and Object
• How to identify classes
• Hints for class design
• How to declare/use a class
• Current object
• Member functions
• Concept Package
• Access modifiers (a way to hide some members in a
class)
• Case study
The concept of an object
• Real-world object representation
• Each object is characterized by its own
properties and behaviors
Traits and behavior
• Characteristics
• Manufacturer
• Model
• Five
• Color
• Behavior (What can a car do?)
• Start up
• Stop
• Brake
• Turn on the wipers
Class
Car Animal
Class definition
• A class is a template used to describe
objects of the same type.
• The class consists of properties (data
fields) and methods (member functions).
Attributes and methods
• Attributes (fields)
Danh từ
• Manufacturer
• Model
• Five
• Color
• Method (method)
Động từ
• Start()
• Stop()
• Brake()
• Turn on wiper()
Attributes and methods
Ô tô
Thuộc tính
• Năm
• Nhà sản xuất
Ô tô của Dũng • Model Ô tô của Mai
• Màu
Phương thức
Thuộc tính Thuộc tính
• Khởi động
• Năm = 2010 • Năm = 2012
• Dừng
• Nhà SX=Honda • Nhà SX=BMW
• Phanh
• Model = Accord • Model = CS30
• Màu = Xanh • Màu = Bạc
Phương thức Phương thức
• Khởi động • Khởi động
• Dừng • Dừng
• Phanh • Phanh
class
Khai báo các trường
class <<ClassName>>
{
<<type>> <<field1>>;
Khai báo các phương thức
…
<<type>> <<fieldN>>;
<<type>> <<method1>>([parameters]) {
// body of method
}
…
<<type>> <<methodN>>([parameters]) {
// body of method
}
}
Ex
Trường
Phương thức
Lớp Employee có 2 thuộc tính là fullname và salary và 2 phương thức là input() và output()
Create object
• The following code uses the Employee class to
create an employee then calls the methods of the
class.
• Attention:
• The new operator is used to create objects
• The emp variable contains a reference to the object
• Use dot (.) to access class members (fields and
methods).
DEMO
Create a student description class
including full name, grade, and
methods of input, output, and
academic rating
Định nghĩa phương thức
Method definition
• A method is a module of code that does a
specific job
• In Employee class, there are 2 methods input() and
output().
• The method can have one or more parameters
• The method can have a return type or void
(returns nothing).
• Syntax
<<return type>> <<method name>> ([parameter list])
{
// method body
}
thức
class MayTinh{
int tong(int a, int b){return a + b;}
int tong(int a, int b, int c){return a + b + c;}
}
+ hoTen: String
+ diemTB: double
+ xepLoai(): String
+ xuat(): void
DEMO
+ nhap(): void
+ SinhVien()
+ SinhVien(hoTen, diemTB)
• Lab 4 – bài 1
• Lab 4 – bài 2
Package
• Packages are used to divide classes and
interfaces into different packages.
• This is similar to managing files on the drive in that
class (file) and package (folder)
• The following example creates class MyClass in
package com.poly
package com.poly;
public class MyClass{…}
extends
}
use
use
package p1; package p2; package p3;
public class B{ public class C{ public class D extends A{
A x = new A(); A x = new A(); void method(){
void method(){ void method(){ a = 1;
x.a = 1; x.a = 1; b = 1;
x.b = 1; x.b = 1; c = 1;
x.c = 1; x.c = 1; d = 1;
x.d = 1; x.d = 1; }
} } }
} }
Encapsulation
Code: String
name: String fields
bYear: int
address: String
//default constructor
public Student(){
code=“SE123”;
name=“Hieu”;
bYear= 2000;
address=“1 Ba Trieu , HN”.
}
//constructor with parameters
public Student(String code, String name, int bYear, String address){
this.code=code;
this.name=name;
this.bYear= year;
this.address=address.
}
The current object: this
• For example:
public String getName(){
return name;
}
public void setName(String name){
if(! name.isEmpty())
this.name=name;
}
Member functions:
other methods
• For example:
public void input(){
//code here
}
public void output(){
//code here
}
Passing Arguments a Constructor/Method
• Create an object
ClassName obj1=new ClassName();
ClassName obj2=new ClassName(params);
• Accessing a field of the object
object.field
• Calling a method of an object
object.method(params)
Demo: If we do not implement any
constructor, compiler will insert to the class a
system default constructor
(2) Setup
values
y 0
100 x 0
(1) Memory
allocation
interface
package/ subclass
protected
class
outside cannot access
private
members of
interface/class
package
no specifier
(default)
Interface is a group of prototyped
methods and they will be
Note: If you don't use any modifier, it is implemented in a class afterward.
treated as default by default. It will be introduced later.
Access Modifiers
Car
- Colour: String
- EnginePower: int
- Convertible: boolean
- parkingBrake: boolean
+Car()
+Car(String, int, boolean, boolean )
+pressStartButton():void
+pressAcceleratorButton(): void
+ getColour(): String
+ setColour(String): void
…
Implement
Implement
Implement
Summary
• The anatomy of a class, and how to declare
fields, methods, and constructors.
• Hints for class design:
• Main noun Class
• Descriptive nouns Fields
• Methods: Constructors, Getters, Setters, Normal methods
• Creating and using objects.
• To instantiate an object: Using appropriate
constructors
• Use the dot operator to access the object's
instance variables and methods.