More on Methods and Encapsulation

Similar documents
Encapsulation 10/26/2015. Department of Computer Science & Information Engineering. National Taiwan University

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

Basic Java OOP 10/12/2015. Department of Computer Science & Information Engineering. National Taiwan University

Class versus Instance (Section 5.1)

Instance Methods and Inheritance (1/2)

Generics. Hsuan-Tien Lin. OOP Class, May 23, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Generics OOP 05/23/ / 16

ITI Introduction to Computing II

Inheritance. Hsuan-Tien Lin. OOP Class, March 29-30, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Inheritance OOP 03/29-30/ / 41

ITI Introduction to Computing II

Polymorphism 11/09/2015. Department of Computer Science & Information Engineering. National Taiwan University

Array 10/26/2015. Department of Computer Science & Information Engineering. National Taiwan University

The Midterm Exam. Hsuan-Tien Lin. Department of CSIE, NTU. OOP Class, April 26-27, 2010

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Inheritance 11/02/2015. Department of Computer Science & Information Engineering. National Taiwan University

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

1 Java Night Countdown (30%)

CS 6110 Lecture 28 Subtype Polymorphism 3 April 2013 Lecturer: Andrew Myers

Python. chrysn

UML. Design Principles.

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

Programming Languages Fall 2013

Computer Science Introductory Course MSc - Introduction to Java

Design Patterns and Refactoring

Turing Machines Part Three

Lecture 5: Jun. 10, 2015

Extensibility Patterns: Extension Access

Course Announcements. Bacon is due next Monday. Next lab is about drawing UIs. Today s lecture will help thinking about your DB interface.

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

COMP 204. Object Oriented Programming (OOP) Mathieu Blanchette

Compiling Techniques

Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

6.001 Recitation 22: Streams

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

Introduction to ASM - ByteCode Manipulating Tool

Introduction to Programming (Java) 3/12

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Thread Specific Storage

Elementary Sorts 1 / 18

PHP-Einführung - Lesson 4 - Object Oriented Programming. Alexander Lichter June 27, 2017

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

arxiv: v1 [cs.se] 10 Jan 2018

Machine Learning Techniques

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Lab Course: distributed data analytics

Lecture 20: conp and Friends, Oracles in Complexity Theory

Constructors - Cont. must be distinguished by the number or type of their arguments.

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 09/06/2014 SESSION 8:30-10:30

Introduction to Estimation Theory

Programsystemkonstruktion med C++: Övning 1. Karl Palmskog Translated by: Siavash Soleimanifard. September 2011

Notes from Yesterday s Discussion. Big Picture. CIS 500 Software Foundations Fall November 1. Some lessons.

Scripting Languages Fast development, extensible programs

Lists, Stacks, and Queues (plus Priority Queues)

Cryptographic Hash Functions

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

This chapter covers asymptotic analysis of function growth and big-o notation.

Prioritized Garbage Collection Using the Garbage Collector to Support Caching

CS5314 Randomized Algorithms. Lecture 15: Balls, Bins, Random Graphs (Hashing)

JChartLib ver 1.0. Silvio Schneider, June 7, Introduction 3. 2 Chart and Dataset 4. 3 Adding data to a chart 4

Introduction to Python

Lecture 14: Nov. 11 & 13

CMSC 441: Algorithms. NP Completeness

Notes 6: Multivariate regression ECO 231W - Undergraduate Econometrics

Pysynphot: A Python Re Implementation of a Legacy App in Astronomy

Lecture 4: Stacks and Queues

2 - Strings and Binomial Coefficients

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis

Lecture 27: Theory of Computation. Marvin Zhang 08/08/2016

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Supporting Information. Kekule.js: An Open Source JavaScript Chemoinformatics Toolkit

Analysis of Algorithms

Techniques of Java Programming

Machine Learning Foundations

CSE370: Introduction to Digital Design

Presentation Outline: Haunted Places in North America

A Short Introduction to Hoare Logic

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

R: A Quick Reference

Modern Functional Programming and Actors With Scala and Akka

Machine Learning Foundations

Introduction to functions

16.1 Countability. CS125 Lecture 16 Fall 2014

About the impossibility to prove P NP or P = NP and the pseudo-randomness in NP

8 Square matrices continued: Determinants

CMSC 132, Object-Oriented Programming II Summer Lecture 1:

Before We Start. The Pumping Lemma. Languages. Context Free Languages. Plan for today. Now our picture looks like. Any questions?

Supplement for MAA 3200, Prof S Hudson, Fall 2018 Constructing Number Systems

List reversal: back into the frying pan

Java Programming. Final Examination on December 13, 2015 Fall 2015

Announcements. Project #1 grades were returned on Monday. Midterm #1. Project #2. Requests for re-grades due by Tuesday

