Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedural Abstraction Object-Oriented Code

Similar presentations


Presentation on theme: "Procedural Abstraction Object-Oriented Code"— Presentation transcript:

1 Procedural Abstraction Object-Oriented Code
© 2014 Project Lead The Way, Inc. Unpublished work © 2013 Project Lead The Way, Inc.

2 Abstraction Remove details Generalize
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Abstraction Remove details Generalize Example: Sink faucet – we know how to use it, but we often ignore details of how it works Easier to handle complexity if we don’t think of all details at once Abstraction is when you remove details and generalize to a broader category. As an example, consider a sink faucet. Left is hot water; right is cold water. We don’t need to know anything about how the valve works or how the water is put under pressure. You turn the faucet handle, and water comes out. Different cities and rural areas have different system for delivering the water. Different faucets have different internal parts. But we generalize away from those details and have an idea that can be applied to a large group of faucets: left is hot water; right is cold water. Counter-clockwise is on; clockwise is off.

3 Abstraction Separation of concerns
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Abstraction Separation of concerns Example: Sink faucet – we know how to use it, but we often ignore details of how it works Easier to handle complexity if we don’t think of all details at once Abstracting like that – ignoring details – lets us solve complex problems by focusing on one layer of abstraction. If I’m a water-color artist, I get water from the sink but concentrate on painting. The plumber concentrates on the faucet layer; the civil engineer solves the water supply issues. Separation of concerns with clean interfaces between them – that is how we build complex systems.

4 Bits on Tape Presentation Name Course Name
Unit # – Lesson #.# – Lesson Name Bits on Tape In the computing machines of the 1950s and 60s, bits were fed into computers on paper cards or rolls of tape. In the 70s, magnetic tape became more common. We aim to understand how higher levels of abstraction come from lower levels of abstraction. The tape of binary digits represents numbers, instructions, character strings, … and then objects. How does it do that? The abstraction to get us from here --binary digits on tape-- to object-oriented instructions is an important one!

5 Presentation Name Course Name Unit # – Lesson #.# – Lesson Name The Earliest Objects On those tapes data were sometimes recorded along with a group of procedures. The procedures had instructions for the processor to read information from the data. The procedures had instructions to update the information in the data. The procedures were written one after another. Pointers were written at the front of the tape. A pointer is an address in memory. The pointer listed “jumping-in” points for the procedures section, saying how many rows into the tape you could enter each procedure. © 1993 ACM, The Early History of Smalltalk by Alan Kay

6 Object-Oriented Code Attributes Methods Presentation Name Course Name
Unit # – Lesson #.# – Lesson Name Object-Oriented Code Attributes We call the data “attributes” of an object. We call the procedures “methods”. Methods

7 Programming Paradigms
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Programming Paradigms Imperative Programming One statement after another Procedures used to organize code Functional Programming Statements grouped in functions Functions transform data Object-oriented Programming Data associated with its own procedures In the 1990s object-oriented code became dominant. The languages C++ (starting in 1985) and Java (starting in 1996) encourage programmers to use the object-oriented programming paradigm.

8 Object-Oriented Code Objects Class Attributes Class Data
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Object-Oriented Code Objects Class Data Object-oriented code lets you use the method procedures over and over, applied to different objects of the same kind. To create a class, the computer executes a class definition. It stores the shared method procedures and shared data (click), the part shown in this box. A method can be executed for any object in the class. The computer stores the procedures once for the entire class, as shown here. No need to have all those instructions duplicated! Setting up one object is called instantiation. One of the class’ procedures contains the instructions for setting up the object; it is a constructor. The procedures are the methods of a class. This picture shows three methods (and then some). This picture shows a class that has four instances. The objects store data in fields, called instance fields. Each object has its own data for each field. The class can store data too, but there’s only one copy, so all objects share the same data in the class fields. A class is like a blueprint. You can create a lot of objects with one blueprint. Attributes Class

9 Objects Are Instances of a Class Objects
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Objects Are Instances of a Class Objects Data Each object is an instance of the class. “Calling a method on an object” means “executing a procedure using the data of a particular object.

10 Uniform Markup Language
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Uniform Markup Language A standard way to describe the classes defined in a program is Unified Markup Language. UML defines conventions for diagrams to describe code at a high level. A UML class diagram shows the class name, the attribute names, and the method names.

11 Class Definitions in Python
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Class Definitions in Python To instantiate: In Python we define a class with the class keyword. The name of the class is capitalized by convention. This class definition defines two attributes: height and weight. This class definition defines the constructor, which always is called __init__(). You always call the __init__() constructor by using the name of the class. In the example shown here, the object alex is created. The object alex is a Student, an instance of the class Student. Alex has attributes height (72) and weight (130, the default). Additional notes as advanced topics: These details are included here only to “take the ceiling off.” Some specific things to point out: 1. Classes can be extended by making a subclass. Line 1 says that Student will be a subclass of the most general Python class, “object”. Object is the superclass of Student. Student is the subclass of object. 2. The "self argument will be discussed in a later slide. All methods in a class have “self” as the first argument. For the __init__ method, “self” is the object being created. For all the other methods, "self" is the object on which the method is being called. When you call a function that is defined as a method of a class, the “self” argument is added by the interpreter. 3. The __init__ method usually creates some attributes. In this example, lines 3 and 4 create the attributes "height" and "weight" for the object being instantiated. The height and weight on the right side of those equal signs only refer to the arguments passed to the constructor. Those values only can be referred to inside the def block, and then they will be garbage collected. To remember the arguments passed to the constructor (like the 72 in the example), the values have to be assigned to an attribute of the new object “self”.

12 Using attributes of a class
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Using attributes of a class To refer to an attribute: Once an object is created, you can access its attributes with the object.attribute notation.

13 Using methods of a class
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Using methods of a class To call a method: You can call a method on an object with object.method() .

14 Using methods A common error: using too many arguments
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Using methods A common error: using too many arguments All methods for a class have “self” as the first argument. “self” is the object being created or the object on which the method is being called. When you call a function from a class definition, the “self” argument is added by the interpreter. Knowing about the “self” argument can help you understand errors like the one shown here. In input #50, the grow method has been called with two arguments: 72 and 1. The interpreter added “self”, making 3 arguments in total. The error message tells you that only 2 arguments are allowed: self and one more.


Download ppt "Procedural Abstraction Object-Oriented Code"

Similar presentations


Ads by Google