Presentation is loading. Please wait.

Presentation is loading. Please wait.

Workshop for Programming And Systems Management Teachers

Similar presentations


Presentation on theme: "Workshop for Programming And Systems Management Teachers"— Presentation transcript:

1 Workshop for Programming And Systems Management Teachers
Chapter 2 Introduction to Object-Oriented Programming Georgia Institute of Technology

2 Georgia Institute of Technology
Learning Goals Understand at a conceptual level What is object-oriented programming? What is an object? What is a class? An object-oriented program is a simulation Objects are responsible for their data and operations Georgia Institute of Technology

3 What does Object-Oriented mean?
The focus is on objects Finding objects Describing objects Classifying objects (What type of thing?) Describing how objects interact to accomplish a task Determining the relationships between types of objects Georgia Institute of Technology

4 Object-Oriented Example
What happens when you go to a restaurant to get a meal? How many objects are involved (people and other things)? What things do the objects know about? What things (operations) can they do? When you go to a restaurant you usually will have someone greet you and ask how many are in your party. They will probably also ask if you want the smoking or non-smoking section. They will show you to your table and perhaps notify the waiter or waitress that you are there. The waiter or waitress takes your order and gives it to the chef. The chef cooks the order and notifies the waiter or waitress that it is ready. The waiter or waitress picks up the food and brings it to you. You eat the food and pay the bill. Each object in this simulation has data and operations. The objects work together to complete the task. Georgia Institute of Technology

5 Georgia Institute of Technology
Objects and Classes The objects in the restaurant are the people or things doing the work or being acted upon The classes are how we classify the objects What type of thing is the object? What job classifications are there in a restaurant? How many people do you need for each job classification? Georgia Institute of Technology

6 Georgia Institute of Technology
What is an Object? Person, place, or thing that knows something about itself has attributes (data) and can do something has operations (behaviors) Example object It is a dog named Spot. He is a small dog. He can wag his tail. He can bark. Look around you and notice the object objects near you. What data do they have and what operations can they do? Attributes are also called fields, properties, data, instance variables, and state. Operations are also called behaviors. Georgia Institute of Technology

7 Objects are Responsible
Objects are responsible for their data The data is private Methods are used to modify the data An object may refuse to do the requested action because the data would be left in an incorrect state Like a negative account balance Keeping your data private is also known as information hiding. Georgia Institute of Technology

8 Objects know how to do their jobs
Other objects don’t worry about how the object handles the request They just assume it knows how This makes it easy to change one object without affecting others Meaning the programs can be assembled from existing objects Georgia Institute of Technology

9 Georgia Institute of Technology
What is a Class? The class is the abstract basis for the object. It describes the data that all objects of that class have All dogs have a name, and a size and the things they can do All dogs can wag their tails and bark This slide shows two objects of the dog class. They both have names and sizes and can bark and wag their tails. However, they have different names and sizes. Georgia Institute of Technology

10 Georgia Institute of Technology
Class as Blueprint The class can be thought of as the blueprint, cookie cutter, or factory It gives the objects created from it their shape. You can make many objects from the same blueprint or cookie cutter. A software class creates the objects of that class An object is an instance of a class. The blueprint is the class the created houses are the objects. Objects know what class created them. Georgia Institute of Technology

11 Object and Class Example
These are objects (instances) of the car class Cars have data a manufacturer, model, year, etc Cars have operations can go forward, go backward, turn, stop Georgia Institute of Technology

12 Computation as Simulation
An object-oriented program is a simulation of the domain Objects send each other messages to accomplish a task A message is a request Requests can be refused Stop Playing No Georgia Institute of Technology

13 Software Objects are Models
The objects we create in software are models of the physical object We can’t stick a person in our software We can create a model of the person with the information we need to know for that person for our task Person identifier name address phoneNumber We don’t put the real car in our software either. Car objects would be models or representations of the real car. Georgia Institute of Technology

14 Georgia Institute of Technology
Gopher Bash Exercise Go to What objects do you see? What type of things are they? What data do they have and what can they do? This is a simulation of the gopher bash game in arcades. Georgia Institute of Technology

15 Object-Oriented Principles
Abstract Data Types - Objects and Classes Encapsulation of data and associated operations The ability to create your own data types (classes) Inheritance The ability to factor common things into a parent class (generalize) This ability to do something different (specialize) Polymorphism A variable can be of different types (classes) at run-time Requires dynamic (run-time) binding If a system claims to be object-oriented test that it meets these three principles. Dynamic binding is also called late binding. Georgia Institute of Technology

16 Georgia Institute of Technology
Encapsulation Objects encapsulate attributes (data) and operations (methods) The data is modified by the methods Other objects can ask an object to do something to its’ data by sending a message data data Only the object should modify its’ data. This means the object controls the data and is responsible for it. If the data is wrong it is the objects fault for allowing it to be wrong. methods methods message Georgia Institute of Technology

17 Georgia Institute of Technology
Inheritance Attributes and operations are inherited from the parent class. Dogs and cats are types of mammal and thus bear live young The mammal class is a generalization The dog and cat classes are specializations Mammal If a new animal was found and you were told it was a mammal, what could you tell me about it? The parent class is also called the base class or superclass. The child class is also called the derived or subclass. Cat Dog Georgia Institute of Technology

18 Advantage of Inheritance
Handles commonality Common attributes and operations are factored out and put in one place Not copied to several locations Easier to maintain, extend, and reuse Handles differences Children can inherit the parts that are common from the parent They can add attributes and operations to handle how they differ from the parent Georgia Institute of Technology

19 Polymorphism – Many Forms
Dynamic binding The method depends on the run-time type Say we are creating an animal symphony All animals can make a noise The noise that is made depends on which animal Animal makeNoise() We would keep a list of animals that have been picked and ask each animal to make a noise. The noise that is made would depend on the actual animal at run-time. We have no way of knowing at compile-time which animal a child will pick first. We just know that it will be an animal. But, at run-time the objects will know what class they belong to. Cat Dog Pig Georgia Institute of Technology

20 Georgia Institute of Technology
Kinds of Polymorphism Unrestricted No types are declared Any object can be of any type at run-time All binding of operations is done at run-time Restricted (inheritance-based) Types are declared Objects can be the declared type or a child of the declared type at run-time May be able to do some compile-time binding Scheme and Lisp use unrestricted polymorphism. Java, C++, and C# all use restricted or inheritance-based polymorphism. Georgia Institute of Technology

21 Advantages of Polymorphism
Used to create general algorithms that work on objects of different types Collections that hold objects Makes it easy to add new types Just create the new class and implement the required operations Animal makeNoise() Compare polymorphism with how this result is achieved in a procedural language. You would need an complex conditional based on the type of object. If you wanted to add a new type you would have to edit your code everyplace the conditional exists. Cat Dog Pig Bird Georgia Institute of Technology

22 Georgia Institute of Technology
Summary Object-oriented programs are simulations Object-oriented programs have objects that interact by sending each other messages Objects have data (fields) and operations (methods) The three principles of object-oriented programs are Abstract data types (objects) Inheritance Polymorphism Georgia Institute of Technology


Download ppt "Workshop for Programming And Systems Management Teachers"

Similar presentations


Ads by Google