Encapsulation. Mason Vail Boise State University Computer Science

Similar documents
ECE 122. Engineering Problem Solving with Java

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

$ % $ % BankAccount. public class BankAccount {... private??? balance; } "commands, transformers, mutators! "abstract state! "queries, accessors!

Anatomy of a Class Encapsulation Anatomy of a Method

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Designing Classes part 2

CS112 Lecture: Defining Instantiable Classes

Chapter 4: Writing Classes

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Classes and Methods: Classes

Defensive Programming

What will this print?

Practice problem on defining and using Class types. Part 4.

CS1004: Intro to CS in Java, Spring 2005

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Handout 8 Classes and Objects Continued: Static Variables and Constants.

BankAccount account1 = new BankAccount(...);,this

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Where do objects come from? Good question!

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Principles of Object Oriented Programming. Lecture 4

CPS122 Lecture: Defining a Class

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

ICOM 4015: Advanced Programming

Programming II (CS300)

Implementation. (Mapping to Java) Jörg Kienzle & Alfred Strohmeier. COMP-533 Implementation

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

Programming II (CS300)

Implementing Classes (P1 2006/2007)

Software Design and Analysis for Engineers

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Inheritance and Subclasses

Java Puzzle Ball MOOC Lab 4: Lambda Expressions

Implementing Classes

STUDENT LESSON A5 Designing and Using Classes

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Data types. CISC 1600/1610 Computer Science I. Class dog. Introducing: classes. Class syntax declaration. Class syntax function definitions 12/2/2015

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

CSCE 156 Computer Science II

Handout 9 OO Inheritance.

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Building Java Programs

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Programming a Bank Database. We ll store the information in two tables: INTEGER DECIMAL(10, 2)

SWC test question #01. Using objects part I

EXAMINATIONS 2012 END-OF-YEAR SWEN222. Software Design. Question Topic Marks 1. Design Quality Design Patterns Design by Contract 12

CSC 222: Object-Oriented Programming. Fall Inheritance & polymorphism

Chapter 4 Defining Classes I

The Object Oriented Paradigm

Creating and Using Objects

EEE-425 Programming Languages (2013) 1

CLASSES AND OBJECTS IN JAVA

CMSC 202. Exceptions

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

ECE 122. Engineering Problem Solving with Java

CS 1302 Chapter 9 (Review) Object & Classes

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

EEE-425 Programming Languages (2013) 1

Imperative Languages!

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Container Vs. Definition Classes. Container Class

Object-Oriented Programming (Java)

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Programming Abstractions

CHAPTER 10 INHERITANCE

Methods and Data (Savitch, Chapter 5)

Last lecture. Lecture 9. in a nutshell. in a nutshell 2. Example of encapsulation. Example of encapsulation. Class test. Procedural Programming

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Ticket Machine Project(s)

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

Intro to Computer Science 2. Inheritance

Introduction to Objects. James Brucker

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Today s Agenda. Quick Review

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

Java Puzzle Ball Nick Ristuccia

Quarter 1 Practice Exam

Design-by-Contract (Dbc) Test-Driven Development (TDD)

BloomBank Financial Software Design

Chapter 5: Classes and Objects in Depth. Information Hiding

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Programming II (CS300)

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

Practice Midterm Examination

Transcription:

Encapsulation Mason Vail Boise State University Computer Science

Pillars of Object-Oriented Programming Encapsulation Inheritance Polymorphism Abstraction (sometimes)

Object Identity Data (variables) make one object different from another. An object s data make up its identity. Examples: Student: name, ID, address, major, GPA, etc. Book: title, author, copyright, edition, etc. BankAccount: accountnumber, owner, balance, etc.

Need for Protecting Data Imagine your bank account is represented as an object in the bank s software. There are specific valid ways that your account balance can be modified: deposits, withdrawals, fees, and interest. Any other modification to an account balance other than through those transactions should not be allowed. for(account acc : accounts) { acc.balance = 0;

Encapsulation: Protecting Data through Visibility Is it possible to guarantee that data can only be affected by valid operations? Yes. Encapsulation is the concept of protecting an object s data so that it can only be modified through acceptable, valid operations. Encapsulation is enforced through use of visibility modifiers - keywords that specify where data and operations can be seen and used. In Java, the primary distinction is between public and private visibility.

Encapsulation: Protecting Data through Visibility Visibility modifiers should be explicitly applied to all instance (and class) variables, constants, and methods. Visibility of all variables and all internal support methods should be private. private long accountnumber; private String accountowner; private double balance; Only methods intended to be called by outside users should be public. public void deposit(double amttoadd) { public double getbalance() { Access to private data, then, will be indirect, through public methods only.

Visibility and Scope Variables, constants, or methods declared as private can be seen and used anywhere inside the scope of the class - in any code within the class. No code outside the class, however, can see or use anything declared as private for that class. Anything declared public can be seen and used in code inside or outside the class. This is why it is important that a class only exposes a limited set of appropriate methods to the outside world. Public methods make up the public interface of an object of the class type, through which other code will interact with the object.

Accessor ( getter ) and Mutator ( setter ) Methods Methods that return the current value of an instance variable or update an instance variable value are common. A method that simply returns a current value is called an accessor, or getter method. A method that directly updates a current value is called a mutator, or setter method. public int getsize() { return size; public void setsize(int newsize) { size = newsize; When writing a class, you only need to provide accessors or mutators for the variables you want a user to be able to view or update. Mutator methods often contain logic to validate any requested changes. For example, a mutator could confirm that a new value is within a required range.

Simple Encapsulated Class: BankAccount (part 1) public class BankAccount { //all instance variables are private private long acctnumber; //accessor below, but no mutator private String acctowner; //accessor below, but no mutator private double acctbalance; //accessor below, changes via deposit() and withdraw() //constructor initializes instance variables public BankAccount(long acctnumber, String acctowner) { this.acctnumber = acctnumber; this.acctowner = acctowner; This.balance = 0.0;...

Simple Encapsulated Class: BankAccount (part 2) public class BankAccount {... //public accessor methods return current values of instance variables public long getacctnumber() { return acctnumber; public String getacctowner() { return acctowner; public double getacctbalance() { return acctbalance;...

Simple Encapsulated Class: BankAccount (part 3) public class BankAccount {... public void deposit(double amttoadd) { //update only if valid input if (amttoadd > 0) { acctbalance += amttoadd; else { throw new InvalidTransactionException(); public void withdraw(double amttowithdraw) { //update only if valid input if (amttowithdraw > 0 && acctbalance >= amttowithdraw) { acctbalance -= amttowithdraw; else { throw new InvalidTransactionException();

Encapsulation Mason Vail Boise State University Computer Science