330 likes | 462 Views
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.
E N D
Object-Oriented Concepts Intro-OO
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
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
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
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
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
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
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
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
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
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
Find Object Types Exercise • List the “types” of objects in this picture Intro-OO
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
The End!! Intro-OO