+ All Categories
Home > Documents > Object-Oriented Concepts

Object-Oriented Concepts

Date post: 24-Feb-2016
Category:
Upload: freira
View: 52 times
Download: 0 times
Share this document with a friend
Description:
Object-Oriented Concepts. What is Object-Oriented?. This means that we focus on the objects not just the procedures Focus on who (what objects?) as well as what (what do the objects do?). Example. - PowerPoint PPT Presentation
Popular Tags:

of 33

Click here to load reader

Transcript

Summer Institute for Computing Education

Intro-OO1Object-Oriented ConceptsChapter 1 - 1Introduction to Object-Oriented Development

What is Object-Oriented?This means that we focus on the objects not just the proceduresFocus on who (what objects?)as well as what (what do the objects do?)

Intro-OO2Chapter 1 - 2This contrasts with procedural programming in which we focus on the task that needs to be done. You may break that task up into smaller parts.In object-oriented programming we need to know who (what class) as well as what (procedures).Object-oriented programming is concerned with responsibility. Who (what object) is responsible for doing the job, not just what jobs need to be done.

Introduction to Object-Oriented Development

ExampleImagine that you are put into a group to do a job: say design and sell t-shirts to raise money for an organizationYou would want to specify the tasks to be doneYou would also want to specify who will work on each task

Intro-OO3Chapter 1 - 3Talk about why it is important to say who will do each task? How do you decide who will do the task? Why is it important to know what needs to be done? Is it a good idea to assign all the tasks to one person? Is it a good idea to assign tasks based on skills people have?Introduction to Object-Oriented Development

Task ListDesign T-ShirtsWhat will people buy?What will look good?What would be too expensive?Make the T-ShirtsWhere to get them?How to add the design?How long will they take to make?Sell T-ShirtsHow much to sell them for?When and where to sell?How do you keep track of who sold what and who ordered what?

Intro-OO4

Chapter 1 - 4Each task may have one or more sub-tasks. You might assign a person to each sub-task.Introduction to Object-Oriented Development

Job ListDesignersResponsible for designing the t-shirtsManufacturersResponsible for making the t-shirtsSellersResponsible for selling the t-shirts

Intro-OO5Chapter 1 - 5This is one way to organize who will do the work and what to call each category (class).You would probably have 1 or more designers, several manufacturers, and many sellers.Introduction to Object-Oriented Development

Objects have ResponsibilitiesIntro-OO6An object-oriented designDetermines the tasks to be doneDetermines what objects will be responsible for each taskNo one object does everythingObjects work together to accomplish tasksThe assignment of responsibilities is the key skill in object-oriented design

Chapter 1 - 6Introduction to Object-Oriented Development

What is an Object?A person, place, or thingThat knows something about itselfHas data (attributes, fields)A cashier has a id, name, and a passwordAnd can do somethingHas operations (methods)A cashier can total the items, take payment, make change

Intro-OO7Chapter 1 - 7Point out objects in the room (like a book, pen, computer, person, etc). Talk about what type of object it is, what data it has, what it can do.Go through your example real-world scenarios. Point out the objects, their types, data, and operations.The picture is a cashier in a music store. The data is his name and password. The operations are how to ring up and take payment, and make change.Introduction to Object-Oriented Development

What is a Class?The type of an object The way we classify an objectThe Idiot by Dostoevsky is a bookWar and Peace by Tolstoy is a bookMary is a cashierTasha is a cashierGrouping of objects with the same data and operations

Intro-OO8Chapter 1 - 8Objects are instances of a class. This slides shows two book objects of the class book.Introduction to Object-Oriented Development

Class: ExampleMary is a cashierTasha is a cashierCashier is a classAll cashiers have an id, name and passwordEach will have a different id, name, and passwordAll cashiers can total an order, take payment, make change

Intro-OO9Chapter 1 - 9Introduction to Object-Oriented Development

Object DataEach object has its own dataTashas id is 4 and password is mhallMarys id is 3 and password is smile4All cashier objects have an id, name, and passwordChanging Marys data wont affect Tashas data

Intro-OO10Chapter 1 - 10Object data is also called instance variables, object variables, and object fields.Introduction to Object-Oriented Development

