Notes from a lunch talk I gave on July 9, 2002.
- Introduction
- What you will learn:
- Some cool jargon to impress your friends.
- The three (and a half) basic principles OO principles
- Advantages/Disadvantages of Object Orientation,
- Old Method:
Hierarchical Functional Decomposition, aka Top-Down Design (TDD). Actual pictures from CPSC203 circa 1989 at A&M:
What are the advantages/disadvantages of TDD?
- Terms:
TLAs /
TermsDefinition OOA Object Oriented Analysis - analyzing your problem by decomposition into objects. Hint: Scan requirements for frequently used nouns and verbs, and then use Use Cases to further refine OOD Object Oriented Design - designing your code into objects OOP Object Oriented Programming - programming using the concepts of object orientation. A programming language that supports these is great, but not absolutely required. OOX Object Oriented Something - any of the above Class A design (like a blueprint for a house) of an object that contains data and methods. Object An instance (like a particular house with a street address) of a class with a unique identity. See Plato's Ideal Forms. Abstraction Only exposing the important, relevant qualities of an object or system Attribute A named property of an object capable of holding state, aka instance variables or data members Method an operation on an object. Also called a function or operation - An example of a Class using the Unified Modeling Language (UML) notation:
- What you will learn:
- Big Three (and a half)
The three and a half key ingredients of OOP are Encapsulation, Inheritance, Polymorphism, and Message Passing.
- Encapsulation - hides implementation, shows only interface.
"Encapsulation is the process of hiding all of the details of an object that do not contribute to its essential characteristics."
- Booch
What happens if the "YearOfBirth" data member is stored as years since 1900? - Inheritance - ISA relationship
A new class can be created by "Inheriting" from another class or multiple classes. The new class inherits methods and data members of the "parent" class. This is also called "extending" the base class, or "sub-classing". What are the advantages and disadvantages of Single Inheritance?
An example of Multiple Inheritance:
What are the advantages and disadvantages of Multiple Inheritance?Most methods in a class are called "instance" methods which operate on the specific data members of a class. Another type of method is the "class" method (also know as a "static" method) which does not depend on any data members and can be executed without an object (e.g., i = Integer.parseInt(sometext) ).
An "Abstract" (or virtual) base class cannot be instantiated into a real object. It can only be the parent of another class.
- Message Passing
a message is an invocation of a method on an object."an object-oriented program is a bunch of objects laying around sending messages to one another."
This more closely resembles the real world. (e.g., A CEO gets a message from an important client. She calls a VP with a message, the VP calls a manager who calls an under-paid worker who gets the info from a computer or book and sends a message back up the chain.)
- Polymorphism
What does the term mean?Polymorphism - the ability to hide many different implementations behind a single interface. When objects respond differently to the same message this is a form of Polymorphism. Like gas pedal on a car.
parametric polymorphism
Code for drawAllShapes() using virtual functions in an OO way:
void drawAllShapes() { for(i=0;i<shapeList.size();i++) { Shape shape = shapeList[i]; shape.draw(); } }
Polymorphism also exists when a derived class customizes the behavior of methods defined in the base class to meet the special requirements of the derived class. This is known as overriding methods and leads to runtime polymorphism also referred to as late binding, dynamic binding, or run-time polymorphism.
Code for drawAllShapes() using no virtual functions in a non-OO way using an OO language:
Shape would have a "type" String and an array of X and Y values. void drawAllShapes() { for(i=0;i<shapeList.size();i++) { Shape shape = shapeList[i]; // Rectangle if(shape.types equals "Rectangle") { gl_draw(x[0],y[0],x[1],y[1]); // sorta gl_draw(x[1],y[1],x[2],y[2]); ... } // Circle else if(shape.types equals "Circle") { gl_drawCircle(x[0],y[0],x[1]); // x[1] is really the radius } ... } }
Assume we have a dozen operations like draw, hide, move, resize, delete etc. in the files draw.c, hide.c, move.c.
What happens when we add a new shape type?
When we delete?
"Function overloading" provides a type of polymorphism often referred to as "compile-time" polymorphism. (Note: some authors do not consider function overloading to be a true or pure form of polymorphism.). Function Overloading is when a function has the same name but its arguments are different, either in type or number. Examples:
draw() draw(int color) draw(int color,int line_weight)
- Encapsulation - hides implementation, shows only interface.
- Review
- Advantages of OO:
-
- Easier to understand and maintain
- Models the real world more accurately
- Handles change more gracefully
- More opportunity for reuse
- Allows components to be swapped
- Eases the "Locality" problem
- Disadvantages of OO:
-
- Can run slower
- Can take more memory
Final Note: Don't "overdo" Object Orientation. It can be taken to an extreme. Creating and disposing of objects takes time.
- Quiz: What are the three major concepts of Object
Orientation?
What is the difference between Overloading and Overriding functions?
- Extras for Experts:
Model/View/Controller paradigm
Message passing - design by contract
Components can be created from groups of classes and given a well defined interface.
Visibility qualifiers: Public/Protected/Private