Object Oriented Programing (OOP)

Slides:



Advertisements
Similar presentations
When is Orientated Programming NOT? Mike Fitzpatrick.
Advertisements

Python Objects and Classes
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.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Lists Introduction to Computing Science and Programming I.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Guide to Programming with Python
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Introduction to Object-Oriented Programming
4.1 Instance Variables, Constructors, and Methods.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
Working With Objects Tonga Institute of Higher Education.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Inheritance A Review of Objects, Classes, and Subclasses.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Working With Objects Tonga Institute of Higher Education.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Introduction to Object-Oriented Programming Lesson 2.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Classes and OOP.
Topics Procedural and Object-Oriented Programming Classes
Object Oriented Concepts -I
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Chapter 3 Introduction to Classes, Objects Methods and Strings
IFS410: Advanced Analysis and Design
Teach A-Level Computer Science: Object-Oriented Programming in Python
Learning Objectives Classes Constructors Principles of OOP
Defining Classes and Methods
Barb Ericson Georgia Institute of Technology Oct 2005
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
Introduction to Object-Oriented Programming
Defining Classes and Methods
Abstraction and Objects
C++ Object Oriented 1.
Presentation transcript:

Object Oriented Programing (OOP) Intro2CS – week 7b

What is Object Oriented Programing? A programing paradigm that is based on data, and code that are coupled together. Objects store information and carry the code that accesses and manipulates information. Our first example will be the Car class. The class Car is the type. Specific cars of that class are objects (instances).

How “Car” will be used Create a new car and another… Do things with it Get information about it Things to notice: When we “use” the car, it changes its internal state (millage increases, gas is consumed) Cars “remember” things about their current state, have functions (methods) that can change that state. Each car is separate.

Class vs Object Defines a new class named Car. Class names should have capital letters in the beginning of each word. examples:: Car, BigNumber, ChessBoard Additional statements will come here. Create two instances (objects) of type Car. Notice the parentheses: ()

To check if an object is of a certain instance: This is usually not good code.

Class vs Object The class “Car”: <class '__main__.Car'> Add a field to each object noting the amount of fuel in the car. Each object has its own value. These are called by many names: member variables, member fields, attributes

Class vs Object A class variable. Shared by all objects. Output:

Initializing objects The init method. Runs whenever a new object is created. Sometimes called a constructor Here is the argument given to __init__. Self is the newly created object Output:

Initializing objects Variables within the class definition are class variables. Output:

Objects Have State The data inside objects represents their state The state is changed by performing actions on the object: Invoking “methods” (methods are functions that run and may change its state)

Methods Functions within the class definition are called methods, or member functions. They add functionality to the objects. The first parameter is usually called self. It’s the object they run on Here is how they are invoked.

These two calls are equivalent

Hiding the internal representation Good object oriented programming helps objects behave in a consistent manner Example: No way to increase the mileage of the car without driving it (and burning fuel) If the programmer directly changes car1.mileage=-2 he can make mistakes. It is therefore good practice to write objects that hide internal data and only change as a result of their own method calls.

Hiding the internal representation Private members are denoted with a double underscore. access data only through methods. Will not work. Variable is still there, but has actually been renamed to _Car__fuel

Hiding the Internals If the class is written properly, it will be impossible to change the internal state to something inconsistent. A car with negative fuel? A car with negative milage?

Ensuring internal consistency Similarly with get_milage

Application Interfaces (APIs) and Design by Contract The methods of the class define its API – how programmers interact with its instances The “design by contract” paradigm: APIs make promises about the behavior of objects (but without committing to internal representation). Encapsulation: Programmer needs only the interface, doesn’t care about internals.

All Things are Objects

Polymorphism Actions can sometimes be applied to objects of many different types. This is very natural in Python.

Polymorphism Actions can sometimes be applied to objects of many different types. This is very natural in Python. (There are actually better ways to do this in OOP using inheritance)

Documenting classes Write docstrings for each method in the class. Write what the API promises regarding each method Write a docstring at the beginning of the class Here you should also document attributes, but only the public ones

Objects are Mutable No “return” of car object. What happens?

Another Example

Now we can work directly with the point and rectangle classes.

Adding more behavior to objects We’d like something nicer to be printed

The __str__ method is called when a conversion to a string is needed.

Defining how operators behave False The default behavior of == on objects it to check if the are the exact same object, ignoring data members.

To redefine how == behaves, simply implement the method __eq__() within the class.

Other operators We can define other magic methods in python that make other operators work with our code There are more… + object.__add__(self, other) - object.__sub__(self, other) * object.__mul__(self, other) // object.__floordiv__(self, other) / object.__div__(self, other) % object.__mod__(self, other) ** object.__pow__(self, other[, modulo]) << object.__lshift__(self, other) >> object.__rshift__(self, other) & object.__and__(self, other) ^ object.__xor__(self, other) | object.__or__(self, other) < object.__lt__(self, other) <= object.__le__(self, other) == object.__eq__(self, other) != object.__ne__(self, other) >= object.__ge__(self, other) > object.__gt__(self, other)

An example Python has a set object (a container that doesn’t allow items to be included twice) But what if it didn’t? How would we implement one?

This implementation is inefficient. (Why?) How can we make it better using things we’ve seen?

Gets the “in” operator to work Gets the == operator to work Gets the + operator to work Gets the str() conversion to work Gets the len() function to work

Another example

What assumptions are we making about items we add in?