Object-Oriented Programming “The Rest of the Story”, CS 4450 – Chapter 16.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Advertisements

Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
CS 211 Inheritance AAA.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Object-Oriented PHP (1)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Software Testing and Quality Assurance
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.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Lecture 9 Concepts of Programming Languages
Chapter 10 Classes Continued
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
Neal Stublen Key Concepts  Classes  Objects  Inheritance  Polymorphism  Encapsulation.
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.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
An Object-Oriented Approach to Programming Logic and Design
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
OO as a language for acm l OO phrase l Mental model of key concepts.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter FifteenModern Programming Languages1 A Second Look At Java.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
Chapter SixteenModern Programming Languages1 Object Orientation.
ISBN Object-Oriented Programming Chapter Chapter
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
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.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Overview of C++ Polymorphism
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
 Inheritance  Protected Members  Non-public Inheritance  Virtual Function Implementation  Virtual Destructors  Abstract Base Classes and Interfaces.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
ISBN Chapter 12 Support for Object-Oriented Programming.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
The Movement To Objects
Object-oriented Programming in Java
Inheritance and Polymorphism
Object-Orientated Programming
Types of Programming Languages
Lecture 9 Concepts of Programming Languages
Object Oriented Programming
Chapter 9 Carrano Chapter 10 Small Java
Overview of C++ Polymorphism
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Object-Oriented Programming “The Rest of the Story”, CS 4450 – Chapter 16

Topics What is OOP? Objects Without Classes ◦ “Dynamic inheritance” in Javascript, Python Contract Programming Multiple Dispatch

What is Object Oriented Programming? Encapsulation Inheritance Subtype Polymorphism ◦ or “equivalent”

Encapsulation Separating interface from implementation Different mechanisms ◦ Classes & Interfaces ◦ Nested functions and closures

The Role of Classes Most OO languages have some kind of class construct Classes serve a variety of purposes, depending on the language: ◦ Group fields and methods together ◦ Are instantiable: the running program can create as many objects of a class as it needs ◦ Serve as the unit of inheritance: derived class inherits from base class or classes Chapter SixteenModern Programming Languages5

Classes More purposes: ◦ Serve as a type: objects (or references to them) can have a class or superclass name as their static type ◦ House static fields and methods: one per class, not one per instance  Serve as a labeled namespace (scope); control the visibility of contents outside the class definition Chapter SixteenModern Programming Languages6

Without Classes Imagine an OO language with no classes With classes, you create objects by instantiating a class Without classes, you could create an object from scratch by listing all its methods and fields on the spot Or, you could clone an existing prototype object and then modify parts of it Chapter SixteenModern Programming Languages7

Chapter SixteenModern Programming Languages8 x = new Stack(); x = { private Node top = null; public boolean hasMore() { return (top!=null); } public String remove() { Node n = top; top = n.getLink(); return n.getData(); } … } y = x.clone(); y.top = null; With classes: instantiation Without classes: raw object creation Without classes: prototype cloning

Prototypes A prototype is an object that is copied to make similar objects When making copies, a program can modify the values of fields, and can add or remove fields and methods (“dynamic OOP”) Prototype-based languages (Self, Javascript to some degree) use this concept instead of classes Chapter SixteenModern Programming Languages9

Without Classes Instantiation is only one use of classes Other things prototype-based languages must do without: ◦ Classes as types: most prototype-based languages are dynamically typed ◦ Inheritance: “static inheritance” requires classes  Prototype languages use “dynamic inheritance”  aka “delegation” Chapter SixteenModern Programming Languages10

Inheritance Inheritance is a way to defer attribute or method selection to another type ◦ a “superclass” This is static inheritance ◦ since relationships among types are defined in source code There is another way…

Delegation “ Runtime Inheritance” Object (not type)-based inheritance Instead of a superclass, an object has a delegate ◦ aka “prototype” (just another object) ◦ Delegates are set at runtime Any unresolved name lookup defers to the delegate object ◦ and so on up the delegate chain… ◦ A similar effect to subtype polymorphism, but without types! ◦ See prototype.ds (Javascript), prototype.py

About Dynamic Typing An object may or may not be able to respond to a particular request (“message”) — no compile-time check Total freedom: program can try using any method for any object Polymorphism is a “given” in dynamically-typed languages ◦ Polymorphism is only a conscious concern in statically- typed languages ◦ “Polymorphism is a way gaining some of the freedom of dynamic type checking without giving up the benefits of static checking.” (p. 128) Chapter SixteenModern Programming Languages13

Contract Programming A Perspective for Polymorphic Interfaces Methods are contracts with the user Users must meet pre-conditions of a method ◦ What the method expects from the client ◦ Index in a certain range, for example Method guarantees certain post-conditions ◦ but only if the pre-conditions were met 14CS Defensive Programming

Parties in Contracts Clients and Suppliers Clients must satisfy pre-conditions ◦ They may benefit from post-conditions ◦ But don’t have to Suppliers must satisfy post-conditions ◦ They may assume the pre-conditions ◦ But don’t have to This affects inheritance… 15CS Defensive Programming

Liskov Substitutability Principle All derived objects must be able to substitute for a base class object ◦ In contexts where public methods of the base class are used Derived classes must not change the “rules of the game” CS Defensive Programming16

Contracts and Inheritance Contracts are set by the base class ◦ Derived classes must obey the base contract ◦ Otherwise substitutability is compromised Clients program to the contract ◦ By using and understanding the base class interface and its conditions ◦ And by using base/interface pointers  and letting polymorphism do its work invisibly Example: contravar.cpp, contravar.d 17CS Defensive Programming

Contracts and Inheritance Summary “The problem for instances of B is how to be perfectly substitutable for instances of A. The only way to guarantee type safety and substitutability is to be equally or more liberal than A on inputs, and to be equally or more strict than A on outputs.” – Wikipedia “Require no more; Promise no less” 18CS Defensive Programming

Dynamic Dispatch The Mechanics of Subtype Polymorphism In Java/C++/C#, the static type of a reference may be a superclass or interface of the actual runtime class At runtime, the language system must find the right method for the actual class ◦ the dynamic type That’s dynamic dispatch: the hidden, implicit branch-on-class to implement method calls Optional in C++ (must use virtual keyword) Chapter SixteenModern Programming Languages19

Multiple Dispatch All of the examples we’ve seen of subtype polymorphism (or delegation) key off a single type (or object) to find the right method ◦ p.f(x,y) considers only the dynamic type of p  x and y are not considered at runtime Some languages (CLOS, Groovy, Perl 6) use all types involved to resolve the method lookup ◦ “runtime overloading” ◦ No one class/object “owns” the method

Double Dispatch Example VWX A ✔✔ B ✔ C ✔ Definitions for f(): What is the “most derived” function for the calls: c.f(x), c.f(w)?

An Ordering Perspective List parameter combinations in “odometer order” (most general to most specific): A,V * A,W A,X * B,V B,W B,X * C,V * C,W C,X Reverse, keeping only existing methods: C,V B,X A,X A,V To dispatch, test parameters in the order above for is-a, using RTTI See doubledisp.cpp

Switching the Parameter Order V A * V B V C * W A W B W C X A * X B * X C X B * X A * V C * V A * See doubledisp-B.cpp

Multiple Dispatch Any number of hierarchies/parameters may be used Again, applicable methods are considered in “most derived” order Supported natively by CLOS ◦ Common Lisp Object System ◦ “Generic Methods”

Multiple Dispatch Example See multimeth.lsp, multimeth.cpp, multimeth.d ZVWX A ✔✔ B ✔ C ✔ YVWX A ✔ B ✔ C ✔