SlideShare uma empresa Scribd logo
1 de 9
Baixar para ler offline
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
 
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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.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
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
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
 
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...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
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
 
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...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
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
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
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