CSc 110, Autumn 2016 Lecture 31: Encapsulation

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Encapsulation, this, Subclasses.
1 Building Java Programs Chapter 8: Classes These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Unit 8 Classes and Objects; Inheritance Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 19: encapsulation, inheritance reading: (Slides adapted from Stuart.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2009 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Adapted from slides by Marty Stepp and Stuart Reges
Programming Abstractions
Object Oriented Programming
OOP: Encapsulation &Abstraction
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Classes and Objects; Inheritance
Classes and Objects – digging deeper
Lecture 11: More on Classes
Adapted from slides by Marty Stepp and Stuart Reges
Classes and Objects, State and Behavior
Lecture 8-3: Encapsulation, this
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Object initialization: constructors
Topic 28 classes and objects, part 2
Building Java Programs
Week 8 Classes and Objects
Topic 29 classes and objects, part 3
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Classes, Objects and lists
Classes and objects Readings: 8.1.
Building Java Programs
Lecture 12: Classes II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 12: 2D Arrays AP Computer Science Principles
Building Java Programs
Building Java Programs
Lecture 11: Intro to Classes
Adapted from slides by Marty Stepp and Stuart Reges
Kinds of methods accessor: A method that lets clients examine object state. Examples: distance, distanceFromOrigin often has a non-void return type mutator: A.
Building Java Programs
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topic 29 classes and objects, part 3
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Topic 29 classes and objects, part 3
Presentation transcript:

CSc 110, Autumn 2016 Lecture 31: Encapsulation Adapted from slides by Marty Stepp and Stuart Reges

Abstraction Don't need to know this Can focus on this!!

Encapsulation encapsulation: Hiding implementation details of an object from its clients. Encapsulation provides abstraction. separates external view (behavior) from internal view (state) Encapsulation protects the integrity of an object's data.

Private fields A field can be made invisible to outsiders No code outside the class can access or change it easily. __name Examples: self.__id self.__name Client code sees an error when accessing private fields:

Accessing private state We can provide methods to get and/or set a field's value: # A "read-only" access to the __x field ("accessor") def get_x(self): return self.__x # Allows clients to change the __x field ("mutator") def set_x(self, new_x): self.__x = new_x Client code will look more like this: print("p1: (" + str(p1.get_x()) + ", " + str(p1.get_y()) + ")") p1.set_x(14)

Benefits of encapsulation Provides abstraction between an object and its clients. Protects an object from unwanted access by clients. A bank app forbids a client to change an Account's balance. Allows you to change the class implementation. Point could be rewritten to use polar coordinates (radius r, angle θ), but with the same methods. Allows you to constrain objects' state (invariants). Example: Only allow Points with non-negative coordinates.

Point class, version 4 # A Point object represents an (x, y) location. class Point: self.__x self.__y def __init__(self, initial_x, initial_y): self.__x = initial_x self.__y = initial_y def distance_from_origin(self): return sqrt(self.__x * self.__x + self.__y * self.__y) def get_x(self): return self.__x def get_y(self): return self.__y def set_location(self, new_x, new_y): self.__x = new_x self.__y = new_y def translate(self, dx, dy): self.__x = self.__x + dx self.__y = self.__y + dy

Client code, version 4 def main9): # create two Point objects p1 = Point(5, 2) p2 = Point(4, 3) # print each point print("p1: (" + str(p1.get_x()) + ", " + str(p1.get_y()) + ")") print("p2: (" + str(p2.get_x()) + ", " + str(p2.get_y()) + ")") # move p2 and then print it again p2.translate(2, 4) OUTPUT: p1 is (5, 2) p2 is (4, 3) p2 is (6, 7)