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

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.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
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
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
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.
Writing Classes (Chapter 4)
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
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.
10-Nov-15 Java Object Oriented Programming What is it?
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,
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
CSC 205 Java Programming II Defining & Implementing Classes.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
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.
 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.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Comp1004: Building Better Objects II Encapsulation and Constructors.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Defining Classes and Methods
Classes and Objects: Encapsulation
Chapter 3: Using Methods, Classes, and Objects
Anatomy of a class Part I
COS 260 DAY 10 Tony Gauvin.
Classes and Objects 2nd Lecture
CSC 480 Software Engineering
Classes and Objects Encapsulation
Encapsulation & Visibility Modifiers
COMP 110 Information hiding and encapsulation
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Object-Oriented Programming
Classes & Objects: Examples
Encapsulation and Constructors
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Object-Oriented Programming
Defining Classes and Methods
Object-Oriented Programming
Defining Classes and Methods
Defining Classes and Methods
Information Hiding and Encapsulation Section 4.2
Anatomy of a class Part I
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Presentation transcript:

COMP 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(); berkeley.setYear(2); Student brett = new Student(); 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(); berkeley.setYear(2); Student brett = new Student(); brett.setYear(3); swapYear(berkeley, brett); System.out.println(berkeley.year);

Today  Public / private  Information hiding and encapsulation

 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 public/private Modifier

public class Student { public int classYear; private String major; } Student jack = new Student(); jack.classYear = 1; jack.major = “Computer Science”; Example OK, classYear is public Error!!! major is 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 More About 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? Instance Variables Should Be private

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; } Example: Rectangle 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

 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 Accessors and Mutators

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; } Example: Student Accessors Mutators

 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 Okay, But Why Making Methods private?

 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 Example: Driving a Car

 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

 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 Encapsulation in Classes

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; } Example: Two Implementations of Rectangle 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; }

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

A Well-Encapsulated Class Definition  Imagine a wall between interface and implementation

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