Understanding the Object Paradigm

Similar documents
CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Objectives Pre-Test Questions Introduction Collaboration Diagrams Flow of Events and Special Requirements...

Object-Oriented Programming Concepts

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

CS1004: Intro to CS in Java, Spring 2005

1. Write two major differences between Object-oriented programming and procedural programming?

Software Development. Modular Design and Algorithm Analysis

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objectives. Explain the purpose and objectives of objectoriented. Develop design class diagrams

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

UML Primer. -Elango Sundaram

Chapter 4: Writing Classes

CS200: Advanced OO in Java

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Object-Oriented Programming. Objects. Objects. Objects

Chapter 6 Introduction to Defining Classes

ECE 122. Engineering Problem Solving with Java

2D1358 Object Oriented Program Construction in C++ Exercises & Labs. Course Registration / Accounts. Course Literature

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Object-Oriented Systems Analysis and Design Using UML

Introduction to OO Concepts

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CS112 Lecture: Defining Instantiable Classes

Module 10 Inheritance, Virtual Functions, and Polymorphism

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

EXAMINATION FOR THE BSC (HONS) INFORMATION TECHNOLOGY; BSC (HONS) INFORMATION SYSTEMS & BSC (HONS) COMPUTER SCIENCE; YEAR 1

Object-Oriented Programming

CSCE 156 Computer Science II

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

13. Java Classes. Educational Objectives. Classes - Technical. Definition: Classes

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

1. Software Systems Complexity, OO Paradigm, UML

JAVA: A Primer. By: Amrita Rajagopal

Programming II (CS300)

JAVA GUI PROGRAMMING REVISION TOUR III

Inheritance and Polymorphism

Classes and Objects. CGS 3416 Spring 2018

CS 152 Computer Programming Fundamentals Lab 7: Book and Author Objects

Anatomy of a Class Encapsulation Anatomy of a Method

Defining Classes and Methods

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object- Oriented Design with UML and Java Part I: Fundamentals

OBJECT ORIENTED SYSTEM DEVELOPMENT Software Development Dynamic System Development Information system solution Steps in System Development Analysis

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

James Newkirk

Programming II (CS300)

10. Java Classes. Classes - Technical. Example: Earthquake catalog. Classes - Conceptual

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Course 3 7 March

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

OOAD - OBJECT MODEL. The concepts of objects and classes are intrinsically linked with each other and form the foundation of object oriented paradigm.

Objects and Classes -- Introduction

Object oriented programming Concepts

Methods and Data (Savitch, Chapter 5)

Defining Classes and Methods

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

5. Defining Classes and Methods

CLASSES AND OBJECTS IN JAVA

administrivia today UML start design patterns Tuesday, September 28, 2010

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

CHAPTER 5 GENERAL OOP CONCEPTS

SOFTWARE DESIGN COSC 4353 / Dr. Raj Singh

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

06. Analysis Modeling

The Open Group SOA Ontology Technical Standard. Clive Hatton

CS304 Object Oriented Programming Final Term

CSCU9T4: Managing Information

Design Patterns. An introduction

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Basic Object-Orientation

INTERNAL ASSESSMENT TEST III Answer Schema

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Information systems modelling UML and service description languages

ACRONYMS AND GLOSSARY

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

Object Oriented Programming

What are the characteristics of Object Oriented programming language?

Introduction to Classes

CHAPTER 1. Objects, UML, and Java

CS112 Lecture: Working with Numbers

Encapsulation. Mason Vail Boise State University Computer Science

CMSC 132: Object-Oriented Programming II

OBJECT ORİENTATİON ENCAPSULATİON

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

An Introduction to Patterns

Software Service Engineering

Java Object Oriented Design. CSC207 Fall 2014

Credit where Credit is Due. Lecture 4: Fundamentals of Object Technology. Goals for this Lecture. Real-World Objects

May Comp-B 11, Advanced Software Design. 3 hours duration

COP 3330 Final Exam Review

Object-Oriented Programming

Module Outline. What is Object-Oriented? Some Possible Definitions. Why Object-oriented? Fundamentals of Object Orientation

Transcription:

