Workshop for Programming And Systems Management Teachers Chapter 2 Introduction to Object-Oriented Programming 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 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 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? 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 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. Georgia Institute of Technology 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 Georgia Institute of Technology 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 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 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. Georgia Institute of Technology 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 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 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 Georgia Institute of Technology Gopher Bash Exercise • Go to http://javaboutique.int ernet.com/Gopher/ • What objects do you see? • What type of things are they? • What data do they have and what can they do? Georgia Institute of Technology 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 runtime – Requires dynamic (run-time) binding 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 methods data message Georgia Institute of Technology methods 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 Cat Dog Georgia Institute of Technology 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 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() Cat Georgia Institute of Technology Dog Pig 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 Georgia Institute of Technology 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() Cat Dog Pig Bird 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