Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Concepts

Similar presentations

Presentation on theme: "Object-Oriented Concepts"— Presentation transcript:

1 Object-Oriented Concepts

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?) This contrasts with procedural programming in which we focus on the task that needs to be done. You may break that task up into smaller parts. In object-oriented programming we need to know who (what class) as well as what (procedures). Object-oriented programming is concerned with responsibility. Who (what object) is responsible for doing the job, not just what jobs need to be done. 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 Talk about why it is important to say who will do each task? How do you decide who will do the task? Why is it important to know what needs to be done? Is it a good idea to assign all the tasks to one person? Is it a good idea to assign tasks based on skills people have? Intro-OO

4 Task List Design T-Shirts Make the T-Shirts Sell 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? Each task may have one or more sub-tasks. You might assign a person to each sub-task. Intro-OO

5 Job List Designers Manufacturers Sellers
Responsible for designing the t-shirts Manufacturers Responsible for making the t- shirts Sellers Responsible for selling the t- shirts This is one way to organize who will do the work and what to call each category (class). You would probably have 1 or more designers, several manufacturers, and many sellers. 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 Point out objects in the room (like a book, pen, computer, person, etc). Talk about what type of object it is, what data it has, what it can do. Go through your example real-world scenarios. Point out the objects, their types, data, and operations. The picture is a cashier in a music store. The data is his name and password. The operations are how to “ring up” and “take payment”, and “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 Objects are instances of a class. This slides shows two book objects of the class book. 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 Object data is also called instance variables, object variables, and object fields. 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? There are probably computers in your room. What do we mean by a “computer”? What data does it have (manufacturer, size screen, size memory, etc). What can it do? Point out that nobody had to tell the students that these where computers. How did they know? They fit their idea of what a computer looks like. 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. Plato’s Republic is online at Each bed is an object (instance) of the bed class. 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 Aristotle’s Parts of Animals is online at Information on Carl Linnaeus - Carl von Linné can be found online at 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 Bjarne Stroustrup. The Design and Evolution of C++. Reading, MA: Addison-Wesley, 1994. Bjarne Stroustrup, “A History of C++”. History of Programming Languages (HOPL-II). J. E. Sammet. New York, ACM: , 1993. Bjarne Stroustrup’s home page is at 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 James Gosling’s home page is at 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 We can’t put the “real” objects into the computer so we need to make software objects that represent the “real” objects. 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 When you create an object it has a reference to the object that represent the class that created it. So, objects always know what type (class) they are. 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() To get the class object that represents the class use object.getClass(); To check if an object is an instance of a class use instanceof(className); There is actually an array of Method objects that holds the methods for a class. There is also an array of Field objects which describes the fields in the class. 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 If I get a message from someone asking me to do something I can choose to do the action or not. 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 If an object in class A wants to change the data in an object of class B it needs to ask the object of class B to do the change (by sending a message). The object of class B can refuse to do the change. 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 In procedural or functional programming you create functions or procedures that act on data and any function or procedure can act on any data. So, when data is in an invalid state you don’t know which procedure or function caused the problem. You have to check all the code that is passed the data to find where the problem occurred. In large procedural or functional projects it is easy for people to create several functions that do the same or similar things because they aren’t aware of the others. 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 We can also think of this as security. An object keeps its data secure and doesn’t let objects of other classes mess with the data. A customer object could ask a bank account object to withdraw more money than is in the bank account but the bank account object won’t let it. The customer object doesn’t even need to know how the bank account object stores the current balance. The customer object can’t directly change the current balance. 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 This type of diagramming is called UML which is the Unified Modeling Language and is a standard way to show object-oriented designs. 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 Some other ideas are (teacher, math teacher), (school, elementary school, middle school, high school) 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

Download ppt "Object-Oriented Concepts"

Similar presentations

Ads by Google