Click here to load reader

Intro-OO 1 Object-Oriented Concepts. What is Object-Oriented? This means that we focus on the objects not just the procedures Focus on who (what objects?)

Embed Size (px)

Citation preview

  • Slide 1

Intro-OO 1 Object-Oriented Concepts Slide 2 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?) Intro-OO 2 Slide 3 Example Imagine that you are put into a group to do a job: say design and sell t-shirts to raise money for an organization You would want to specify the tasks to be done You would also want to specify who will work on each task Intro-OO 3 Slide 4 Task List Design T-Shirts What will people buy? What will look good? What would be too expensive? Make the T-Shirts Where to get them? How to add the design? How long will they take to make? Sell T-Shirts How much to sell them for? When and where to sell? How do you keep track of who sold what and who ordered what? Intro-OO 4 Slide 5 Job List Designers Responsible for designing the t-shirts Manufacturers Responsible for making the t- shirts Sellers Responsible for selling the t- shirts Intro-OO 5 Slide 6 Objects have Responsibilities Intro-OO 6 An object-oriented design Determines the tasks to be done Determines what objects will be responsible for each task No one object does everything Objects work together to accomplish tasks The assignment of responsibilities is the key skill in object- oriented design Slide 7 What is an Object? A person, place, or thing That knows something about itself Has data (attributes, fields) A cashier has a id, name, and a password And can do something Has operations (methods) A cashier can total the items, take payment, make change Intro-OO 7 Slide 8 What is a Class? The type of an object The way we classify an object The Idiot by Dostoevsky is a book War and Peace by Tolstoy is a book Mary is a cashier Tasha is a cashier Grouping of objects with the same data and operations Intro-OO 8 Slide 9 Class: Example Mary is a cashier Tasha is a cashier Cashier is a class All cashiers have an id, name and password Each will have a different id, name, and password All cashiers can total an order, take payment, make change Intro-OO 9 Slide 10 Object Data Each object has its own data Tashas id is 4 and password is mhall Marys id is 3 and password is smile4 All cashier objects have an id, name, and password Changing Marys data wont affect Tashas data Intro-OO 10 Slide 11 Teaching Objects and Classes Point 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-OO 11 Slide 12 Find Object Types Exercise List the types of objects in this picture Intro-OO 12 Slide 13 History of Objects and Classes Platos Republic (~375 BC) theory of forms Let 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-OO 13 Slide 14 History of Inheritance Intro-OO 14 Aristotle Parts of Animals (350 BC) describes inheritance. Linnaeuss Species Plantarum (1753) applied inheritance systematically and is the basis for modern botanical nomenclature. Mammal CatDog Slide 15 C++ Intro-OO 15 Developed by Bjarne Stroustrup at Bell Labs in 1985Bjarne Stroustrup created a highly-efficient version of Simula by extending the C language very popular in the late 80s to 90s still popular for 3d graphics Slide 16 Java Intro-OO 16 In 1991, Sun Microsystems began an internal project to produce a language that could run on intelligent consumer electronic devices James Gosling created the programming language Oak for this project as a highly portable, object-oriented programming language. James Gosling Oak evolved into Java and was released in 1995. Very popular Slide 17 Simulation Intro-OO 17 Object-oriented development means creating a simulation of the problem We need to know the objects in the problem So we can create software objects to represent them We need to know the types of the objects (classes) So we can define the data and the operations for all objects of that type Slide 18 Classes Create Objects The class can be thought of as a recipe, blueprint, or factory Many objects can be created from one class Objects keep track of the class that created them I am an object (instance) of the Cookie class Intro-OO 18 Slide 19 Classes Define the Objects Intro-OO 19 The computer doesnt know what we mean by a car or cashier We define the class Cashier so that the computer will understand what a cashier or bank account is and what it can do In the context of the problem we are trying to solve Then the computer can create objects from the classes Slide 20 Abstraction Intro-OO 20 Pull out only the important details about the thing we are simulating Cashiers have hobbies but we dont need to know about them Slide 21 What do Objects Look Like? Objects are created with space for their data (Instantiated) Object have a reference to the object that represents the class Object of the class Class Intro-OO 21 Mary : Cashier id = 3, name=Mary password = smile4 Tasha : Cashier id = 4 Name=Tasha password = mhall Name = Cashier Methods = totalOrder() takePayment(payment) makeChange() Cashier : Class Slide 22 Software Objects are Models The objects we create in software are models of the physical object We cant 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 Intro-OO 22 Cashier id name password Slide 23 Communicating Objects Objects in the simulation need to communicate They send each other messages Messages cause methods (operations) to be executed Methods are written with parameters to represent variable data determined by the caller Methods are passed arguments when they are called Intro-OO 23 Clean your room, please later Slide 24 Data Responsibility Objects are responsible for their data The data should not be allowed to get into an invalid state Like withdrawing more money than is in a back account Intro-OO 24 Slide 25 Encapsulation Data and operations are combined in classes All changes to data should be done by methods in the class Making sure that the data stays valid Data should be private Intro-OO 25 data methods data methods message Slide 26 Why use Encapsulation? Intro-OO 26 If something goes wrong we know where the trouble is Some class didnt protect the data or didnt make sure the data was valid If something changes it is easy to find the class to fix If I need to add or change data the methods that work on it are together in the class If you need a method it is easy to check if such a method exists Slide 27 Data Hiding In OO Programming we hide data from objects in other classes No direct changing of data in objects of another class Objects send messages asking for operations to be done on the data They dont need to know how an object is going to do something as long as they do it Intro-OO 27 Slide 28 Intro-OO Inheritance Did you get features or abilities from your parents? People inherit physical characteristics Some people inherit abilities: music Inheritance in OO means receiving data and methods from your parent In Java you can only have one parent Slide 29 Intro-OO Inheritance in our Models One class can inherit from another Gets all the fields and methods The class you inherit from is called Parent, superclass, base class The class doing the inheriting is called Child, subclass, derived class Person Student Parent Child Slide 30 Intro-OO Teaching Inheritance Point out inheritance in common objects What is a book? What is a dictionary? Talk about the things that are the same Talk about the things that are different Slide 31 Intro-OO Polymorphism Literally: many forms In 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 Slide 32 Intro-OO How 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 message If a class can be subclassed then a variable declared to be of the parent type can point to an object of the parent class or any subclass at run-time the compiler cant determine the method to invoke the method to invoke is figured out at run-time Slide 33 The End!! Intro-OO