SlideShare uma empresa Scribd logo
Encapsulation

Monday, October 14, 13
Encapsulation
• encapsulation is hiding or protecting the implementation details
of an object from users of the object
• this creates an abstraction which allows us to focus on essential
properties rather than inner details

2
Monday, October 14, 13
Private fields
• To encapsulate the fields of an object, we make them private:
public class Point {
private int x;
private int y;
}

• Only visible within the Point class, but not anywhere else

3
Monday, October 14, 13
Accessors
• We need a way for external code to access x and y:
// can’t access private vars
System.out.println("p1 is (" + p1.x + ", " + p1.y + ")");
// Point class, with *accessors*
public class Point {
private int x;
private int y;
public int
return
}
public int
return
}
}
Monday, October 14, 13

getX() {
x;
getY() {
y;
4
Accessors
• Write a program called PointMain that creates a new Point at
(3, 5) and prints a description of the Point
public class Point {
private int x;
private int y;
public Point(int x, int y) {
self.x = x;
self.y = y;
}
public int
return
}
public int
return
}
}
Monday, October 14, 13

getX() {
x;
getY() {
y;
5
Accessors
public class PointMain {
public static void main(String[] args) {
Point p = new Point(3, 5);
System.out.println("p is (" + p.getX() + ", " + p.getY() + ")");
}
}

6
Monday, October 14, 13
Mutators
• Write a method for Point called setLocation that takes two
integers and sets the x and y coordinates of the Point
public class Point {
private int x;
private int y;
public Point(int x, int y) {
self.x = x;
self.y = y;
}
public int
return
}
public int
return
}
}
Monday, October 14, 13

