SlideShare una empresa de Scribd logo
1 de 9
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado

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
 

Destacado (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
 

Similar 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
 

Similar 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
 
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
 
Opp compile
Opp compileOpp compile
Opp compile
 

Último

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 

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