CSC 508 – Theory of Programming Languages, Spring, 2009 Week 3: Data Abstraction and Object Orientation.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Object-Oriented Programming
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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
OOP Languages: Java vs C++
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Programming in Java CSCI-2220 Object Oriented Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Object-Oriented Programming Chapter Chapter
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
ISBN Object-Oriented Programming Chapter Chapter
Chapter -6 Polymorphism
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Java Programming Language
CIS 199 Final Review.
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

CSC 508 – Theory of Programming Languages, Spring, 2009 Week 3: Data Abstraction and Object Orientation

Review of Object-Oriented Programming – Encapsulation A module encapsulates implementation code and data, exposing abstract interfaces to client code while hiding implementation details. Also called information hiding, this approach does not necessarily entail using objects. C++ namespaces, Java packages, Python packages and modules. It reduces conceptual load, provides fault containment, and provides component independence.

Interface Inheritance An interface defines operations and symbolic constants that specify operation signatures and semantics. C++ abstract class consisting of pure virtual functions and constant definitions, Java interface, Python class with method declarations that throw exceptions. Derived concrete classes implement the operations of a base class interface while hiding implementation details.

sequence_interface (ADT) – sequence of string objects cp ~parson/JavaLang/sequence_adt ~/JavaLang/sequence_adt > sequence_interface.java sequence_interface insert(value : string, offset : integer) : boolean remove(offset : integer) : boolean get(offset : integer) : string size() : integer

Concrete class sequence_arrayimpl implements sequence_interface > sequence_interface sequence_arrayimpl insert(), remove() get(), size() private String [] array ; private int count ; // number of array elements actually used

