Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
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.
OBJECT ORIENTED PROGRAMMING M Taimoor Khan
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Chapter 1 Object-Oriented System Development
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Basic OOP Concepts and Terms
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object-oriented Programming Concepts
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object Oriented Programming in Python Richard P. Muller Materials and Process Simulations Center California Institute of Technology June 1, 2000.
Chapter 13: Object-Oriented Programming
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-oriented design CS 345 September 20,2002. Unavoidable Complexity Many software systems are very complex: –Many developers –Ongoing lifespan –Large.
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 Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
BCS 2143 Introduction to Object Oriented and Software Development.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CISC6795: Spring Object-Oriented Programming: Polymorphism.
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
SNPL1 Woochang Lim What (Variable) + How (Function) = Object Objects are the physical and conceptual things we find in the universe around us. Object-Oriented.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Basic OOP Concepts and Terms. In this class, we will cover: Objects and examples of different object types Classes and how they relate to objects Object.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
CIS224 Software Projects: Software Engineering and Research Methods Lecture 1b Object concepts (Based on Stevens and Pooley (2006), Chapter 2) David Meredith.
Kal Bugrara, Ph.DSoftware Engineering Northeastern University Fundamentals Of Software Engineering Lecture V.
CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts.
CS190/295 Programming in Python for Life Sciences: Lecture 7 Instructor: Xiaohui Xie University of California, Irvine.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
11-1 CHAPTER 11 Concepts for Object-Oriented Databases.
Salman Marvasti Sharif University of Technology Winter 2015.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 20 Concepts for Object-Oriented Databases Copyright © 2004 Pearson Education, Inc.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Object Oriented Programming Some Interesting Genes.
Object-Oriented Design
Polymorphism, Interfaces & Operator Overloading
The Object-Oriented Thought Process Chapter 1
Chapter 11 Object-Oriented Design
Object-Oriented Database Management System (ODBMS)
Week 4 Object-Oriented Programming (1): Inheritance
Object orientation concepts
Object Oriented Programming
3 Fundamentals of Object-Oriented Programming
Corresponds with Chapter 7
CS190/295 Programming in Python for Life Sciences: Lecture 7
Basic OOP Concepts and Terms
Object Oriented Analysis and Design
Object-Oriented Programming
Presentation transcript:

Object Oriented Programming CS115

Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called object-oriented design (OOD). The essence of OOD is describing a system in terms of magical black boxes and their interfaces.

The Process of OOD Each component provides a service or set of services through its interface. Other components are users or clients of the services. A client only needs to understand the interface of a service – implementation details are not important, they may be changed and shouldn ’ t affect the client at all!

The Process of OOD The component providing the service shouldn ’ t have to consider how the service is used – it just needs to provide the service “ as advertised ” via the interface. This separation of concerns makes the design of complex systems possible.

The Process of OOD Breaking a large problem into a set of cooperating classes reduces the complexity that must be considered to understand any given part of the program. Each class stands on its own! OOD is the process of finding and defining a useful set of classes for a given problem. Like design, it ’ s part art and part science. The more you design, the better you ’ ll get.

OO Concepts The OO approach helps us to produce complex software that is more reliable and cost-effective. OO is comprised of three principles: – Encapsulation – Polymorphism – Inheritance

Encapsulation As you ’ ll recall, objects know stuff and do stuff, combining data and operations. This packaging of data with a set of operations that can be performed on the data is called encapsulation. Encapsulation provides a convenient way to compose complex problems that corresponds to our intuitive view of how the world works.

Encapsulation From a design standpoint, encapsulation separates the concerns of “ what ” vs. “ how ”. The implementation of an object is independent of its use. The implementation can change, but as long as the interface is preserved, the object will not break. Encapsulation allows us to isolate major design decisions, especially ones subject to change.

Encapsulation Another advantage is that it promotes code reuse. It allows us to package up general components that can be used from one program to the next.. Encapsulation alone makes a system object- based. To be object-oriented, we must also have the properties of polymorphism and inheritance.

Polymorphism Literally, polymorphism means “ many forms. ” When used in object-oriented literature, this refers to the fact that what an object does in response to a message (a method call) depends on the type or class of the object.

Polymorphism With polymorphism, a given line in a program may invoke a completely different method from one moment to the next. Suppose you had a list of graphics objects to draw on the screen – a mixture of Circle, Rectangle, Polygon, etc.

Polymorphism You could draw all the items with this simple code: for obj in objects: obj.draw(win) What operation does this loop really execute? When obj is a circle, it executes the draw method from the circle class, etc.

Polymorphism Polymorphism gives object-oriented systems the flexibility for each object to perform an action just the way that it should be performed for that object.

Inheritance The idea behind inheritance is that a new class can be defined to borrow behavior from another class. The new class (the one doing the borrowing) is called a subclass, and the other (the one being borrowed from) is called a superclass. This is an idea our examples have not included.

Inheritance Say we ’ re building an employee management system. We might have a class called Employee that contains general information common to all employees. There might be a method called homeAddress that returns an employee’s home address.

Inheritance Within the class of employees, we might distinguish between salaried and hourly employees with SalariedEmployee and HourlyEmployee, respectively. Each of these two classes would be a subclass of Employee, and would share the homeAddress method.

Inheritance Each subclass could have its own monthlyPay function, since pay is computed differently for each class of employee. Inheritance has two benefits: – We can structure the classes of a system to avoid duplication of operations, e.g. there is one homeAddress method for HourlyEmployee and SalariedEmployee. – New classes can be based on existing classes, promoting code reuse.

Namespaces At the simplest level, classes are simply namespaces class myfunctions: def exp(): return 0 >>> math.exp(1) >>> myfunctions.exp(1) 0 It can sometimes be useful to put groups of functions in their own namespace to differentiate these functions from other similarly named ones.

Python Classes Classes define objects Objects are instances of classes class atom: def __init__(self,atno,x,y,z): self.atno = atno self.position = (x,y,z) __init__ is the default constructor self refers to the object itself, like this in Java.

example class atom: def __init__(self,atno,x,y,z): self.atno = atno self.position = (x,y,z) def __repr__(self): # overloads printing return '%d %10.4f %10.4f %10.4f' % (self.atno, self.position[0], self.position[1],self.position[2]) >>> at = atom(6,0.0,1.0,2.0) >>> print(at)

Discussion Overloaded the default constructor Defined class variables (atno,position) that are persistent and local to the atom object Good way to manage shared memory: – instead of passing long lists of arguments, encapsulate some of this data into an object, and pass the object. – much cleaner programs result Overloaded the print operator

© 2000 Richard P. Muller Classes that look like arrays Overload __getitem__(self,index) to make a class act like an array

Classes that look like functions Overload __call__(self,arg) to make a class behave like a function class gaussian: def __init__(self,exponent): self.exponent = exponent def __call__(self,arg): return math.exp(-self.exponent*arg*arg) >>> func = gaussian(1.) >>> func(3.)

Other things to overload __setitem__(self,index,value) – Another function for making a class look like an array/dictionary – a[index] = value __add__(self,other) – Overload the "+" operator – molecule = molecule + atom __mul__(self,number) – Overload the "*" operator – zeros = 3*[0] __getattr__(self,name) – Overload attribute calls – We could have done atom.symbol() this way

Other things to overload, cont. __del__(self) – Overload the default destructor – del temp_atom __len__(self) – Overload the len() command – natoms = len(mol) __getslice__(self,low,high) – Overload slicing – glycine = protein[0:9] __cmp__(self,other): – On comparisons (<, ==, etc.) returns -1, 0, or 1, like C's strcmp