Chapter 19-21 Object Oriented Programming (OOP) - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Chapter 19-21 Object Oriented Programming (OOP)

Description:

Title: PowerPoint Presentation Last modified by: Dinesh Created Date: 1/1/1601 12:00:00 AM Document presentation format: On-screen Show Other titles – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 22
Provided by: csGsuEdu64
Learn more at: https://www.cs.gsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 19-21 Object Oriented Programming (OOP)


1
Chapter 19-21Object Oriented Programming (OOP)
  • CSC1310 Fall 2009

2
Objects as Models
  • Code is object-based.
  • A program can be thought of as a model of
    reality, with objects in the program representing
    physical objects.
  • Properties of objects
  • State (information stored within the object)
  • Behavior (operations that can be performed on the
    object)

3
Example 1 Ball-point Pen
  • The state of a ball-point pen with a retractable
    point can be represented by two values
  • Is the point of the pen exposed?
  • How much ink remains in the pen?
  • Operations on a pen include
  • Press the button at the end of the pen.
  • Move the pen with the point held against a sheet
    of paper.
  • Replace the pens cartridge.
  • Determine how much ink remains in the pen.

4
Example 2 Bank Account
  • A state of a bank account includes the account
    number, the balance, the transactions performed
    on the account since it was opened, and so forth.
  • Operations on a bank account include
  • Deposit money into an account.
  • Withdraw money from the account.
  • Check the balance in the account.
  • Close the account.

5
Example 3 Car
  • The state of a car includes the amount of fluids
    in the car, the state of the tires, and even the
    condition of each part in the car.
  • For programming purposes, we can focus on just a
    few elements of the state
  • Is the engine on?
  • How much fuel remains in the cars tank?
  • Operations on a car include
  • Start the engine.
  • Drive a specified distance.

6
Artificial Objects
  • Nearly every real-world object can be modeled
    within a program.
  • Programmers also work with artificial objects
    that dont correspond to objects in the physical
    world (mathematical objects function, fraction,
    data record)
  • Like all objects, these artificial objects have
    state and behavior.

7
What is OOP
  • Python OOP is entirely optional!
  • To qualify as being truly object-oriented objects
    generally need to also participate in something
    called an inheritance hierarchy.
  • Inheritance is a mechanism of code customization
    and reuse. It is a way to form new classes
    (instances of which are called objects) using
    classes that have already been defined.

8
What is OOP
  • Polymorphism means that meaning of operation
    depends on the object being operated on.
  • Encapsulation means packaging in Python methods
    and operators implement behavior data hiding is
    a convention by default.

9
Why Classes
  • Class is for implementing new kind of objects
    (statebehavior).
  • Multiple Instances
  • Class factory for generating objects.
  • When class is called new object is generated
    with its namespace. It has access to class
    attributes, but keeps own data.
  • Customization via Inheritance
  • Classes are extended by redefining their
    attributes outside class itself (namespace
    hierarchy)
  • Operator Overloading
  • New class can implement built-in type operations.

10
Attribute Inheritance Search
  • Object.attributefind the first occurrence of
    attribute by looking in object, and all classes
    above it, from bottom to top and left to right.
  • To find an attribute, search a tree
  • Inheritance objects lower in a tree inherit
    attributes of the objects higher in a tree
  • Classes - instances factory provide default
    behavior
  • Instances concrete items record data that
    varies per specific object.

11
Class tree
superclasses (parents)
  • Search is bottom-up subclass may override
    parents behavior, by redefining the attribute
    (for, example C1.x)
  • I2 inherits w from C3.
  • I1.x, I2.x get from C1.
  • I1.z, I2.z find z in C2.
  • I2.name finds name in I2.

subclass
  • Classes and instances are namespaces.
  • Objects in class tree have automatically-search
    links to other namespace objects.

12
Coding the Class Tree
  • class statement generates a new class
  • class ltnamegt(superclass,)
  • datavalue shared class data
  • def ltmethodgt(self,) Methods
  • self.statusdata
  • Each time a class is called, new instance is
    created.
  • Instances are automatically linked to the class
    they are created from
  • Classes are linked to their superclasses.
    Left-to-right order in parenthesis gives the
    order in tree.

13
Coding the Class Tree
  • class C2
  • def x(self) print self.account
  • def z(self,data) self.accdata
  • class C3
  • def w(self,data) self.accdata
  • def z(self,data) self.acc-data
  • class C1 (C2,C3)
  • def x(self,data) self.acc-data
  • def y(self, time) print at
  • timeself.name has
  • str(self.acc)
  • I1C1()
  • I1.nameOReily
  • I1.z(123)
  • I1.y(09/10/08)
  • I2C2()

14
Self
  • def inside class is called as method
  • Method automatically receives a special first
    argument self - which provides a handle back to
    the instance to be processed.
  • class C1
  • def setname(self,name) self.namename
  • def printname(self) print
    self.name
  • I1C1() I2C1() I1.setname(Genry)
    I2.setname(Garry)
  • I1.printname() I2.printname()
  • When method assigns to self attributes, it
    creates or changes an attribute of the instance
    being processed.

15
__init__
  • If coded and inherited, __init__ is called
    automatically each time an instance is generated
    (constructor).
  • Ensure that attributes are always set in
    instances without requiring extra calls.
  • class C1
  • def __init__(self, name) self.namename
  • def printname(self) print
    self.name
  • I1C1(Genry) I2C1(Garry)
  • I1.printname() I2.printname()

16
Class Objects Default Behavior
  • The class statement creates a class object and
    assigns it a name.
  • Assignments inside class statement make class
    attributes.
  • Class attributes provide object state and
    behavior
  • Record state and behavior to be shared among
    instances
  • Methods process instances.
  • class C1
  • statevalue
  • def __init__(self,name)
    self.namename
  • def printName(self) print
    self.name

17
Instance Objects Concrete Items
  • Calling a class object makes a new instance
    object.
  • Each instance object inherits class attributes
    and gets its own namespace.
  • Assignments to attributes of self in methods make
    per-instance attributes.

18
Example 1

x.datachange explicitly x.newAttrless
common)
19
Hierarchy of Classes
  • Subclasses allow to change default behavior
    without changing component in place.
  • Subclasses allow to have different implementation
    of the same method depending on the object.
  • Subclass may replace inherited attributes
    completely, provide attributes that superclass
    expects to find, extend superclass methods.
  • Classes inherit attributes from their
    superclasses listed in parenthesis in the header
    of class.
  • Instances inherit attributes from all accessible
    classes.
  • Each object.attrribute invokes a new search.
  • Logic changes are made by subclassing, not by
    changing superclasses.

20
Example2
  • Specialization of the second class is external
    for the first, it does not affect it.

21
Calling Superclass Constructor
  • __init__ is looked up by inheritance
  • If subclass constructor need to guarantee that
    superclass constructor runs too, it needs
    explicitly call it.
  • class Parent
  • def __init__(self, x)
  • class Child(Parent)
  • def __init__(self, x,y)
  • Parent.__init__(self,x)
  • Without explicit call, subclass constructor
    replaces superclass constructor completely.
Write a Comment
User Comments (0)
About PowerShow.com