Class sequence_linkedlistimpl implements sequence_interface > sequence_interface sequence_linkedlistimpl insert(), remove() get(), size() private static class listelement { // private local helper class String content ; // string stored at this element listelement next ; // next element, higher in number than this one } private listelement first ; private int count ; // number of linked list nodes actually used

Implementation Inheritance An abstract or concrete base class provides fields, public methods and protected methods used in common by alternative implementations in derived classes. Protected fields and methods are accessible only to derived classes. They are part of shared implementation. They eliminate duplicate code. Public fields and methods are accessible to client code, and methods can be redefined or extended by derived classes.

Package sequence_aclass class sequence_abstract_baseclass > sequence_interface > sequence_abstract_baseclass size() : int protected int getRealOffset(int offset, boolean isinsert) : int (helper method to convert an offset parameter to a real offset) protected int count sequence_arrayimpl sequence_linkedlistimpl sequence_arraylistimpl

Subtype Polymorphism Derived classes that implement a common interface can take various forms. Each class can provide an implementation that is extended, customized or optimized for some class of client applications. Once object construction is complete, client access occurs via a pointer or reference to a base class or interface object. The concrete run-time objects accessed via such a pointer or reference have many forms.

Module encapsulation in Python A package takes the form of a directory containing one or more Python modules. The package name is a “.”-separated path name below a directory on PYTHONPATH. A module is a Python source file within a package directory with related data and code definitions. Clients must import components of a module.

Python Packages Assuming $HOME/ProcLang is on my PYTHONPATH, package mysort corresponds to directory $HOME/ProcLang/mysort. File __init__.py contains a list of every module name in the package in its __all__ variable. ~parson/ProcLang/mysort/__init__.py contains the assignment __all__ = ["mysort"] for module mysort.py. Most packages contain multiple modules. This list provides support for “from mysort import *” from other modules. It tells Python what modules to import.

Importing Python Modules Importing a module without importing explicit symbols requires using the module name as a prefix. >>> import sys ; sys.exit(1) Importing distinct names restricts the import and makes those name first class objects. from sys import exit ; exit(1) from sys import exit as byebye ; byebye(1) Importing “*” imports all modules in a package or all symbols in a module. Python does not report symbol collisions from multiple modules. It uses the most recent import.

Python Classes Classes are dynamic constructs in Python, like everything else. There is no compiler check to ensure definition of “abstract methods” in concrete derived classes (there are no abstract methods) or method signature compatibility between base and derived classes. Inherited symbol access is a series of dictionary lookups from derived to base classes. All fields and methods are public. Naming conventions document intended access limitations.

Classy Examples >>> class baseclass(object):... """ baseclass is an example def statmethod(): …print "statmethod in baseclass"... def __init__(self, val):... self.value = val... def f(self, newval):... oldval = self.value... self.value = newval... return oldval >>> class def statmethod(): print "statmethod in d1"... def __init__(self, val):... return super(d1, self).__init__(val) >>> D1 = d1(3) >>> D1.f(5) 3 >>> D1.f(7) 5

More Classy Examples >>> class d2(baseclass):... def f(self, newval):... oldval = self.value... self.value = newval return oldval >>> D2 = d2(5) >>> D2.f(1) 5 >>> D2.f(3) 11.0 >>> isinstance(D2, d2) True >>> isinstance(D2, baseclass) True >>> isinstance(D2, d1) False >>> issubclass(d1, baseclass) True >>> issubclass(d1, d1) True >>> issubclass(d1, d2) False

No method name overloading Both the constructor method, called __init__, and other methods within the class, must have unique names. There is no name overloading in Python. Calls to superclass __init__ are explicit in explicit order. The class determines construction order. You can supply default parameter values. Methods can test for argument types. Methods can use variable number of *positional and **keyword arguments. There are no destructors in Python. The garbage collector invokes __del__(self) on an object being recovered, but most classes do not redefine __del__.

Operator overloading and extension of built-in types You can overload Python operators such as + by defining special methods such as __add_ and __radd__ -- see PY Chapter 3. You can extend built-in types. >>> class coercive_int(int):... def __init__(self, value):... super(coercive_int,self).__init__(self, value)... def __add__(self, value):... print "Inside method __add__"... return int(self) + int(value)... def __radd__(self, value):... print "Inside method __radd__"... return int(self) + int(value)

User-extended types in expressions >>> ci = coercive_int(3) >>> ci 3 >>> ci Inside method __add__ 7 >>> int(ci) >>> coercive_int(ci) Inside method __add__ 7 >>> ci 7.0 >>> "12345" + ci Inside method __radd__ >>> "12345" + 3 Traceback (most recent call last): File " ", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects

Multiple Inheritance uses Mixins Each base class can be a fragment of a class that refers to symbols defined in other fragmentary or complete classes. Combining fragmentary classes via multiple inheritance supplies their missing parts. Python’s SocketServer module has both UDPServer and TCPServer classes, and threading and forking mixin classes. You can define derived classes like this: ThreadingTCPServer(ThreadingMixIn, TCPServer) ForkingUDPServer(ForkingMixIn, UDPServer)

Mixin Inheritance Mixin inheritance requires application-specific names for methods and possibly fields contributed by mixin component classes. Multiple implementation inheritance of complete base classes is also possible. Python walks a class’ inheritance graph in left-to- right order, from most-to-least derived, backing up before shared base classes, to search for symbols and to resolve conflicts. Print CLASSNAME.__mro__ shows the class ordering.

Deciphering multiple inheritance >>> class d11(d1): pass >>> class d12(d1) : pass >>> class d21(d2) : pass >>> class d22(d2) : pass >>> class multi(d11, d12, d21, d22): pass >>> multi.__mro__ (,,, <clas s '__main__.d1'>,,, <class '__main __.d2'>,, ) multi(d11, d12, d21, d22):

All method bindings are either object-dynamic or class-static. The Python bytecode interpreter looks for field and method symbols within class objects using the __mro__ class ordering graph. All lookup is dynamic. There is nothing like a C++ non-virtual member function that is bound statically by the methods require no object. They can be invoked using a class name or an object of that class as a prefix. Resolution of static methods also uses __mro__ for an object prefix.

Python metaclasses (end of PY Ch. 7) Derive a class from type or class and define or redefine the appropriate methods. This new type can create alternatives to “class” for creating user-defined types. pymeta.html pymeta.html sses.html?page=1 sses.html?page=1