Analog Computing: a different way to think about building a (quantum) computer

Lecture 16 More Profiling: gperftools, systemwide tools: oprofile, perf, DTrace, etc.

A Practical Comparison of Agile Web Frameworks

Theory of Computation Prof. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute Of Technology, Madras

Strategies for dealing with Missing Data

On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work

Compiling Techniques

Voortgezet Programmeren

CompA - Complex Analyzer

Transcription:

More on Methods and Encapsulation Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 31, 2009 H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 0 / 38

Local Variables Local Variables (7/7, courtesy of Prof. Chuen-Liang Chen) b l belong to t declaration modifier difi static i allocation (when) allocation (where) initial to 0 de-allocation scope local variable method invocation within method NO O method invocation stack memory NO method return usage range from declaration to end of block instance variable class (static) variable i t instance class l within class within class NO O YES S instance class g creation loading heap memory heap memory YES YES automatic garbage NO collection direct access range whole class OOP H.-T. Lin (NTU CSIE) whole class Chuen-Liang Chen, NTU CS&IE / 5 More on Methods and Encapsulation OOP(even) 03/31/2009 1 / 38

Local Variables Local Variables: Key Point local variables: the status of the current frame by spec not necessarily initialized H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 2 / 38

JVM Memory Memory Revisited 1 p u b l i c class FibDemo { 2 i n t f i b ( i n t n ) { 3 i n t res ; 4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; 5 System. out. p r i n t l n ( s ) ; 6 i f ( n <= 1) res = 1; 7 else res = f i b ( n 1) + f i b ( n 2); 8 r e t u r n res ; 9 } 10 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 11 FibDemo f = new FibDemo ( ) ; 12 System. out. p r i n t l n ( " res = " + f. f i b ( 5 ) ) ; 13 } 14 } a stack that holds frames (n, res, s, arg, f) a heap that holds objects (where arg, f, s point to) a constant pool that holds constant objects ("fib(") somewhere that holds the class (variables and codes) HEAP: 2nd one, or the last three (automatic GC on heap) H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 3 / 38

Memory Revisited JVM Memory: Key Point method stack: for method calls HEAP: for objects/classes (with GC on heap) H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 4 / 38

Method Overloading Method Overloading (1/2) 1 class P r i n t e r { 2 void p r i n t I n t e g e r ( i n t a ) ; 3 void printdouble ( double a ) ; 4 void p r i n t S t r i n g ( S t r i n g a ) ; 5 } Integer and int are basically saying the same thing lazy people don t want to type so many words H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 5 / 38

Method Overloading Method Overloading (2/2) 1 class P r i n t e r { 2 void p r i n t ( i n t a ) ; 3 void p r i n t ( double a ) ; 4 i n t p r i n t ( S t r i n g a ) ; 5 void p r i n t ( i n t a, i n t b ) ; 6 } 7 class PrinterThatCompilerSees { 8 void p r i n t _ i n t ( i n t a ) ; 9 void p r i n t _ d o u b l e ( double a ) ; 10 i n t p r i n t _ S t r i n g ( S t r i n g a ) ; 11 void p r i n t _ i n t _ i n t ( i n t a, i n t b ) ; 12 } Java s (and many modern language s) solution: one method name, many possible argument types have seen it in constructors called method overloading Java signature of a method: include name and parameters (but NO return types) H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 6 / 38

Method Overloading Method Overloading (2/2) 1 class P r i n t e r { 2 s t a t i c void p r i n t ( i n t a ) ; 3 s t a t i c i n t p r i n t ( i n t a ) ; 4 s t a t i c double p r i n t ( i n t a ) ; 5 6 s t a t i c void main ( S t r i n g [ ] argv ) { 7 P r i n t e r. p r i n t ( P r i n t e r. p r i n t ( 3 ) + 5 ) ; 8 / / which one do you want to c a l l? 9 } 10 } determine programmer s intention from arguments: easy determine programmer s intention from return value: hard can cast, can discard, etc. Java signature of a method: include name and parameters only compiler s job: from arguments (type), determine which method (name+parameters) to call cannot have two methods with the same signature H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 7 / 38

Method Overloading Method Overloading: Key Point method overloading: a compiler s help by looking at signature rather than name in calling H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 8 / 38

Method Overloading Operator Overloading 1 class Demo{ 2 s t a t i c void main ( S t r i n g [ ] argv ) { 3 i n t a = 5; 4 System. out. p r i n t l n (3 + a ) ; / / plus ( 3, a ) 5 System. out. p r i n t l n ( " " + a ) ; / / plus ( " ", a ) 6 } 7 } Java: a limited special case for String (actually, StringBuffer); the usual cases for primitive types; but not for other extended types C++: can overload almost any operator for any class double-sided sword: powerful, but easily misused H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 9 / 38

