COMP 110 Information hiding and encapsulation

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Information Hiding and Encapsulation
COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips
COMP More About Classes Yi Hong May 22, 2015.
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming"
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Day Class Definitions and Methods Local & global variables, parameters & arguments,
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Comp1004: Building Better Objects II Encapsulation and Constructors.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Defining Classes and Methods
Andrew(amwallis) Classes!
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
COMP 110 Review for midterm exam
Chapter 3: Using Methods, Classes, and Objects
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Anatomy of a class Part I
ATS Application Programming: Java Programming
COS 260 DAY 10 Tony Gauvin.
Classes and Objects 2nd Lecture
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
An Introduction to Java – Part II
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Encapsulation and Constructors
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Defining Classes and Methods
COP 3330 Object-oriented Programming in C++
Lecture 5- Classes, Objects and Methods
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Defining Classes and Methods
Defining Classes and Methods
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Information Hiding and Encapsulation Section 4.2
Classes and Objects CGS3416 Spring 2019.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 8 Object Oriented Programming (OOP)
Encapsulation.
Anatomy of a class Part I
Announcements Lab 5 due Wednesday at noon.
CSG2H3 Object Oriented Programming
Presentation transcript:

COMP 110 Information hiding and encapsulation Michele Weigle - COMP 14 - Spr 04 COMP 110 Information hiding and encapsulation Catie Welsh February 28, 2011

Michele Weigle - COMP 14 - Spr 04 Announcements Midterm exam Monday, March 14th Program 3 due Wednesday by 11:59pm Lab 5 due Friday by 1pm

Michele Weigle - COMP 14 - Spr 04 Questions? Any other questions?

Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 More about methods public/private Information hiding and encapsulation

Methods with parameters Parameters are used to hold the value that you pass to the method Parameters can be used as (local) variables inside the method public int square(int number) { return number * number; } Parameters go inside parentheses of method header

Methods with multiple parameters Multiple parameters separated by commas public double getTotal(double price, double tax) { return price + price * tax; }

Method parameters and arguments public class SalesComputer { public double getTotal(double price, double tax) return price + price * tax; } // ... SalesComputer sc = new SalesComputer(); double total = sc.getTotal(“19.99”, Color.RED); double total = sc.getTotal(19.99); double total = sc.getTotal(19.99, 0.065); int price = 50; total = sc.getTotal(price, 0.065); Automatic typecasting

Calling methods from methods A method body can call another method Done the same way: receiving_object.method(); If calling a method in the same class, do not need receiving_object: method(); Alternatively, use the this keyword this.method();

this Within a class definition, this is a name for the receiving object this.age this.major this.getAge() Frequently omitted, but understood to be there See book for details

public/private modifier public void setMajor() public int classYear; public: there is no restriction on how you can use the 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 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; // Output: 50, but wrong answer!

Accessors and mutators How do you access private instance variables? Accessor methods (a.k.a. get methods, getters) Allow you to look at data in private instance variables Mutator methods (a.k.a. set methods, setters) Allow you to change data in private instance variables

Example: Student Mutators Accessors public class Student { private String name; private int age; public void setName(String studentName) name = studentName; } public void setAge(int studentAge) age = studentAge; public String getName() return name; public int getAge() return age; Mutators Accessors

Okay, but why make methods private? Helper methods that will only be used from inside a class should be private External users have no need to call these methods Encapsulation

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

Encapsulation in classes A class interface tells programmers all they need to know to use the class in a program The implementation of a class consists of the private elements of the class definition 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

Well encapsulated Imagine a wall between interface and implementation Private instance variables Private constants Private Methods Bodies of all methods Method definitions Interface: Comments Headings of public methods Public defined constants Programmer

Michele Weigle - COMP 14 - Spr 04 Comments Precondition - everything that needs to be true before method Postcondition - describes effect of method call From now on you are expected to ALWAYS use pre and post conditions for methods 25

Michele Weigle - COMP 14 - Spr 04 When to use Pre and Post You can omit for obvious methods get (accessor), set (mutator) … All other methods need pre and post conditions If you are unsure, write pre and post! 26

Michele Weigle - COMP 14 - Spr 04 Guidelines Comments before class definition (this is your header) Instance variables are private Provide public accessor and mutator methods Pre and post comments before methods Make helping methods private /**/ for user-interface comments and // for implementation comments You are expected to follow these guidelines from now on. 27

Michele Weigle - COMP 14 - Spr 04 Global vs. Local Variables are local to methods Instance variables are Global for all methods in a class 28

Michele Weigle - COMP 14 - Spr 04 Wednesday Objects and references Start Midterm Review