~~SLIDESHOW~~ ====== Object-Orientation Fundamentals ====== Mithat Konar ===== Why object-orientation? ===== * **Object-oriented programming** came about to support **object-oriented analysis and design** (OOAD). * OOAD created to solve issues involved in designing and managing large applications. ===== Procedural thinking ===== * Break down large problems into functions or procedures. * //What needs to happen?// * "Verb" oriented. ===== Object-oriented thinking ===== * Break down large problems into the components (objects) that make up the problem. * How do they behave? * How do they interact? * "Noun" oriented. ===== Object-oriented languages ===== * Possible to implement OOAD in any language. * Easier and more robust when language has support for defining and using objects: an **object-oriented language**. ===== Object-oriented concepts ===== * The concept of "object" in computing comes directly from the concept of “object” in the real world. * Critical ideas: * state * behavior * encapsulation and protection * self-governance * Let’s explore this with the example of a microwave oven. ===== State ===== * At any given instant, a microwave oven has a particular **state**. * Is it cooking? * At what power level? * How much cooking time is left? * What time does it think it is? * These attributes (and others) collectively define the oven’s state. ===== Behavior ===== * A microwave also has predefined **behavior**: * Push the “1” button → start cooking at maximum power for one minute. * Push the “+30 sec” button → add 30 seconds to the cooking time if it’s already cooking or start cooking for 30 seconds at maximum power. * Push a magical combination of buttons → set the internal clock. * These operations (and others) collectively define the oven’s behavior. * **interface**: the public-facing behavior (i.e., the operations a user can engage). ===== Encapsulation ===== * User changes the state of the oven only by engaging one or more of the operations in the oven’s interface. * Don’t need to know how a magnetron, the clock’s electronics, or anything else works to use the oven. * Only need to know what changes in state to expect from the “cook 1 minute” or “add 30 seconds” operations. * “I don’t care about how it works—I only need to know what it does,” is part of **encapsulation**. ===== Protection ===== * Oven internals are protected against unwanted fiddling by screws and scary labels—for a reason. * “No user serviceable parts inside.” * **protection**: keeping the user out of stuff that the user should not be allowed to access. ===== Objects ===== * The microwave oven takes responsibility for managing its own state using a set of predetermined behaviors: it’s **self-governing**. * **object-oriented language**: a language that let you create self-governing entities. * Self-governing entities are called **objects**. * **object**: a program entity that encapsulates state (via attributes) and behavior (via operations involving those attributes) for some meaningful abstraction. ===== Class-based object-orientation ===== * My Farberware 4241 microwave.\\ {{:python:about_python:farberware4241-200.jpeg?nolink|}} * All Farberware 4241s made from a master plan. * Defines what the Farberware 4241 //is//. * In computing terminology, the master plan is a **class**. * A class contains all the specifications needed to make a particular kind of object. ===== Class-based object-orientation ===== * Objects that have been created from classes are also called **instances**. * The process of creating an instance from a class definition is called **instantiation**. ===== Prototype-based object-orientation ===== * **Prototype-based object-orientation**: used in JavaScript and some other languages. * Currently less common than class-based OOP.