Method Overloading Operator Overloading: Key Point operator overloading: very limited support in Java (up to now, and possibly will be) H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 10 / 38

Encapsulation (1/5) 1 class Record { 2 S t r i n g name ; 3 S t r i n g password ; 4 } 5 6 p u b l i c class RecordDemo { 7 p u b l i c s t a t i c void main ( S t r i n g [ ] argv ) { 8 Record r ; 9 S t r i n g s, p ; 10 s = getloginnamefromuser ( ) ; 11 r = getrecordfromfile ( s ) ; 12 System. out. p r i n t l n ( r. password ) ; 13 p = getpasswordfromuser ( ) ; 14 i f ( p. equals ( r. password ) ) { 15 / /... 16 } 17 } 18 } if password not encoded, the SYSOP might easily get your password by getrecordfromfile H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 11 / 38

Encapsulation (2/5) 1 class Record { 2 S t r i n g name ; 3 S t r i n g encoded_password ; 4 } 5 6 p u b l i c class RecordDemo { 7 p u b l i c s t a t i c void main ( S t r i n g [ ] argv ) { 8 Record r ; 9 S t r i n g s, p ; 10 s = getloginnamefromuser ( ) ; 11 r = getrecordfromfile ( s ) ; 12 p = getpasswordfromuser ( ) ; 13 i f (YOUR_ENODING( p ). equals ( r. encoded_password ) ) { 14 / /... 15 } 16 / / A new and careless programmer adds t h i s l i n e 17 r. encoded_password = n u l l ; 18 } 19 } even when password encoded, a careless programmer may make stupid bugs H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 12 / 38

Encapsulation (3/5) 1 class Record { 2 p r i v a t e S t r i n g encoded_password ; 3 p u b l i c S t r i n g get_encoded_password ( ) { 4 r e t u r n encoded_password ; 5 } 6 } 7 p u b l i c class RecordDemo { 8 p u b l i c s t a t i c void main ( S t r i n g [ ] argv ) { 9 Record r ; 10 S t r i n g s, p ; 11 s = getloginnamefromuser ( ) ; 12 r = getrecordfromfile ( s ) ; 13 p = getpasswordfromuser ( ) ; 14 i f (YOUR_ENODING( p ). equals ( r. get_encoded_password ( ) ) ) { 15 / /... 16 } 17 / / A new and careless programmer adds t h i s l i n e 18 r. encoded_password = n u l l ; / / won t work 19 } 20 } what if you want to set a new password? H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 13 / 38

Encapsulation (4/5) 1 class Record { 2 S t r i n g name ; 3 p r i v a t e S t r i n g encoded_password ; 4 p u b l i c S t r i n g get_encoded_password ( ) { 5 r e t u r n encoded_password ; 6 } 7 p u b l i c void set_encoded_password ( S t r i n g raw_password ) { 8 i f ( blahblah ) 9 encoded_password = YOUR_ENCODING( raw_password ) ; 10 } 11 } separate implementation and use: you implement the Record class, and other people (possibly you after two years) use it don t trust other people: silly mistakes can happen hide unnecessary details (a.k.a. member variables) think about possible correct/incorrect use of your class: check them in the methods H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 14 / 38

Encapsulation (5/5) 1 p u b l i c class RecordDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] argv ) { 3 Record r ; 4 S t r i n g s, p ; 5 s = getloginnamefromuser ( ) ; 6 r = getrecordfromfile ( s ) ; 7 p = getpasswordfromuser ( ) ; 8 i f ( r. match_password ( p ) ) { 9 / / no need to show encoding to the outside 10 } 11 r. set_encoded_password ( old_password, new_password ) ; 12 / / don t want t h i s to happen 13 r. encoded_password = n u l l ; 14 } 15 } freedom on making assignments: a potential hole to do bad and/or make bugs H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 15 / 38

Encapsulation: Key Point as a designer, you should avoid giving the users of your code too much freedom to do bad and/or make bugs H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 16 / 38

Hiding Variables from All Classes (1/3) 1 class Record { 2 p r i v a t e S t r i n g encoded_password ; 3 } private: hiding from all classes (except myself, of course) H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 17 / 38

Hiding Variables from All Classes (2/3) 1 class Record { 2 p r i v a t e S t r i n g name ; 3 p u b l i c S t r i n g get_name ( ) { 4 r e t u r n name ; 5 } 6 p u b l i c S t r i n g get_copied_name ( ) { 7 r e t u r n new S t r i n g (name ) ; 8 } 9 } public: accessible by all classes getter: get the reference/content of the class H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 18 / 38