2 Understanding the Object Paradigm M MAJOR A J O R T TOPICSO P I C S Objectives... 18 Pre-Test Questions... 18 Introduction... 19 Classes... 19 Classes and Functions... 20 Abstraction... 25 Adding and Naming Classes... 26 Operation Visibility... 26 Class Responsibility... 27 Instantiation... 28 Object Communication... 28 Object Relationships... 28 Application Objects... 30 Summary... 32 Post-Test Questions... 32

18 Chapter 2 Understanding the Object Paradigm OBJECTIVES At the completion of this chapter, you will be able to: Explain the difference between classes and instances. Describe two common method types and their applications. Identify the benefits of abstraction and encapsulation. Describe communication among objects. Define a simple object model using the has a and uses a relationships. Describe Jacobson's application object types. PRE-TEST QUESTIONS The answers to these questions are in Appendix A at the end of this manual. 1. How do abstraction and encapsulation augment the reusability of objects?...... 2. What three types of objects did Jacobson identify in object-oriented software systems?......

Introduction 19 INTRODUCTION In object oriented software design, object dynamics and object communication are vital to system integrity. The organization and accessibility of object properties can be implemented by a variety of structures, including classes, methods, and functions. These are manipulated by processes, such as data instantiation, encapsulation, and abstraction. The classification of object relationships also plays a vital role in organizing object abstraction and inter-system communication. CLASSES Objects are the building blocks of an object-oriented software system. They model realworld collections of information (such as a purchase order) and are able to modify themselves. A class provides the blueprint for an object. For example, a library check-out system manages the movement of thousands of books, as patrons check them out and return them. Each book is represented by an object that maintains properties, such as the title, the author, and the ISBN. A class provides a blueprint for these objects. Throughout this chapter and the next, you will study short code examples written in Java and C++. The following Java code is the class definition of a book: class Book { String title; String author; String isbn; }

20 Chapter 2 Understanding the Object Paradigm The same class is defined using C++ as follows: class Book { char *title; char *author; char *isbn; } The properties that form a class are its member variables. The Book class has three member variables: title, author, and ISBN. Once a Book object is instantiated, values can be assigned to its member variables to define the unique characteristics of this book. Both Java and C++ use dot notation to access member variables. The following Java code assigns values to the member variables: catcherintherye.title = "Catcher in the Rye"; catcherintherye.author = "J. D. Salinger"; catcherintherye.isbn = "0-316-76948-7"; CLASSES AND FUNCTIONS In addition to member variables, classes have methods, which access and modify member data. Programmers who have experience using structured programming languages, such as C and Pascal, will recognize that methods are similar to functions. A method, however, is specific to a particular class. It always takes an action, with respect to the class in which it is defined. As part of a specific class, a method is associated with any object of that class. While it is possible for a class or object to have multiple methods, a method may only access information pertaining to one object. This prevents the exchange of data among objects and limits data corruption.

Classes and Functions 21 Imagine a library system developed using a structural programming language. Two abstract data types exist: one representing a book, and a second representing a binary search tree of all books (a book collection). Using C, the code to add a book to the book collection might be similar to the following: addbook(bookcollection, book); A function addbook takes two parameters: a Book to be added and a BookCollection to which you add the Book. The addbook function is defined outside of the definition of the BookCollection data type, even though the two are closely related. In an object-oriented system, addbook would be defined as a method of a BookCollection class. Member methods can be accessed using the same dot notation used to access member data: bookcollection.addbook(book); The addbook method can be thought of as a function in which an instance of BookCollection is implicitly passed. This bundling of functionality with data is fundamental to object-oriented programming. Two common types of methods are accessors and mutators, which are used to access and modify member data. They are used as an alternative to accessing member data directly using the dot notation discussed in the previous section.

22 Chapter 2 Understanding the Object Paradigm Accessors Accessor methods can be used to ask an object about itself. Rather than accessing member data directly, accessors provide a safe interface to an object. Following is the Book class definition with accessors added using Java: class Book { String title; String author; String isbn; String gettitle(); String getauthor(); String getisbn(); } The same class is defined using C++ as follows: class Book { char *title; char *author; char *isbn; } char *gettitle(); char *getauthor(); char *getisbn(); In the new class definition, prototypes are added for three accessor methods: gettitle, getauthor, and getisbn. These methods are used to access the member data.

