CS302 Topic: Intro. to Object Oriented Pgmg. Tuesday, Sept. 6, 2005
Announcements � Lab 1 due this Friday � Lab 2 is now available (online) (due Friday, Sept. 16) � Uses red-black trees � Still in C � Don’t procrastinate!!! Finish Lab 1 early, then get early start on Lab 2. � This week’s lab meetings will discuss Lab 2 assignment � ☺ Today’s m ovie case study : Movies of swarms, and relationship to OOP ☺
Computational Models � Modeling is an essential part of many disciplines in science and engineering, including software engineering � The importance of having a “good” model increases with the complexity of the project
Computational Models � A model is a simplification of reality � Main reason for developing models: � Understanding the system being developed � We build models of complex systems because we cannot comprehend such a system in its entirety
Principles of Modeling � The choice of the model has a significant impact on how the problem is approached and how a solution is devised � A model can be expressed at different levels of precision � A good model is connected to reality � A single model is not sufficient to represent most systems; A set of nearly independent models are required
Models in Software Engineering � A modeling technique suitable for the programming approach should be used � Since object-oriented approach has proven to be superior to other approaches, models designed for this approach are widely used in software engineering
Basic Principles of Object Orientation � Abstraction � Encapsulation � Modularity � Hierarchy � Inheritance
What is Abstraction? � A model that includes most important aspects of a given problem while ignoring less important details An example of an item purchasing abstraction: Counter Items Queue
What is Encapsulation? � Hide implementation from clients • Clients depend on interface
What is Modularity? � The breaking up of something complex into manageable pieces or modules Queue Canteen System Order Placement Delivery Billing
What is Hierarchy? � Level of abstraction Increasing Art abstraction Music Films Sculptures R&B Rap Decreasing Sci-Fi Action … Abstraction
What is Inheritance? � Process by which one object acquires properties of another object � Supports concept of hierarchical classification � With inheritance, object only needs to define what makes it unique within its class � In OOP, Inheritance means inheriting another object’s interface, and possibly its implementation
Example of Interface Inheritance � Suppose you want to define Search_Object_1 that: � Stores (key, value) pairs � Allows values to be looked up using keys � Supported operations for Search_Object_1: � insert: Adds a (key, value) pair to the object � delete: Deletes a (key, value) pair from the object � lookup: Given a key, retrieves the value associated with that key from the object � Later, define new object (Search_Object_2) that additionally allows traversing (key, value) pairs in sorted order � Supported operations for Search_Object_2: � All of above, plus: � rewind: returns us to beginning � next: returns next (key, value) pair � Accomplish this by having Search_Object_2 inherit Search_Object_1’s interface
Example of Implementation Inheritance � Suppose: � Search_Object_1 is implemented using binary search tree � You already have a binary search tree implemented, including implementations for the previous operations, but using different names � To inherit binary search tree implementation, we make Search_Object_1 be a subclass of the binary tree. � We then make the insert, delete, and lookup operations call the appropriate binary tree operations
But, there’s a problem with Implementation Interface… � Remember: � Object is supposed to hide its implementation � Implementation should be interchangeable with other objects that implement the same interface � Problem: � Search object’s interface is tied to the binary search tree’s interface � By making search object inherit from binary search tree, we’ve also made its implementation dependent on the binary tree � NOT GOOD!! � � Nearly always best to only inherent interface
More detailed look at Object Orientation… 1. Object 2. Class 3. Attributes 4. Operation 5. Interface (Polymorphism) 6. Generalization Relationships
1. What is an Object? � An object is a “smart” data structure � Set of state variables � Set of methods for manipulating state variables � Examples – physical, conceptual, or software entities: � Physical entity: robot � chemical process Conceptual entity: � Software entity: linked list data structure
Objects (con’t.) � An object advertises: � The types of data it will store � The types of operations it allows to manipulate its data (i.e., its interface) � An object hides: � Its implementation of the above � An object is something that has: � State � Behavior (i.e., operations) � Identity
An Object Has State � The state of an object is one of the possible conditions in which an object may exist � The state of an object normally changes over time � Represented by: Attribute values + Links (relationship instances) Object: L. Parker Name: L. Parker Employee ID: 9738239 Date hired: Aug. 1, 2002 Teaching Status:
An Object Has Behavior � Behavior determines how an object acts and reacts to requests from other objects � Behavior is represented by the set of messages it can respond to (i.e., the operations the object can perform) Add me to CS302 with L. Parker (Returns: confirmation) Registration System CS302 Course
Representing Objects � As an example, we can represent an object as rectangles with underlined names : Professor L. Parker Class Name Only Object Name Only L. Parker : Professor Class and Object Name
English 102 Physics 135 Math 241 CS 311 Example: Objects CS302 CS 140 CS102
2. What is a Class? � A class is a description of a group of objects with common properties (attributes), behavior (operations), relationships, and semantics � An object is an instance of a class � A class is an abstraction in that it: � Emphasizes relevant characteristics � Suppresses other characteristics
The Relationship Between Classes and Objects � A class is an abstract definition of an object • It defines the structure and behavior of each object in the class • It serves as a template for creating objects � Objects may be grouped into classes � A particular object of a class is an instance Professor Professor Parker Professor Thomason Professor Berry
Sample Class • Class Course Properties Behavior Name Add a student Location Delete a student Days offered Get course roster Credit hours Determine if it is full Start time End time
Representing Classes � As an example, we can represent a class using a compartmented rectangle Professor
Class Compartments � For example, we can represent a class as being comprised of three sections: • The first section contains the class name • The second section shows the structure (attributes) • The third section shows the behavior (operations) Professor Name empID create() change() save() delete()
Class Compartments (cont.) � The second and third sections may be suppressed if they need not be visible on the diagram Professor Professor Professor Professor Name Name empID empID create() create() change() change() save() save() Professor delete() delete()
English 102 Physics 135 Math 241 CS 311 CS 140 CS102 CS302 Example: Class CourseOffering
� How many classes can you see? Classes of Objects
3. What is an Attribute? Object Class :CourseOffering Attribute Number=CS311 Attributes values startTime=1540 endTime=1655 CourseOffering :CourseOffering Number startTime Number=CS302 endTime startTime=1410 endTime=1525
4. What is an Operation? CourseOffering Class addStudent deleteStudent getStartTime getEndTime Operations
5. What is Polymorphism? � The ability to hide many different implementations behind a single interface OO Principle: Manufacturer A Encapsulation Manufacturer B Manufacturer C
5. (con’t.) What is an Interface? � A named set of operations that characterize the behavior of an element. � The interface formalizes polymorphism <<interface>> Triangle Polygon draw Square move scale rotate Pentagon
6. Relationships: Generalization � A relationship among classes where one class shares the structure and/ or behavior of one or more classes � Defines a hierarchy of abstractions in which a subclass inherits from one or more super classes � Single inheritance � Multiple inheritance � Generalization is an “is-a-kind of” relationship
Example: Single Inheritance � One class inherits from another Ancestor BankAccount Superclass balance (parent) name number Generalization Withdraw() Relationship CreateStatement() Subclasses Current Savings Withdraw() GetInterest() Withdraw() Descendents
Recommend
More recommend