getX() {
x;
getY() {
y;
7
Mutators
public class Point {
private int x;
private int y;
public Point(int x, int y) {
self.x = x;
self.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setLocation(int newX, int newY) {
x = newX;
y = newY;
}
}
Monday, October 14, 13

8
Lab!
• See https://dl.dropboxusercontent.com/u/20418505/Labs/
M2.W3-0.txt

9
Monday, October 14, 13

Mais conteúdo relacionado

Mais procurados

16858 memory management2
16858 memory management216858 memory management2
16858 memory management2
Aanand Singh
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]
Palak Sanghani
 

Mais procurados (17)

Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
Arrays
ArraysArrays
Arrays
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 
Review constdestr
Review constdestrReview constdestr
Review constdestr
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
Software Design Thinking
Software Design ThinkingSoftware Design Thinking
Software Design Thinking
 
Hash tables
Hash tablesHash tables
Hash tables
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 

Destaque

2 m3.w1.d2 - interfaces
2   m3.w1.d2 - interfaces2   m3.w1.d2 - interfaces
2 m3.w1.d2 - interfaces
Justin Chen
 
2 m2.w2.d1 - oop
2   m2.w2.d1 - oop2   m2.w2.d1 - oop
2 m2.w2.d1 - oop
Justin Chen
 
m1.w4.d2 - parameters
m1.w4.d2 - parametersm1.w4.d2 - parameters
m1.w4.d2 - parameters
Justin Chen
 
2 m2.w5.d1 - superclass
2   m2.w5.d1 - superclass2   m2.w5.d1 - superclass
2 m2.w5.d1 - superclass
Justin Chen
 
2 m2.w4.d1 - inheritance
2   m2.w4.d1 - inheritance2   m2.w4.d1 - inheritance
2 m2.w4.d1 - inheritance
Justin Chen
 
bahan ajar materi bilangan bulat kelas 7
bahan ajar materi bilangan bulat kelas 7bahan ajar materi bilangan bulat kelas 7
bahan ajar materi bilangan bulat kelas 7
renatrisea
 

Destaque (12)

A success-to-be-shared
A success-to-be-sharedA success-to-be-shared
A success-to-be-shared
 
2 m3.w1.d2 - interfaces
2   m3.w1.d2 - interfaces2   m3.w1.d2 - interfaces
2 m3.w1.d2 - interfaces
 
2 m2.w2.d1 - oop
2   m2.w2.d1 - oop2   m2.w2.d1 - oop
2 m2.w2.d1 - oop
 
m1.w4.d2 - parameters
m1.w4.d2 - parametersm1.w4.d2 - parameters
m1.w4.d2 - parameters
 
Subir prof tapia 2
Subir prof tapia 2Subir prof tapia 2
Subir prof tapia 2
 
2 m2.w5.d1 - superclass
2   m2.w5.d1 - superclass2   m2.w5.d1 - superclass
2 m2.w5.d1 - superclass
 
Adán y Eva un matrimonio ejemplar
Adán y Eva un matrimonio ejemplarAdán y Eva un matrimonio ejemplar
Adán y Eva un matrimonio ejemplar
 
Architecting for Enterprise with JavaScript
Architecting for Enterprise with JavaScriptArchitecting for Enterprise with JavaScript
Architecting for Enterprise with JavaScript
 
EL CRISTIANO Y LAS AFLICCIONES
EL CRISTIANO Y LAS AFLICCIONESEL CRISTIANO Y LAS AFLICCIONES
EL CRISTIANO Y LAS AFLICCIONES
 
Luas dan volum tabung
Luas dan volum tabungLuas dan volum tabung
Luas dan volum tabung
 
2 m2.w4.d1 - inheritance
2   m2.w4.d1 - inheritance2   m2.w4.d1 - inheritance
2 m2.w4.d1 - inheritance
 
bahan ajar materi bilangan bulat kelas 7
bahan ajar materi bilangan bulat kelas 7bahan ajar materi bilangan bulat kelas 7
bahan ajar materi bilangan bulat kelas 7
 

Semelhante a 2 m2.w3.d1 - encapsulation

#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
Hadziq Fabroyir
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
Paulo Morgado
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
MaruMengesha
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
I am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdfI am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdf
eyewaregallery
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
Kumar
 

Semelhante a 2 m2.w3.d1 - encapsulation (20)

Classes and object
Classes and objectClasses and object
Classes and object
 
w10 (1).ppt
w10 (1).pptw10 (1).ppt
w10 (1).ppt
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
Unit 2
Unit 2Unit 2
Unit 2
 
Les nouveautés de C# 6
Les nouveautés de C# 6Les nouveautés de C# 6
Les nouveautés de C# 6
 
namespace ConsoleApplication15 { class Program { static void Main(stri.docx
namespace ConsoleApplication15 { class Program { static void Main(stri.docxnamespace ConsoleApplication15 { class Program { static void Main(stri.docx
namespace ConsoleApplication15 { class Program { static void Main(stri.docx
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
 
class and objects
class and objectsclass and objects
class and objects
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
 
662305 10
662305 10662305 10
662305 10
 
I am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdfI am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdf
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
 
Design pattern - part 3
Design pattern - part 3Design pattern - part 3
Design pattern - part 3
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 

Último

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Último (20)

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 

2 m2.w3.d1 - encapsulation

  • 2. Encapsulation • encapsulation is hiding or protecting the implementation details of an object from users of the object • this creates an abstraction which allows us to focus on essential properties rather than inner details 2 Monday, October 14, 13
  • 3. Private fields • To encapsulate the fields of an object, we make them private: public class Point { private int x; private int y; } • Only visible within the Point class, but not anywhere else 3 Monday, October 14, 13
  • 4. Accessors • We need a way for external code to access x and y: // can’t access private vars System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); // Point class, with *accessors* public class Point { private int x; private int y; public int return } public int return } } Monday, October 14, 13 getX() { x; getY() { y; 4
  • 5. Accessors • Write a program called PointMain that creates a new Point at (3, 5) and prints a description of the Point public class Point { private int x; private int y; public Point(int x, int y) { self.x = x; self.y = y; } public int return } public int return } } Monday, October 14, 13 getX() { x; getY() { y; 5
  • 6. Accessors public class PointMain { public static void main(String[] args) { Point p = new Point(3, 5); System.out.println("p is (" + p.getX() + ", " + p.getY() + ")"); } } 6 Monday, October 14, 13
  • 7. Mutators • Write a method for Point called setLocation that takes two integers and sets the x and y coordinates of the Point public class Point { private int x; private int y; public Point(int x, int y) { self.x = x; self.y = y; } public int return } public int return } } Monday, October 14, 13 getX() { x; getY() { y; 7
  • 8. Mutators public class Point { private int x; private int y; public Point(int x, int y) { self.x = x; self.y = y; } public int getX() { return x; } public int getY() { return y; } public void setLocation(int newX, int newY) { x = newX; y = newY; } } Monday, October 14, 13 8