© 2006 Pearson Addison-Wesley. All rights reserved 6.2.1 How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Classes & Data Abstraction
Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Software Testing and Quality Assurance
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Review of C++ Programming Part II Sheng-Fang Huang.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Visibility, Accessibility, and Information Hiding.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
© 2006 Pearson Addison-Wesley. All rights reserved Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4.1 More About Methods - Using a Prototyping Approach.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Examples of Classes & Objects
How to Design Supplier Classes
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Encapsulation & Visibility Modifiers
Chapter 4: Writing classes
Object-Oriented Programming
Classes & Objects: Examples
Object-Oriented Programming
Defining Classes and Methods
Object-Oriented Programming
NAME 436.
Information Hiding and Encapsulation Section 4.2
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2) Discover private attributes. Generally, the need for a supplier class is discovered via an object. Programmers discover objects by looking for data and operations that belong together. 3)Implement public methods. (This step often suggests the need for other private members.) 4)Double-check the scope of all members (local is better than private; private is better than public)

© 2006 Pearson Addison-Wesley. All rights reserved public A public variable, method or class has no scope restrictions (i.e., it is accessible anywhere its class is used. The word scope refers to regions of the program that are permitted to utilize some declared entity. private A private variable or method can be referenced only within its own class. local A local variable or formal parameter can be referenced only within its own method.

© 2006 Pearson Addison-Wesley. All rights reserved RULE: Hide as much information (from the client) as possible. public class MyClass { public int badVar; private int betterVar; public Myclass() { int bestVar;... } public void publicMethod() {... } private void privateMethod() {... } public class MyClass { public int badVar; private int betterVar; public Myclass() { int bestVar;... } public void publicMethod() {... } private void privateMethod() {... }

© 2006 Pearson Addison-Wesley. All rights reserved Definition The lifetime of an object is the portion of execution time between the object’s instantiation and the time it is orphaned (or the program terminates). The lifetime of primitive data follows its scope.  Local variable lifetime is the run time of a single method execution.  Formal parameter lifetime is the run time of a single method execution.  Instance variable lifetime is the lifetime of the containing object. The lifetime of reference data depends upon code execution.  A new expression begins a lifetime.  When a variable is assigned null, any object previously bound to that variable may end its lifetime. (When would this not end the object’s lifetime?  What other instructions might orphan an object? (i.e., end its lifetime)

© 2006 Pearson Addison-Wesley. All rights reserved RULE: Hide as much information (from the client) as possible. Use _______ whenever possible. Use _________ if variable must be retained between method executions. Variables... Avoid _______ variables. Use private when possible. Methods... Use public if the method is designed to be called from outside the class.

© 2006 Pearson Addison-Wesley. All rights reserved RULE: All supplier class instance variables should be declared private. Write access can be provided to clients by a public set method. Write access Read access can be provided to clients by a public non-void method to return the variable’s value. Read access Example public class Figure { private Color privateColor;... public void setBackground( Color c ) { privateColor = c; } public Color getBackground() { return privateColor; }... public class Figure { private Color privateColor;... public void setBackground( Color c ) { privateColor = c; } public Color getBackground() { return privateColor; }...

© 2006 Pearson Addison-Wesley. All rights reserved Just like defensive driving... Defensive programming occurs when the programmer guards against possible failures. Encapsulating and hiding data through proper use of scope is an important aspect of defensive programming. local variables... are like Grandma driving a Humvee private variables without read or write access... are like Dale Earnhardt, Jr. in a Volvo private variables with read-only access... are like your parents in their family car private variables with write access... are like giving the keys to a first-time driver public instance variables... are like 150 mph in a Ford Pinto on a curvy country road