Encapsulation in Java

Similar documents
Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Programming by Delegation

EECS2030 Fall 2016 Preparation Exercise for Lab Test 2: A Birthday Book

Documenting, Using, and Testing Utility Classes

Classes and Objects. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

Dot and Scope Resolution Operator

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

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

Principles of Object Oriented Programming. Lecture 4

COMP 401 INTERFACES. Instructor: Prasun Dewan

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Classes. Classes as Code Libraries. Classes as Data Structures

Chapter 4. Defining Classes I

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

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.

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

Software and Programming 1

Object-Oriented Programming in Java

Chapter 4 Defining Classes I

ECE 122. Engineering Problem Solving with Java

Motivating Example: Two Types of Errors (2) Test-Driven Development (TDD) with JUnit. Motivating Example: Two Types of Errors (1)

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

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

Test-Driven Development (TDD) with JUnit

Methods and Data (Savitch, Chapter 5)

Object Oriented Programming

Using API in Java. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

What will this print?

Object Oriented Programming

Programming II (CS300)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Welcome to the Using Objects lab!

Software and Programming 1

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

Software and Programming 1

Programming II (CS300)

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

This exam is open book. Each question is worth 3 points.

Programming II (CS300)

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Example: Fibonacci Numbers

Software and Programming 1

The Object Oriented Paradigm

Visibility: Project, Packages, Classes. Documenting, Using, and Testing Utility Classes. Visibility of Classes

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Objects as a programming concept

EECS168 Exam 3 Review

CMPS 134: Computer Science I Fall 2011 Test #1 October 5 Name Dr. McCloskey

CS1004: Intro to CS in Java, Spring 2005

CS 302 Week 9. Jim Williams

CS1083 Week 2: Arrays, ArrayList

Chapter 6 Lab Classes and Objects

CS Programming I: Classes

TaxiBot New attributes Variables Math! TaxiBot

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

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

Final Examination Semester 2 / Year 2012

Welcome to the Using Objects lab!

Data Structures Lecture 1

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

ENCAPSULATION AND POLYMORPHISM

Overview of Eclipse Lectures. Module Road Map

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

Classes and Methods: Classes

1.00 Lecture 6. Java Methods

COMP 102: Test August, 2017

Chapter 5: Classes and Objects in Depth. Information Hiding

What This Course Is About Design-by-Contract (DbC)

coe318 Lab 1 Introduction to Netbeans and Java

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Imperative Languages!

Recursion 1. Recursion is the process of defining something in terms of itself.

Lecture 12: Classes II

Object-Oriented Programming

Methods. Methods. Mysteries Revealed

CS 251 Intermediate Programming Methods and Classes

Syntax of Eiffel: a Brief Overview

Chapter 2: Java OOP I

Commands, and Queries, and Features. Syntax of Eiffel: a Brief Overview. Escape Sequences. Naming Conventions

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Chapter 4: Writing Classes

13. Java Classes. Educational Objectives. Classes - Technical. Definition: Classes

Chapter 10. Object-Oriented Thinking

Introduction to Classes and Objects. David Greenstein Monta Vista High School

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

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

10. Java Classes. Classes - Technical. Example: Earthquake catalog. Classes - Conceptual

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

Chapter 6 Lab Classes and Objects

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

COMP 401 USER INTERFACE AND ANNOTATIONS. Instructor: Prasun Dewan

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

CSCE 156 Computer Science II

Transcription:

Encapsulation in Java EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Encapsulation (1.1) Consider the following problem: A person has a name, a weight, and a height. A person s weight may be in kilograms or pounds. A person s height may be in meters or inches. A person s BMI is calculated using their height in meters and weight in kilograms. Consider a first solution: class Person { public String name; public double weight; /* in kilograms */ public double height; /* in meters */ public double getbmi() { return weight / (height * height); } } Since both attributes height and weight are declared as public, we do not need the setter methods for them. 2 of 9

Encapsulation (1.2) Say an application of the Person class mistakenly thinks that the height in inches and weight in pounds should be set: 1 class BMICalculator { 2 public static void main(string args[]) { 3 Person jim = new Person(); 4 /* Jim s height and weight are 1.78 m and 85 kg */ 5 jim.weight = 85 * 2.2; 6 jim.height = 1.78 * 39; 7 System.out.println(jim.getBMI()); 8 } } 85 2.2 Line 7: = 0.038, rather than 85 = 26.827!!! (1.78 39) 2 1.78 2 Solution: Disallow any application class of Person to directly assign to weight and height. Provide proper setter methods as the only means for assigning these two attributes. 3 of 9

Encapsulation (2.1) Now consider a better solution: class Person { public String name; private double weight; /* in kilograms */ private double height; /* in meters */ public void setweightinkilograms(double k) { weight = k; } public void setweightinpounds(double p) { weight = p / 2.2; } public void setheightinmeters(double m) { height = m; } public void setheightininches(double i) { height = i / 39; } public double getbmi() { return weight / (height * height); } } Exercise: Modify the Person class so that weight is measured in pounds and hegiht is measured in inches. 4 of 9

Encapsulation (2.2) Now an application of the Person class may only set the weight and height of a person by calling the appropriate methods: 1 class BMICalculator { 2 public static void main(string args[]) { 3 Person jim = new Person(); 4 /* Jim s height and weight are 1.78 m and 85 kg */ 5 jim.setweightinpounds(85 * 2.2); 6 jim.setheightininches(1.78 * 39); 7 System.out.println(jim.getBMI()); 8 } } Since both attributes weight and height in class Person are declared as private, it is disallowed in any other class (e.g., BMICalculator) to access them (e.g., jim.weight). Line 7 now should return the correct BMI value. 5 of 9

Encapsulation (3.1) Question : What if in the Person class, we want the weight attribute to mean pounds and height to mean inches? Hint: Which classes will you have to change? Person? BMICalculator? Both? Modify the setter methods in Person accordingly. [Exercise!] No change is needed in the BMICalculator! Since class BMICalculator was disallowed to access weight and height, as soon as the setter definitions are modified in Person, the calculation will still work! What we have achieved: Implementation details in Person (i.e., weight and height) are hidden from all potential applications (e.g., BMICalculator). When these implementation details are changed in Person (e.g., weight interpreted in pounds rather than in kilograms): Only the Person class has to be changed. All existing application classes can remain unchanged. 6 of 9

Encapsulation (3.2) A software component hides the internal details of its implementation, so that: It has a stable interface; Programmers of other components can only depend on its public interface, rather than writing code that depends on those implementation decisions ; The component developer may change the implementation without affecting the code of any other components. In Java, we achieve this by declaring attributes or helper methods as private; providing public accessors or mutators. 7 of 9

Encapsulation (3.3) Follow this tutorial video: https: //www.youtube.com/watch?v=d2q-uasrmau&index=1& list=pl5dxamcmjv_492h1b0yizsyhc3imeetlv For complete details about controlling the access for attributes, refer to: https://docs.oracle.com/javase/tutorial/java/ javaoo/accesscontrol.html 8 of 9

Index (1) Encapsulation (1.1) Encapsulation (1.2) Encapsulation (2.1) Encapsulation (2.2) Encapsulation (3.1) Encapsulation (3.2) Encapsulation (3.3) 9 of 9