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

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

Python Objects and Classes
CIT 590 Intro to Programming Classes. Schedule change The upcoming HW (HW6) is your last major Python HW. It will involve object oriented programming.
Object-Oriented Programming
Chapter 19, 20 Object Oriented Programming (OOP) Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Object Oriented Programming Intermediate VB Doug Waterman Fox Valley Technical College.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
Object-oriented Programming Concepts
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object Oriented Concepts. Movement toward Objects Instead of data-oriented or process-oriented Analysis, many firms are now moving to object-oriented.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Fundamentals of Python: From First Programs Through Data Structures
BACS 287 Basics of Object-Oriented Programming 1.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
OBJECT AND CLASES: THE BASIC CONCEPTS Pertemuan 8 Matakuliah: Konsep object-oriented Tahun: 2009.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Object Oriented Programing (OOP)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Object Oriented Programming In Python
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
OOPS CONCEPT.  OOPS  Benefits of OOPs  OOPs Principles  Class  Object Objectives.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Exceptions and Handling
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming
OOP: Encapsulation &Abstraction
Guide to Programming with Python
Object-Oriented Modeling
The Movement To Objects
3 Fundamentals of Object-Oriented Programming
Python Classes By Craig Pennell.
MSIS 670 Object-Oriented Software Engineering
Object Oriented Analysis and Design
Presentation transcript:

Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009

Objects as Models object-based. 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  State (information stored within the object)  Behavior  Behavior (operations that can be performed on the object)

Example 1: Ball-point Pen state 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 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 pen’s cartridge.  Determine how much ink remains in the pen.

Example 2: Bank Account state 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 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.

Example 3: Car state 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 car’s tank? Operations Operations on a car include:  Start the engine.  Drive a specified distance.

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

What is OOP Python OOP is entirely optional! object-oriented To qualify as being truly object-oriented objects generally need to also participate in something called an inheritance hierarchy. Inheritance objectsInheritance 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.

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

Why Classes Class Class is for implementing new kind of objects (state+behavior). Multiple Instances 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 Customization via Inheritance  Classes are extended by redefining their attributes outside class itself (namespace hierarchy) Operator Overloading Operator Overloading  New class can implement built-in type operations.

Attribute Inheritance Search Object.attributefirst attributeobject Object.attribute:”find 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 Inheritance: objects lower in a tree inherit attributes of the objects higher in a tree Classes Classes - instance’s factory: provide default behavior Instances Instances – concrete items: record data that varies per specific object.

Class tree superclasses (parents) subclass override Search is bottom-up: subclass may override parent’s 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. namespacesClasses and instances are namespaces. Objects in class tree have “automatically-search” links to other namespace objects.

Coding the Class Tree class class statement generates a new class class (superclass,…): data=value #shared class data data=value #shared class data def (self,…): # Methods def (self,…): # Methods self.status=data self.status=data 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.

Coding the Class Tree class class C2: self def x(self): print self.account self def z(self,data): self.acc=data class class C3: self def w(self,data): self.acc+=data self def z(self,data): self.acc-=data class class C1 (C2,C3): self def x(self,data): self.acc-=data self def y(self, time): print “at ”+time+”:”+self.name +” has ”+ str(self.acc)I1=C1() I1.name=“O’Reily” I1.z(123) I1.y(“09/10/08”)I2=C2()

Self defmethod def inside class is called as method self Method automatically receives a special first argument – self - which provides a handle back to the instance to be processed. class class C1: self def setname(self,name): self.name=name def printname(self): print self.name I1=C1(); I2=C1(); I1=C1(); I2=C1(); 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.

__init__, __init__ constructor 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 class C1: __init__ def __init__(self, name): self.name=name def printname(self): print self.name I1=C1(“Genry”); I2=C1(“Garry”) I1.printname(); I2.printname()

Class Objects: Default Behavior class The class statement creates a class object and assigns it a name. class 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 class C1: state=value self def __init__(self,name): self.name=name def printName(self): print self.name

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

Example 1 x.data=“change explicitly” x.newAttr=“less common:)”

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. class Classes inherit attributes from their superclasses listed in parenthesis in the header of class. Instances inherit attributes from all accessible classes. object.attrribute Each object.attrribute invokes a new search. subclassing Logic changes are made by subclassing, not by changing superclasses.

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

Calling Superclass Constructor __init__ __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.