190 likes | 403 Views
Introduction to Object-Oriented Programming (OOP). Part 1. Objectives. Name the basic components of object-oriented programming. Differentiate classes and objects. Define class methods and attributes. Draw UML diagrams for classes and objects. OOP is about classes and objects.
E N D
Introduction to Object-Oriented Programming (OOP) Part 1 SE-1010Dr. Mark L. Hornick
Objectives • Name the basic components of object-oriented programming. • Differentiate classes and objects. • Define class methods and attributes. • Draw UML diagrams for classes and objects. SE-1010Dr. Mark L. Hornick
OOP is about classes and objects • These are two very basic concepts in OOP • …but what are they? Java is an OOP language SE-1010Dr. Mark L. Hornick
Alan Kay, who was instrumental in the creation of the first Apple Macintosh, was also the creator of the first OOP language called Smalltalk. He defined OOP as follows: SE-1010Dr. Mark L. Hornick
1. Everything is an object An object is a thing, both tangible… Objects often represent physical entities SE-1010Dr. Mark L. Hornick
1. Everything is an object … and intangible, like • Time • Date • Bank Account • Grocery List Or, objects can just represent ideas or concepts SE-1010Dr. Mark L. Hornick
2. Every object has a type of class • Classes are more difficult to describe: • A Class is an abstraction (a blueprint, or template) that defines the attributes and behavior of Objects that belong to the Class • Objects assume the characteristics of a class • We say an Object is an Instance of a Class, kind of how a cake is an instance of a recipe for a cake. • We can also say that a Class Instance is an Object. SE-1010Dr. Mark L. Hornick
3. An object is comprised of attributes and methods defined by its class • attributes are data that define an object’s properties • Each object has its own variables where it can store the values of its attributes • methods are behaviors • Methods are executed when an object receives a message to execute it. • Methods often manipulate attributes Alan Kay’s OOP definitions, continued SE-1010Dr. Mark L. Hornick
Exercise 1 List some attributes and behaviors for a BankAccountclass • Pretend it’s used within the software of an ATM machine (or within a personal finance app) • What does it represent? • What can it do? • What are its properties? SE-1010Dr. Mark L. Hornick
Exercise 2 • List some attributes and behaviors for a Printer class • What does a printer do? • What properties does it have? SE-1010Dr. Mark L. Hornick
Unified Modeling Language (UML) A notation for objects and classes. Can be applied to any OOP language (not just Java). UML class diagram SE-1010Dr. Mark L. Hornick
We’re not done with Alan Kay’s definitions yet… SE-1010Dr. Mark L. Hornick
4. Object-oriented programs use objects • An object-oriented program is a bunch of objects telling each other what to do by sending and receiving messages to and from one another • A message instructs an object to execute one of it’s methods Alan Kay’s OOP definitions, continued SE-1010Dr. Mark L. Hornick
The Relationship between Messages and Methods • To instruct an object to do something, we “send a message” to it. • You can send a message only to the objects that understand the message you send to them. • In Java (and other OO languages like C++, C#, or VB), messages are sent by calling a methoddefined within the object’s class • we also say “execute a method” or “invoke a method” • and sometimes we use “function” instead of “method” We don’t usually send messages to classes, although there are exceptions we’ll learn about later SE-1010Dr. Mark L. Hornick
UML again A UML Sequence Diagram illustrating messages being sent to an instance of a class. The object’s name hp1 The object’s class Printer The message purge hp1:Printer purge print(“report.txt”) SE-1010Dr. Mark L. Hornick
Passing values in messages A value we pass to an object when sending a message is called an argument of the message. The object’s name hp1 The object’s class Printer The message print with theargument “report.txt” hp1:Printer print(“report.txt”) SE-1010Dr. Mark L. Hornick
Two-way communication Many times, a method will return a message back to the sender. The object’s name The object’s class No argument The method hp11:Plotter getPaperLevel() 27 The return value SE-1010Dr. Mark L. Hornick
Many arguments can be sent in an originating message, but only one return value can be sent back The object’s name The object’s class Two arguments The method hp11:Plotter setFont(“Arial”, “italic”) “ok” The return value SE-1010Dr. Mark L. Hornick
Object-oriented programming • The object-oriented (OO) approach provides tools for the programmer to represent elements in the problem, or domain space • Elements in the problem space, and their representation in the solution space, are referred to as “objects” • OO allows a programmer to define a class (the type of an object) to fit the problem, rather than being forced into existing data types representing units of storage in a machine • Object-orientation allows you to describe the problem in terms of the problem, rather than in terms of the solution SE-1010Dr. Mark L. Hornick