Teaching Objects and ClassesPoint out various objects in the room and ask what type of thing are they. Ask what data is known about each type and what operations can objects of that type do.Point out that there are several objects of the same type.How are they the same and how different?

Intro-OO11Chapter 1 - 11There are probably computers in your room. What do we mean by a computer? What data does it have (manufacturer, size screen, size memory, etc). What can it do?Point out that nobody had to tell the students that these where computers. How did they know? They fit their idea of what a computer looks like.Introduction to Object-Oriented Development

Find Object Types ExerciseList the types of objects in this picture

Intro-OO12Chapter 1 - 12Introduction to Object-Oriented Development

History of Objects and ClassesPlatos Republic (~375 BC) theory of formsLet us take any common instance; there are beds and tables in the world -- plenty of them, are there not? Yes. But there are only two ideas or forms of them -- one the idea of a bed, the other of a table.

Intro-OO13Chapter 1 - 13Platos Republic is online at http://plato.evansville.edu/texts/jowett/republic39.htmEach bed is an object (instance) of the bed class.Introduction to Object-Oriented Development

History of InheritanceIntro-OO14Aristotle Parts of Animals (350 BC) describes inheritance.Linnaeuss Species Plantarum (1753) applied inheritance systematically and is the basis for modern botanical nomenclature.

MammalCatDogChapter 1 - 14Aristotles Parts of Animals is online at http://www.knuten.liu.se/~bjoch509/works/aristotle/parts_animals.txt.Information on Carl Linnaeus - Carl von Linn can be found online at http://www.systbot.uu.se/dept/history/linneaus.html.

Introduction to Object-Oriented Development

C++Intro-OO15Developed by Bjarne Stroustrup at Bell Labs in 1985created a highly-efficient version of Simula by extending the C language very popular in the late 80s to 90sstill popular for 3d graphicsChapter 1 - 15Bjarne Stroustrup. The Design and Evolution of C++. Reading, MA: Addison-Wesley, 1994. Bjarne Stroustrup, A History of C++. History of Programming Languages (HOPL-II). J. E. Sammet. New York, ACM: 699-769, 1993. Bjarne Stroustrups home page is at http://www.research.att.com/~bs/.

Introduction to Object-Oriented Development

JavaIntro-OO16In 1991, Sun Microsystems began an internal project to produce a language that could run on intelligent consumer electronic devicesJames Gosling created the programming language Oak for this project as a highly portable, object-oriented programming language. Oak evolved into Java and was released in 1995.Very popularChapter 1 - 16James Goslings home page is at http://java.sun.com/people/jag/.

Introduction to Object-Oriented Development

SimulationIntro-OO17Object-oriented development means creating a simulation of the problemWe need to know the objects in the problemSo we can create software objects to represent themWe need to know the types of the objects (classes)So we can define the data and the operations for all objects of that typeChapter 1 - 17We cant put the real objects into the computer so we need to make software objects that represent the real objects.Introduction to Object-Oriented Development

Classes Create ObjectsThe class can be thought of as a recipe, blueprint, or factoryMany objects can be created from one classObjects keep track of the class that created themI am an object (instance) of the Cookie class

Intro-OO18Chapter 1 - 18When you create an object it has a reference to the object that represent the class that created it. So, objects always know what type (class) they are.Introduction to Object-Oriented Development

Classes Define the ObjectsIntro-OO19The computer doesnt know what we mean by a car or cashierWe define the class Cashier so that the computer will understand what a cashier or bank account is and what it can doIn the context of the problem we are trying to solveThen the computer can create objects from the classesChapter 1 - 19Introduction to Object-Oriented Development

AbstractionIntro-OO20Pull out only the important details about the thing we are simulatingCashiers have hobbies but we dont need to know about them Chapter 1 - 20Introduction to Object-Oriented Development

What do Objects Look Like?Objects are created with space for their data (Instantiated)Object have a reference to the object that represents the classObject of the class Class

Intro-OO21Mary : Cashierid = 3, name=Marypassword = smile4Tasha : Cashierid = 4Name=Tashapassword = mhallName = CashierMethods = totalOrder() takePayment(payment) makeChange()Cashier : ClassChapter 1 - 21To get the class object that represents the class use object.getClass();To check if an object is an instance of a class use instanceof(className);There is actually an array of Method objects that holds the methods for a class. There is also an array of Field objects which describes the fields in the class.Introduction to Object-Oriented Development