Classes and Functions 23 Mutators Mutators are member methods that are used to modify member data. Like accessors, mutators prevent programmers from accessing member data directly. Following is the Book class definition with mutators added using Java: class Book { private String title; private String author; private String isbn; public String gettitle(); public String getauthor(); public String getisbn(); } public void settitle(string); public void setauthor(string); public void setisbn(string);

24 Chapter 2 Understanding the Object Paradigm The same class is defined using C++ as follows: class Book { private: char *title; char *author; char *isbn; public: char *gettitle(); char *getauthor(); char *getisbn(); } void settitle(char*); void setauthor(char*); void setisbn(char*); Three mutators have been added to the Book class: settitle, setauthor, and setisbn. The public and private keywords set access privileges for the Book class. The member data is declared as private, which prevents member data from being accessed directly from outside the class. To access the member data, you must use the publicly declared accessors and mutators. Encapsulation The use of accessors and mutators offers many advantages. By hiding the implementation of the Book class, you can make changes to the implementation without disrupting the design of other classes. Consider the C++ class definition in the previous example. Replace the character arrays used to hold the title, author, and ISBN with a string class. Provided that all access to Book data is restricted to accessors and mutators, the change will be transparent to the rest of the program. This type of data protection is called encapsulation. Accessors and mutators can be thought of as surrounding the data that forms a class. Figure 2-1 illustrates the encapsulation of the Book class.

Abstraction 25 getisbn title author isbn gettitle getauthor Figure 2-1: Book class ABSTRACTION Encapsulation plays an important role in developing objects as black boxes. The process of developing classes in terms of their interfaces and functionality, instead of their implementation details, is called abstraction. Abstraction is used to manage complexity; software engineers use abstraction to break complex systems into smaller components. As development progresses, programmers know the functionality they can expect from as yet undeveloped subsystems. Thus, programmers are not burdened by considering the ways in which the implementation of later subsystems will affect the design of earlier development.

26 Chapter 2 Understanding the Object Paradigm ADDING AND NAMING CLASSES As shown, each class has unique properties, which are known as the class definitions. In the above examples, class definitions include the accessor methods of gettitle(), getauthor(), and getisbn(), and the mutator methods of settitle(), setauthor(), and setisbn(). These definitions are part of a class. Before they can be used, however, an instance must be created. OPERATION VISIBILITY In the above examples of mutators, the functions were grouped into two areas: private and public. This exemplifies the used of object visibility. Object visibility specifies which parts, if any, of a class are visible (i.e. modifiable or viewable) to other parts of a program. There are three major classes of object visibility: public, protected, and private. Public data are those that are visible from anywhere inside a program. In the above examples, the mutators settitle(), setauthor(), and setisbn() are declared public. This occurs because the entire program must be able to see and use those functions to modify the Book class data. Private data are only visible from within their class. The actual variables of Title, Author, and ISBN in the above examples are private, because they do not need to be visible and/or modifiable by the rest of the program. If users can use the publically declared mutators to modify the private data, why make the variables private at all? Although there are many reasons, safety is the most important. By making the data private and only modifiable by the mutators, the data is not directly modifiable by other functions in the program. This way, error checking can be performed before the data is modified, or there can be a log recording which part of the program modified the data. There is also the type of protected. Protected data will only be visible within a certain hierarchy of classes. For example, in a class named Vehicle, which includes subclasses of Car, Truck, and Van, protected data can be visible to the subclasses of Car, Truck, and Van.

Class Responsibility 27 CLASS RESPONSIBILITY Each class has its own data and actions, which are known as the class s responsibilities. Anything that the class has knowledge of and/or performs is known as its responsibilities. Using the above examples, the Book class has responsibilities in the form of its Name, Author, and ISBN number. It also has the ability to be checked in or out. This is graphically described in the following diagram. Figure 2-2: Class Responsibility

