1 / 33

Object-Oriented Concepts

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.

freira
Download Presentation

Object-Oriented Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Concepts Intro-OO

  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

  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

  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

  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

  6. Objects have Responsibilities • 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 Intro-OO

  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

  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

  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

  10. Object Data • Each object has its own data • Tasha’s id is 4 and password is mhall • Mary’s id is 3 and password is smile4 • All cashier objects have an id, name, and password • Changing Mary’s data won’t affect Tasha’s data Intro-OO

  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

  12. Find Object Types Exercise • List the “types” of objects in this picture Intro-OO

  13. History of Objects and Classes • Plato’s 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

  14. History of Inheritance • Aristotle Parts of Animals (350 BC) describes inheritance. • Linnaeus’s Species Plantarum (1753) applied inheritance systematically and is the basis for modern botanical nomenclature. Mammal Cat Dog Intro-OO

  15. C++ • Developed by Bjarne Stroustrup at Bell Labs in 1985 • 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 Intro-OO

  16. Java • 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. • Oak evolved into Java and was released in 1995. • Very popular Intro-OO

  17. Simulation • 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 Intro-OO

  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

  19. Classes Define the Objects • The computer doesn’t 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 Intro-OO

  20. Abstraction • Pull out only the important details about the thing we are simulating • Cashiers have hobbies but we don’t need to know about them Intro-OO

  21. What do Objects Look Like? Mary : Cashier Tasha : Cashier • 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” id = 3, name=“Mary” password = smile4 id = 4 Name=“Tasha” password = mhall Cashier : Class Name = Cashier Methods = totalOrder() takePayment(payment) makeChange() Intro-OO

  22. 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 Cashier id name password Intro-OO

  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 later Clean your room, please Intro-OO

  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

  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 data data methods methods message Intro-OO

  26. Why use Encapsulation? • If something goes wrong we know where the trouble is • Some class didn’t protect the data or didn’t 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 Intro-OO

  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 don’t need to know how an object is going to do something as long as they do it Intro-OO

  28. 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 Intro-OO

  29. 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 Parent Student Child Intro-OO

  30. 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 Intro-OO

  31. 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 Intro-OO

  32. 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 can’t determine the method to invoke • the method to invoke is figured out at run-time Intro-OO

  33. The End!! Intro-OO

More Related