Hiding Variables from All Classes (3/3) 1 class Record { 2 p r i v a t e S t r i n g name ; 3 p u b l i c void set_name_by_ref ( S t r i n g name ) { 4 i f (name!= n u l l ) 5 t h i s. name = name ; 6 } 7 p u b l i c void set_name_by_copy ( S t r i n g name ) { 8 i f (name!= n u l l ) 9 t h i s. name = new S t r i n g (name ) ; 10 } 11 } setter: check and set the member variable to a value H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 19 / 38

Hiding Variables from All Classes: Key Point private member variables public getter/setter methods H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 20 / 38

More on Hiding Details (1/3) 1 class Date { / / implemented f o r your desktop 2 p r i v a t e i n t month, day ; 3 p u b l i c i n t get_month ( ) { r e t u r n month ; } 4 p u b l i c i n t get_day ( ) { r e t u r n day ; } 5 } 6 class Date_TWO{ / / implemented on a small memory machine 7 p r i v a t e s h o r t encoded_month_and_day ; 8 p u b l i c i n t get_month ( ) { 9 r e t u r n encoded_month_and_day / 100; 10 } 11 p u b l i c i n t get_day ( ) { 12 r e t u r n encoded_month_and_day % 100; 13 } 14 } two implementations, same behavior easy for users to switch on different machines trade-offs: memory usage, computation, etc. H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 21 / 38

More on Hiding Details (2/3) 1 class Distance { 2 p r i v a t e double mile ; 3 p u b l i c double get_mile ( ) { 4 r e t u r n mile ; 5 } 6 p u b l i c double get_km ( ) { 7 r e t u r n mile 1. 6 ; 8 } 9 p u b l i c void set_by_km ( double km ) { 10 t h i s. mile = km / 1. 6 ; 11 } 12 p u b l i c void set_by_mile ( double mile ) { 13 t h i s. mile = mile ; 14 } 15 } one storage, different information from different getter/setter H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 22 / 38

More on Hiding Details (3/3) Some rules of thumb: make all instance/class variables private use getters/setters for safely access the variables Cons: accessing requires method calls, slower Pros: less chance of misuse by other users flexibility H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 23 / 38

More on Hiding Details (Yet Another Case) 1 class Solver { 2 p u b l i c void read_in_case ( ) { } 3 p u b l i c void compute_solution ( ) { } 4 p u b l i c void o u t p u t _ s o l u t i o n ( ) { } 5 p u b l i c void solve ( ) { 6 read_in_case ( ) ; 7 compute_solution ( ) ; 8 o u t p u t _ s o l u t i o n ( ) ; 9 } 10 } should the three utility functions be public? H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 24 / 38

More on Hiding Details: Trade-Off Debate no hiding at all hiding everything, showing only few allowed handles H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 25 / 38

More on Hiding Details: Key Point hiding details: don t directly access internal stuff to gain flexibility and avoid misuse H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 26 / 38

Java Member Encapsulation (1/2) 1 class Distance { 2 p r i v a t e double mile ; 3 p u b l i c double get_mile ( ) { 4 r e t u r n mile ; 5 } 6 p r i v a t e double g e t _ r a t i o ( ) { 7 r e t u r n 1. 6 ; 8 } 9 p u b l i c double get_km ( ) { 10 r e t u r n mile r a t i o ; 11 } 12 } private: hidden, on variables and methods that you do not want anyone to see public: on variables and methods that you want everyone to see H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 27 / 38

Java Member Encapsulation (2/2) 1 package tw. edu. ntu. csie ; 2 class Demo{ 3 p r i v a t e double mile ; 4 d e f a u l t i n t a ; / / imagine, but not c o r r e c t grammar 5 i n t b ; 6 p u b l i c double get_mile ( ) { 7 r e t u r n mile ; 8 } 9 p r i v a t e double g e t _ r a t i o ( ) { 10 r e t u r n 1. 6 ; 11 } 12 double get_km ( ) { 13 r e t u r n mile r a t i o ; 14 } 15 } 16 class Another { 17 } default: classes in the same package can access it e.g. in the same source file, declared with the same package a grey-area usage H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 28 / 38

Java Member Encapsulation: Key Point public/private: the more common pair for OO programmers default: for laziness of beginners, or real-advanced use in package making H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 29 / 38

Java Class Encapsulation (2/2) 1 package my. package. name ; 2 p u b l i c class PublicDistance { 3 } 4 p r i v a t e class P r i v a tedistance { / /NO! 5 } 6 class DefaultDistance { 7 } no private: class is meant for everyone to use public class should have same name as source file (thus, one public code per source file) default class for utility use within the same package H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 30 / 38

Java Class Encapsulation: Key Point public: entry point of each source default: for utility use H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 31 / 38

Who Can Access What? callee class public public public default default default callee membership public private default public private default callee itself same source same package others H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 32 / 38