28 Chapter 2 Understanding the Object Paradigm INSTANTIATION Class definitions can be used to create objects. This process is called instantiation, and the objects created are called instances. These instances serve as realizations of the class definitions. Following is the Java code to instantiate a Book object: Book catcherintherye = new Book(); The code to instantiate a Book object using C++ is as follows: Book catcherintherye = Book(); OBJECT COMMUNICATION Due to the black box nature of objects, communication between objects is carried out through messages. Classes not only define objects, but also determine which messages are related to those particular objects. Because all communication and functions of objects are possible with a message interface, no other form of communication is needed. One doesn t even need to know what is contained by an object to use it. In this type of communication, an object cannot be directly modified. Since, code and data are fused within an object in object-oriented software, it is best that the information within an object be left undisturbed so as not to disrupt the normal functionality of the object. Thus, by only allowing communication with objects through their messages, the code and data within the object is protected. This is known as information hiding, or encapsulation. OBJECT RELATIONSHIPS Objects do not exist in isolation, but as part of a larger system of objects that must interact with each other. The most fundamental relationship between objects is the has a relationship. The Java implementation of the Book class has three String attributes: title, author, and ISBN. String is a class, and the relationship between the Book class and the String class is illustrated in Figure 2-3.

Object Relationships 29 Figure 2-3: The has a relationship The preceding figure is an example of a Booch class diagram. Grady Booch developed this notation for representing the relationships between the classes that comprise a software system. The simplest relationship is the has a relationship. The filled-in circle at one end of the connecting line indicates that it is the Book that has the String. Objects are more than abstract data types. In addition to their attributes, objects exhibit behavior. In a library check-out system, for example, a library card is issued, a book is checked out, and a patron pays an overdue fine. Accessors and mutators provide an access layer to member data, but other methods exist to give the object more specific behavior. These methods can be denoted with a uses a relationship. Figure 2-4 illustrates the relationship between the Patron, Book and Fine classes. Patron checks out Book pays Fine Figure 2-4: The uses a relationship

30 Chapter 2 Understanding the Object Paradigm APPLICATION OBJECTS Ivar Jacobson identified three major types of objects that comprise an object-oriented software system: entity objects, interface objects, and control objects. These types persist in the Unified Process as entity classes, boundary classes, and control classes. Entity objects Entity objects model real-world objects. All of the objects you have studied thus far have been examples of entity objects. The analysis process begins by identifying business information entities, such as different types of reports, customer information, or inventory information. Most of these information entities can be translated directly into entity objects. The business information entities of a library system include books, a book collection, patrons, librarians, addresses, and fees. Each of these can be translated into an entity object. Interface objects Interface objects bridge the gap between a system and its users. The objects that form a graphical user interface are examples of interface objects. Interface objects often take the form of window, terminal, or printer objects. Figure 2-5 illustrates the relationship between interface objects and entity objects. Patron Uses Card Catalog Terminal Searches Book Collection Database Figure 2-5: Interface objects

Application Objects 31 For example, patrons interact with a library's information system through a card catalog terminal. In the preceding figure, the card catalog terminal is represented by an interface object. In the completed software system, this interface object would implement the user interface. The Unified Process refers to interface objects as boundary classes because they act as the boundary between a software system and its users. Control objects Control objects implement complex business logic that cannot be easily implemented in either entity or interface objects. Control objects typically represent schedulers, sequencers, and other concepts that can be used to tie a system together. Figure 2-6 illustrates the relationship between control objects and entity objects. Book Collection Database Uses Fine Scheduler Issues Fine Pays Patron Figure 2-6: Control objects For example, fines are issued when a book is overdue. In the preceding figure, the Fine Scheduler object uses information it collects from the Book Collection Database to determine when to issue a fine.

32 Chapter 2 Understanding the Object Paradigm SUMMARY Objects are the building blocks of object-oriented software systems. Instantiation is the creation of an object based on class definitions. Encapsulation provides a barrier between users of an object and an object's implementation details. Accessors and mutators accomplish the goal of encapsulation. Encapsulation is closely related to the concept of abstraction. Objects communicate through a message interface. Objects relate to each other in several different ways, and class diagrams are used to denote the relationships between objects. Most objects can be classified as one of three major object types: entity objects, interface objects, or control objects. POST-TEST QUESTIONS The answers to these questions are in Appendix A at the end of this manual. 1. Why do uses a and has a relationships make reusing objects more complicated?...... 2. Why are class diagrams used?......