190 likes | 320 Views
(a very brief) Introduction to Objects. by Dr. Billy B. L. Lim. # of attendees at OOPSLA/JavaOne continues to increase Products continue to surface in the market OO Languages: C++, Smalltalk, Java , Eiffel, Objective-C, CLOS, Turbo Pascal, Ada 9X, OO Turing, Object-COBOL, etc. OO 4GLs:
E N D
(a very brief) Introduction to Objects by Dr. Billy B. L. Lim
# of attendees at OOPSLA/JavaOne continues to increase • Products continue to surface in the market • OO Languages: • C++, Smalltalk, Java, Eiffel, Objective-C, CLOS, Turbo Pascal, Ada 9X, OO Turing, Object-COBOL, etc. • OO 4GLs: • PowerBuilder, Enfin, ObjectView, etc. • ORDBMSs/OODBMSs: • Oracle 10g, DB2 Viper, SQL Server 2005, etc. • ObjectStore, Gemstone, Versant, Ontos, OpenODB, Trellis, O2, Orion, etc. • Class Libraries • MFC, OWL, Tools.h++, etc. Why Object-Orientation: Observation
1. a thing that can be seen or touched • 2. a person or thing to which action, feeling, etc. is directed ... [Webster's 87] • Objects are an attempt to associate more meaning with data • Data and code together form objects • Code associated with an objects (called methods) define its behavior and provides services • An encapsulation of state (data values) and behavior (operations) [Budd 91] • People, places, and things (PPT) What is an Object?
An "Object" in C /* This is a struct in C */ struct Student { char* ssn; char* name; float gpa; char* major; }; struct Student s1, s2;
class Student { /* State */ private String name; private String address; private float gpa; private String major; /* Behavior */ public void print() { System.out.println("Name ="+ name); // print other attributes here } } Student s = new Student() ; An Object in Java creates an object of type Student
class Student { /* State */ char *name; char *address; float gpa; char *major; /* Behavior */ public: void print() { cout << "Name = " << name; // print other attributes here } }; Student s ; An Object in C++ s is an object of type Student
What is a Class? • A number of people or things grouped together because of likenesses; kind; sort ...[Webster's 87] • A description of a set of objects with similar characteristics, attributes, and behaviors. [LaLonde & Pugh 90] • An abstract description of the data and behavior of a collection of similar objects [Budd 91]
Templates (i.e., blueprint) for objects • Object Factory What is a Class? (cont'd) Telephone Class Person Class
Person object a message Messages and Methods John Male 40 getAge() int getAge() { return age; } 40 a method Methods are not stored with objects!
OO: The Defining Characteristics • Polymorphism • Inheritance • Encapsulation Easy to remember, just think of P I E
Person Student • A object/class can extend the capability of a previously defined object/class (Thine classes or types shalt inherit from their ancestors) • Organization: Generalization/Specialization hierarchy Inheritance
An object should hide things from other objects, limiting visibility about what "I know and do." [Object International](Thou shalt encapsulate thine objects) Encapsulation Methods Data
Name: Billy Sex: Male Salary: 5K Name: Mary Sex: Female HoursWorked: 40 Rate: $100 annualSalary? annualSalary? $208,000/yr $60,000/yr • means many forms • The interpretation of a message is in the hands of the receiver; i.e., the same message can be interpreted differently by different receivers (Thou shalt not bind prematurely) Polymorphism SalariedEmployee HourlyEmployee
Polymorphism: Example • Problem:Want to print a variety of objects from a given container Container Square1 Square2 Circle1 Star1 Circle2
Traditional Solution: // static, inflexiblep := first object;while (not end of container) { switch (p->tag) { case (square): printSquare (p); case (circle): printCircle (p); case (star): printStar (p); default: error(); } next p;} Polymorphism: Example (2)
Polymorphism: Example (3) • Object-Oriented Solution: // dynamic, flexiblep := first object;while (not end of container) { p.print(); next p;}
Traditional vs. OO Software Development • Traditional • based on top-down, functional decomposition"What routines/functions are needed to solve the problem?" • Object-Oriented • based on bottom-up, object decomposition; the idea of objects that interact with each other"What are the fundamental objects in the system? what do they look like? How do they interact with each other? What associated routines do they need?"
Traditional vs. OO Software Development (2) • Traditional • Object-Oriented data structure data structure Function1 FunctionN Function2 ObjN Obj2 Obj1 Message Message