Software Objects are ModelsThe objects we create in software are models of the physical objectWe cant stick a person in our softwareWe can create a model of the person with the information we need to know for that person for our task

Intro-OO22Cashieridnamepassword

Chapter 1 - 22Introduction to Object-Oriented Development

Communicating ObjectsObjects in the simulation need to communicateThey send each other messagesMessages cause methods (operations) to be executedMethods are written with parameters to represent variable data determined by the callerMethods are passed arguments when they are called

Intro-OO23Clean your room, pleaselaterChapter 1 - 23If I get a message from someone asking me to do something I can choose to do the action or not.Introduction to Object-Oriented Development

Data ResponsibilityObjects are responsible for their dataThe data should not be allowed to get into an invalid stateLike withdrawing more money than is in a back account

Intro-OO24Chapter 1 - 24Introduction to Object-Oriented Development

EncapsulationData and operations are combined in classesAll changes to data should be done by methods in the classMaking sure that the data stays validData should be privateIntro-OO25datamethodsdatamethodsmessageChapter 1 - 25If an object in class A wants to change the data in an object of class B it needs to ask the object of class B to do the change (by sending a message). The object of class B can refuse to do the change.Introduction to Object-Oriented Development

Why use Encapsulation?Intro-OO26If something goes wrong we know where the trouble isSome class didnt protect the data or didnt make sure the data was validIf something changes it is easy to find the class to fixIf I need to add or change data the methods that work on it are together in the classIf you need a method it is easy to check if such a method existsChapter 1 - 26In procedural or functional programming you create functions or procedures that act on data and any function or procedure can act on any data. So, when data is in an invalid state you dont know which procedure or function caused the problem. You have to check all the code that is passed the data to find where the problem occurred.In large procedural or functional projects it is easy for people to create several functions that do the same or similar things because they arent aware of the others.Introduction to Object-Oriented Development

Data HidingIn OO Programming we hide data from objects in other classes No direct changing of data in objects of another classObjects send messages asking for operations to be done on the dataThey dont need to know how an object is going to do something as long as they do it

Intro-OO27Chapter 1 - 27We can also think of this as security. An object keeps its data secure and doesnt let objects of other classes mess with the data.A customer object could ask a bank account object to withdraw more money than is in the bank account but the bank account object wont let it. The customer object doesnt even need to know how the bank account object stores the current balance. The customer object cant directly change the current balance.Introduction to Object-Oriented Development

Intro-OOInheritanceDid you get features or abilities from your parents?People inherit physical characteristics Some people inherit abilities: musicInheritance in OO means receiving data and methods from your parentIn Java you can only have one parent

Chapter 3 - 28Introduction to Object-Oriented Development

Intro-OOInheritance in our ModelsOne class can inherit from anotherGets all the fields and methodsThe class you inherit from is calledParent, superclass, base classThe class doing the inheriting is calledChild, subclass, derived classPersonStudentParentChildChapter 3 - 29This type of diagramming is called UML which is the Unified Modeling Language and is a standard way to show object-oriented designs.Introduction to Object-Oriented Development

Intro-OOTeaching InheritancePoint out inheritance in common objectsWhat is a book?What is a dictionary?Talk about the things that are the sameTalk about the things that are different

Chapter 3 - 30Some other ideas are (teacher, math teacher), (school, elementary school, middle school, high school)Introduction to Object-Oriented Development

Intro-OOPolymorphismLiterally: many formsIn Object-Oriented development it means that what happens when a message is sent to an object depends on the type (class) of the object at runtime

Chapter 3 - 31Introduction to Object-Oriented Development

Intro-OOHow Does Polymorphism Work?If a class is declared to be final then the compiler can figure out the location of a method that matches the messageIf a class can be subclassedthen a variable declared to be of the parent type can point to an object of the parent class or any subclass at run-timethe compiler cant determine the method to invokethe method to invoke is figured out at run-timeChapter 3 - 32Introduction to Object-Oriented Development

The End!!Intro-OO

Chapter 1 - 33Introduction to Object-Oriented Development


Recommended