Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
C++ Classes & Data Abstraction
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
UML – Class Diagrams.
Object-Oriented Databases v OO systems associated with – graphical user interface (GUI) – powerful modeling techniques – advanced data management capabilities.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Fundamentals of Python: From First Programs Through Data Structures
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Inheritance in the Java programming language J. W. Rider.
Chapter 12 Support for Object oriented Programming.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Concepts
Object-Oriented Modeling
The Movement To Objects
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Interface, Subclass, and Abstract Class Review
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Types of Programming Languages
Object-oriented Design in Processing
IFS410: Advanced Analysis and Design
Object-Oriented Programming
Week 3 Object-based Programming: Classes and Objects
Object-Oriented Programming
Object-oriented Design in Processing
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
CIS 199 Final Review.
Object-oriented Design in Processing
Programming For Big Data
Object-oriented Design in Processing
Presentation transcript:

Object-Oriented Programming Python

OO Paradigm - Review Three Characteristics of OO Languages Inheritance It isn’t necessary to build every class from scratch – attributes can be derived from other classes Polymorphism/virtual functions Objects that belong to subclasses of the same superclass can be treated as if they are all objects of the superclass. Encapsulation Object behavior and object data are combined in a single entity. Object interface defines interaction with the object; no need to know/understand the implementation (and in general no way to access the implementation.)

Polymorphism Polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. http://en.wikipedia.org/wiki/Type_polymorphism; 3/29/2010

Object Orientation in Python In Python, everything is an object – integers, strings, dictionaries, … Class objects are instantiated from user- defined classes, other objects are from language defined types.

Python Classes Can be defined anywhere in the program All methods and instance variables are public, by default The language provides “limited support” for private identifiers (see section 9.6 in the documentation).

Example class MyClass: def set(self, value): self.value = value def display(self): print(self.value) MyClass has two methods: set and display; and one attribute: value. The class definition is terminated by a blank line.

Example The first parameter in each method refers to the object itself. Self is the traditional name of the parameter, but it can be anything. def set(self, value): self.value = value When the method is called, the self parameter is omitted

Python Class Objects y = MyClass() There is no new operator; to instantiate an object for a class, use function call notation: x = MyClass() y = MyClass() Each time a class is called, a new instance object is created. If the class has a constructor, you can use it here.

Example Declare and assign value to a class variable: >>> y = MyClass()#”declare” a MyClass object >>> y.set(4) # no param corresponding to >>> y.display() # “self” in either call 4 >>> y.value

Constructor - Example Constructors are defined with the __init__ method. Instead of def set(self, value): use def __init__(self, value):

Constructors def __init__(self): self.value = 0 # default or def __init__(self, thing): self.value = thing # parameterized Usage: x = MyClass( ) sets x.value to 0 y = MyClass(5) sets y.value to 5 (default constructor and parameterized constructor)

Class with Constructor >>> class Complex: def __init__(self, realp, imagp): self.r = realp self.i = imagp >>> x = Complex(3.0, -4.5) >>> x.r, x.i (3.0, -4.5)

Attributes (Data Members) Attributes are defined by an assignment statement, just as variables are defined (as opposed to being declared). def set(self, value): self.value = value Can be defined in classes or instances of classes. Attributes attached to classes belong to all subclasses and instance objects, but attributes attached to instances only belong to that instance.

Python Class Data Members Variables in Python are created when they are assigned to. New data members (attributes) can be created the same way; e.g., x.counter = 1 creates a new data attribute for the object x – but not for y.

Example (from online documentation) Variables in Python are created when they are assigned to New data attributes can be created the same way: . . . x.counter = 1 while x.counter < 10: x.counter = x.counter * 2 print(x.counter) del x.counter x.counter = 1 creates a new data attribute for the object x – but not for y.

Information Hiding There is no foolproof way to enforce information hiding in Python, so there is no way to define a true abstract data type Everything is public by default; it is possible to partially circumvent the methods for defining private attributes.

Inheritance class DerivedClassName(BaseClassName): <statement-1> . . . <statement-N> If a requested attribute is not found in the derived class, it is searched for in the base class. This rule is applied recursively if the base class itself is derived from some other class.

Multiple Inheritance Python supports multiple inheritance: class DerivedClassName(B1, B2, B3): <statement-1> . . . <statement-N>

Multiple Inheritance Resolving conflicts: (Given the expression object.attribute, find definition of attribute) Depth-first, left-to-right search of the base classes. “Depth-first”: start at a leaf node in the inheritance hierarchy (the object); search the object first, its class next, superclasses in L-R order as listed in the definition.

Illustration of Single & Multiple Inheritance B1 .a .b B2 .x B3 .y .b SUPERCLASSES B4 .x .q DERIVED CLASS I0 .x I1 I2 INSTANCES I0.a and I0.b are defined in B1; I0.x is defined in I0 I1.a, I1.b and I2.a, I2.b are defined in B1; I1.x & I2.x (and q) are defined in B4 I1.y and I2.y are defined in B3, and so forth.

Python and OO Programming See Section 9 in the Python tutorial for more information and examples. Chapter 13, section 5 has several examples of Python classes.