COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

Similar documents
Defining Classes and Methods

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

5. Defining Classes and Methods

Defining Classes and Methods

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Defining Classes and Methods

Defining Classes and Methods

Principles of Object Oriented Programming. Lecture 4

Methods and Data (Savitch, Chapter 5)

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

by Pearson Education, Inc. All Rights Reserved. 2

Programming II (CS300)

Classes. Classes as Code Libraries. Classes as Data Structures

Programming II (CS300)

Methods. Methods. Mysteries Revealed

5. Defining Classes and Methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Darrell Bethea May 20, 2011

Tutorial 6 CSC 201. Java Programming Concepts مبادئ الربجمة باستخدام اجلافا

AP COMPUTER SCIENCE A

Chapter 4 Defining Classes I

Dot and Scope Resolution Operator

CS1150 Principles of Computer Science Objects and Classes

CSC 1214: Object-Oriented Programming

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

BBM 102 Introduc0on to Programming II Spring 2014

ECE 122. Engineering Problem Solving with Java

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

What is an algorithm?

What is an algorithm?

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

Implementing non-static features

Getter and Setter Methods

Encapsulation in Java

CSCE 156 Computer Science II

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

Week 5-1: ADT Design

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CSI33 Data Structures

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Lecture #1. Introduction to Classes and Objects

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CompSci 125 Lecture 07. Chapter 4: Object-Oriented Development

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Object Oriented Programming (II)

How to evaluate a design? How to improve on it? When to stop evaluating and improving?

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

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

Defining Classes and Methods

Lecture 7. Log into Linux New documents posted to course webpage

Chapter 5: Classes and Objects in Depth. Information Hiding

Friend Functions, Inheritance

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Programming II (CS300)

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

1 (5 marks) 2 (6 marks) 3 (6 marks)

System.out.print(); Scanner.nextLine(); String.compareTo();

CS-202 Introduction to Object Oriented Programming

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Objects. say something to express one's disapproval of or disagreement with something.

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

A First Object. We still have another problem. How can we actually make use of the class s data?

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS24 Week 3 Lecture 1

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

System Design and Programming II

C++ Inheritance and Encapsulation

COMP Introduction to Programming Midterm Review

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Encapsulation. Mason Vail Boise State University Computer Science

Lecture 7 Objects and Classes

Designing Classes. Appendix D. Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

CS 2530 INTERMEDIATE COMPUTING

Chapter 6 Class and Method

Programming by Delegation

Loop - 3 & Class. Cheng, Wei COMP May 22, Title

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

Midterms Save the Dates!

Software and Programming 1

Scala. Introduction. Scala

CSCI 161 Introduction to Computer Science

Programming in the Large II: Objects and Classes (Part 1)

Reviewing OO Concepts

G52CPP C++ Programming Lecture 9

Objects as a programming concept

CS100J Prelim I, 29 Sept. 2003

Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

Transcription:

COMP 110-001 Information Hiding and Encapsulation Yi Hong June 03, 2015

Review of Pass-By-Value What is the output? public void swap(student s1, Student s2) Student s3 = s1; s1 = s2; s2 = s3; Student berkeley = new Student(); Student brett = new Student(); berkeley.setyear(2); brett.setyear(3); swap(berkeley, brett); System.out.println(berkeley.year);

Review of Pass-By-Value What is the output? public void swapyear(student s1, Student s2) int year = s1.year; s1.year = s2.year; s2.year = year; Student berkeley = new Student(); Student brett = new Student(); berkeley.setyear(2); brett.setyear(3); swapyear(berkeley, brett); System.out.println(berkeley.year);

Today Public / private Information hiding and encapsulation

public/private Modifier public void setmajor() public int classyear; public: there is no restriction on how you can use the method or instance variable Any class can use a public class, method, or instance variable

public/private Modifier private void setmajor() private int classyear; private: can not directly use the method or instance variable s name outside the class

Example public class Student public int classyear; private String major; Student jack = new Student(); jack.classyear = 1; jack.major = Computer Science ; OK, classyear is public Error!!! major is private

More About private Hides instance variables and methods inside the class/object. The private variables and methods are still there, holding data for the object. Invisible to external users of the class Users cannot access private class members directly Information hiding

Instance Variables Should Be private Private instance variables are accessible by name only within their own class Force users of the class to access instance variables only through methods Gives you control of how programmers use your class Why is this important?

Example: Rectangle public class Rectangle public int width; public int height; public int area; public void setdimensions( int newwidth, int newheight) width = newwidth; height = newheight; area = width * height; public int getarea() return area; Rectangle box = new Rectangle(); box.setdimensions(10, 5); System.out.println(box.getArea()); // Output: 50 box.width = 6; System.out.println(box.getArea()); // Output: 50, but wrong answer!

Instance Variables Should Be Private Public instance variables can lead to the corruption of an object s data, inconsistent data within an object Private instance variables enable the class to restrict how they are accessed or changed Always make instance variables private

Accessors and Mutators How do you access private instance variables? Accessor methods (a.k.a. get methods, getters) A public method that allows you to look at data in an instance variable Typically begin with get Mutator methods (a.k.a. set methods, setters) A public method that allows you to change data in an instance variable Typically begin with set

Example: Student public class Student private String name; private int age; public void setname(string studentname) name = studentname; public void setage(int studentage) age = studentage; Mutators public String getname() return name; public int getage() return age; Accessors

Okay, But Why Making Methods private? Helping methods that will only be used from inside a class should be private External users have no need to call these methods Encapsulation Groups instance variables and methods into a class Hides implementation details, and separates the what from the how

Example: Driving a Car Accelerate with the accelerator pedal Decelerate with the brake pedal Steer with the steering wheel Does not matter if: You are driving a gasoline engine car or a hybrid engine car You have a 4-cylinder engine or a 6-cylinder engine You still drive the same way

Encapsulation The interface is the same The underlying implementation may be different A programmer who uses a method (interface) should need only know what the method does, not how it does it

Encapsulation in Classes A class interface tells programmers all they need to know to use the class in a program A class interface describes the class s public view The implementation of a class consists of the private elements of the class definition, hidden from public view private instance variables and constants private methods bodies of public methods

Example: Two Implementations of Rectangle public class Rectangle private int width; private int height; private int area; public void setdimensions( int newwidth, int newheight) width = newwidth; height = newheight; area = width * height; public int getarea() return area; public class Rectangle private int width; private int height; public void setdimensions( int newwidth, int newheight) width = newwidth; height = newheight; public int getarea() return width * height;

Encapsulation Implementation should not affect behavior described by interface Two classes can have the same behavior but different implementations

A Well-Encapsulated Class Definition Imagine a wall between interface and implementation Class Definition Implementation: Private instance variables Private constants Private methods Bodies of public methods Interface: Comments Headings of public methods Public named constants Programmer who uses the class

Comments Before Method s Definition Precondition: states a method s requirements Everything that needs to be true before the method is invoked Postcondition: states a method s effect Tells what will be true after the method is executed in a situation in which the precondition holds For a method that returns a value, the postcondition will include a description of the value returned by the method

Encapsulation Guidelines Comments before class definition that describes how the programmer should think about the class data and methods Instance variables are private Provide public accessor and mutator methods Pre and post comments before methods Make any helping methods private Write comments within the class definition to describe implementation details A good rule: /* * */ style for class interface comments, and the // style for implementation comments

Summary of Encapsulation The process of hiding all the details of how a piece of software works and describing only enough about the software to enable a programmer to use it Data and actions are combined into a single item, a class object that hides the details of the implementation

Next Class Constructors and static methods