Information Hiding and Encapsulation Section 4.2

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
ICS 201 Inheritance Introduction to Computer Science
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Information Hiding and Encapsulation
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
SWE 510: Object Oriented Programming in Java1 Defining Classes 1 This slide set was compiled from the Absolute Java textbook and the instructor’s own class.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CMSC 202 Java Classes and Object 2nd Lecture. Aug 6, Stack and Heap When your program is running, some memory is used to store local variables.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 10 Introduction to File I/O Section 10.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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).
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
Principles of Programming & Software Engineering
Structures and Classes
Data Abstraction: The Walls
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 3: Using Methods, Classes, and Objects
Chapter 19 Java Never Ends
Comp 249 Programming Methodology
Classes and Objects 2nd Lecture
Comp 249 Programming Methodology
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 14 Graphs and Paths.
Classes and Objects Encapsulation
Encapsulation & Visibility Modifiers
Classes and Objects housefly object Insect class mosquito object
Comp 249 Programming Methodology
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 10 Datapath Subsystems.
Interfaces and Inner Classes
Chapter 18 Bayesian Statistics.
Chapter 20 Hash Tables.
Defining Classes and Methods
Chapter 4 Defining Classes I
The Facts to Be Explained
Defining Classes and Methods
Chapter 3 Debugging Section 3.4
Chapter 4 Constructors Section 4.4
Section 2.3 Introduction to File Input
Multidimensional Arrays Section 6.4
Circuit Characterization and Performance Estimation
Chapter 2 Part 1 Data and Expressions.
Chapter 2 Reference Types.
Presentation transcript:

Information Hiding and Encapsulation Section 4.2 Chapter 4 Information Hiding and Encapsulation Section 4.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage

Information Hiding and Encapsulation Information hiding is the practice of separating how to use a class from the details of its implementation Abstraction is another term used to express the concept of discarding details in order to avoid information overload Encapsulation means that the data and methods of a class are combined into a single unit (i.e., a class object), which hides the implementation details Knowing the details is unnecessary because interaction with the object occurs via a well-defined and simple interface In Java, hiding details is done by marking them private Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

A Couple of Important Acronyms: API and ADT The API or application programming interface for a class is a description of how to use the class A programmer need only read the API in order to use a well designed class An ADT or abstract data type is a data type that is written using good information-hiding techniques Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

public and private Modifiers The modifier public means that there are no restrictions on where an instance variable or method can be used The modifier private means that an instance variable or method cannot be accessed by name outside of the class It is considered good programming practice to make all instance variables private Most methods are public, and thus provide controlled access to the object Usually, methods are private only if used as helping methods for other methods in the class Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Accessor and Mutator Methods Accessor methods allow the programmer to obtain the value of an object's instance variables The data can be accessed but not changed The name of an accessor method typically starts with the word get Mutator methods allow the programmer to change the value of an object's instance variables in a controlled manner Incoming data is typically tested and/or filtered The name of a mutator method typically starts with the word set Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Encapsulation Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

A Class Has Access to Private Members of All Objects of the Class Within the definition of a class, private members of any object of the class can be accessed, not just private members of the calling object Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Mutator Methods Can Return a Boolean Value Some mutator methods issue an error message and end the program whenever they are given values that aren't sensible An alternative approach is to have the mutator test the values, but to never have it end the program Instead, have it return a boolean value, and have the calling program handle the cases where the changes do not make sense Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Preconditions and Postconditions The precondition of a method states what is assumed to be true when the method is called The postcondition of a method states what will be true after the method is executed, as long as the precondition holds It is a good practice to always think in terms of preconditions and postconditions when designing a method, and when writing the method comment Copyright © 2012 Pearson Addison-Wesley. All rights reserved.