Lecture 15: Generalization, Polymorphism and States. Copyright W. Howden 1

Similar documents
Lecture 17: Patterns Potpourri. Copyright W. Howden 1

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Chapter 5 Object-Oriented Programming

CS-202 Introduction to Object Oriented Programming

Object-Oriented Concepts and Design Principles

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Introduction to Programming Using Java (98-388)

Practice for Chapter 11

Inheritance and object compatibility

QUESTIONS FOR AVERAGE BLOOMERS

Lecture 14: Callbacks, Singletons and Wrappers. Copyright W. Howden 1

C++ Important Questions with Answers

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Java Object Oriented Design. CSC207 Fall 2014

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Inheritance. Transitivity

JAVA MOCK TEST JAVA MOCK TEST II

Review sheet for Final Exam (List of objectives for this course)

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Lecture Notes on Programming Languages

Java: introduction to object-oriented features

Software Engineering

PROGRAMMING LANGUAGE 2

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Data Structures (list, dictionary, tuples, sets, strings)

ENCAPSULATION AND POLYMORPHISM

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

OBJECT ORIENTED PROGRAMMING USING C++

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Inheritance and Polymorphism

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Example: Count of Points

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

CMSC 132: Object-Oriented Programming II

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

More Relationships Between Classes

CLASS DESIGN. Objectives MODULE 4

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

CIS 110: Introduction to computer programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Inheritance -- Introduction

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

More on Inheritance. Interfaces & Abstract Classes

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Programming II (CS300)

Inheritance and Polymorphism

ITI Introduction to Computing II

Super-Classes and sub-classes

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Overview. Elements of Programming Languages. Objects. Self-Reference

1 Shyam sir JAVA Notes

Inheritance (an intuitive description)

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Object Oriented Programming: Based on slides from Skrien Chapter 2

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

ob-ject: to feel distaste for something Webster's Dictionary

Inheritance and Polymorphism

Making New instances of Classes

[ L5P1] Object-Oriented Programming: Advanced Concepts

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

CS558 Programming Languages Winter 2013 Lecture 8

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

ITI Introduction to Computing II

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Mobile MOUSe JAVA2 FOR PROGRAMMERS ONLINE COURSE OUTLINE

1.00 Lecture 13. Inheritance

First IS-A Relationship: Inheritance

Lecture 16: Factories and Frameworks. Copyright W.E. Howden 1

Binghamton University. CS-140 Fall Dynamic Types

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

COMP 110/L Lecture 19. Kyle Dewey

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

COMP 250 Fall inheritance Nov. 17, 2017

Object-Oriented Programming

Compaq Interview Questions And Answers

Rules and syntax for inheritance. The boring stuff

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

ECE 122. Engineering Problem Solving with Java

Transcription:

Lecture 15: Generalization, Polymorphism and States Copyright W. Howden 1

Generalization Creation of super and subtype entities May be program and design classes, concepts, use cases, etc. For programs, generalization corresponds to class inheritance Subtypes can modify, add to, and access properties of supertypes Copyright W. Howden 2

Kinds of Generalization specialization specification construction extension limitation combination Copyright W. Howden 3

Specialization Re-defines defined parent properties and methods. Also called refinement. E.g. User-defined class constructor always calls the supertype constructor, either explicitly or automatically E.g. Suppose we have a stack class where a message is returned when an item is pushed on the stack success if pushed, or error if stack is full change this so stack-is-full warning is returned when stack becomes full, i.e. last item is pushed Copyright W. Howden 4

Specification Used to specify behavior, but not to define it Subtype defines the behavior E.g. ActionListener interface in Java GUI. Specifies behavior (methods) for listener classes which implement ActionListener Copyright W. Howden 5

Construction For the purposes of re-use of methods and data structures Defining new methods and data structures in terms of inherited ones Child may have no logical relationship with parent(s) E.g. Use of Vector to define a stack class Copyright W. Howden 6

Extension Adding new properties and methods No modification of parent properties Similar to construction but has the is a property which construction may not have E.g. adding button declarations to the Dialog class in a new extends class Copyright W. Howden 7

Limitation Override some methods with blank/null methods Limits access to certain behaviors May be used with construction to block methods that have no meaning in the subtype E.g. WindowAdpator class which is a limitation of WindowListener interface Copyright W. Howden 8

Combination Uses multiple inheritance for two or more applications of generalization DS Java example MessageDialog extends (i) Dialog implements (ii) ActionListener (i) extension: add buttons, panels (ii) specification: define actionperformed method() Copyright W. Howden 9

Generalization Correctness Rules (From earlier lecture) is-a An instance of a subtype is also an instance of the supertype. e.g. an administrator is a DS user Substitutability Suppose B is a subtype of A. It should be possible to substitute an instance of B any place that requires something of type A. 100% rule All of the supertype s definition should also apply to the subtype (i.e. its attributes, associations) Copyright W. Howden 10

Correct Generalization Uses Specialization is-a Specification is-a, substitutability, 100% Extension is-a, substitutability, 100% Combination (sometimes) Copyright W. Howden 11

Polymorphism Literally, an entity that can take different forms Programs A variable that can have values of different types A method that can take arguments of different types Copyright W. Howden 12

Polymorphic Variables Dynamically typed languages (e.g. Smalltalk) any variable may hold items of any type, provided expected operations are defined Statically typed languages (e.g. Java) variable takes on values of differently typed subtypes of variable s class Copyright W. Howden 13

Polymorphic Methods Method can have arguments of different types Any type Subtypes of declared supertypes Copyright W. Howden 14

Polymorphic Design Pattern and Generalization Based on the specification variation of the uses of generalization Subtypes give different method definitions for the more general method specification Eg. In DS can be in member or admin state, for which there are different behaviors. Design a state class with abstract methods whose substate behavior is defined in the subtype classes Copyright W. Howden 15

Why Polymorphic Design? Subtypes encapsulate behavior alternatives for specification and documentation Promotes cohesion and reuse Follows the basic expert pattern creates an expert which will be given the responsibility for the behavior for which it is the expert Copyright W. Howden 16

Functional Versus Polymorphic Alternative Behavior Functional: conditional statement calls the appropriate function/subroutine or executes the substatement that exhibits the appropriate behavior Polymorphic: send message to an object of a single type (supertype) which will result in the appropriate behavior. Object will be an instance of the associated supertype Copyright W. Howden 17

Overloading Early Uses Use of the same name for different operations/methods Idea inherited from mathematics e.g. overloading of * operation x * y for integers x and y x * y for complex numbers x and y x * y for p n and n q matrices x and y Copyright W. Howden 18

Overloading and Method Names Different methods have the same name methods in the same class methods in different classes related by subtyping methods in different classes not related by subtyping logically and non-logically related Need a way to disambiguate an overloaded method reference Copyright W. Howden 19

Overload Disambiguation Class disambiguation method reference is disambiguated by the class of the object whose method is being invoked subtypes of supertype unrelated classes e.g. isempty() is understood by Vector and Hashtable classes Parameter disambiguation methods in the same class with the same name must have different parameter lists e.g. multiple constructors for a class Copyright W. Howden 20

Overloading and Programming Languages Requires that variables be typed, so that compiler can find and incorporate the correct code Copyright W. Howden 21

Polymorphism and Overloading Polymorphism => overloading Suppose the superclass has a method m() that is defined in the subtypes m() overloaded in the subtypes Uses class disambiguation Copyright W. Howden 22

Non-Polymorphic Overloading Methods in unrelated classes may have the same name E.g. Many classes have an isvalueof() operator that will behave slightly differently for each class The classes are not subtypes of a more general type Copyright W. Howden 23

Coercion and Deferment Deferred method definition abstract method in superclass another kind of downward overloading Coercion types of parameters are coerced to a standard type, not really overloading e.g. x*y is always floatingpoint for both integer and real numbers Copyright W. Howden 24

Overloading Examples DS: execute() methods for logon, DateRequest classes Multiple constructors for the same class Copyright W. Howden 25

States and Alternative Behavior Context: object can be in several states, and it behaves differently depending on which state it is in E.g. DS states reflect type of current user (member, administrator, unauthorized) response to request, such as addmember() will be different in each state Copyright W. Howden 26

State Classes Object x from class A can be in different states Create a state class S S has abstract methods that mirror the state sensitive methods in A define subtypes of S for each of the states of A, that have definitions that describe the different state sensitive behavior Copyright W. Howden 27

State Pattern Suppose A: x is a state sensitive object Define a corresponding state class S for A Include a state variable S: s in A whose value will be an instance of the appropriate subtype of S When a method x.m() in x is invoked, it executes s.m() Copyright W. Howden 28

DS - State Class for DL Domain Logic subsystem has an interface class called DomainLogic. DomainLogic contains an inner class that defines a state class DomainLogicState DomainLogicState has subtypes for each type of user DomainLogic has a class variable called currentstate that is set to an instance of a subtype of DomainLogicState Copyright W. Howden 29

DS Setting the State Variable GUI calls logon(name) in the DomainLogic instance dl dl.logon(name) creates a LogOn instance which is then executed logon.execute() determines user type based on name (administrator has a special name) and whether is a member with this name in member data base execute() sets the usertype and the currentstate variables for the DomainLogic instance Copyright W. Howden 30

public int execute() { if (username.equals(administrator)) { usertype = Constants.ADMIN; currentstate = new DomainLogicAdminState(); return(constants.admin); } b = database.ismember(username); if (database.ismember(username)) { usertype = Constants.MEMBER; currentstate = new DomainLogicMemberState(); return Constants.MEMBER; } else { usertype = Constants.UNAUTH; currentstate = new DomainLogicUnauthState(); return (Constants.UNAUTH); } } Copyright W. Howden 31

DS - State Dependent Behavior Suppose dl gets a message (i.e. one of its methods is performed), such as deletemember(name) dl calls the corresponding state method Copyright W. Howden 32

Abstract class and methods: DomainLogic -state +getadate() «metaclass» DomainLogicState +getadate() MemberState +getadate() AdminState +getadate() UnauthUserState +getadate() getadate(props) getadate(props, dl) Object1 dl:domainlogic :DomainLogicState Method of current substate subtype object is invoked: Copyright W. Howden 33

DS Code for DomainLogic Method public boolean deletemember(string name) { return currentstate.deletemember(this, name); } Note: a parameter has been added to the method for the subtype in order to pass along the identity of the object that can be in different states Copyright W. Howden 34

DS Code for MemberState Subtype Members are not allowed to delete members public boolean deletemember(domainlogic dl, String name) { return false; } Copyright W. Howden 35

DS Code for AdminState Subtype Assume DataBase is built with a delete capability so DL just passes it through public boolean deletemember(domainlogic dl, String name) { return dl.database.deletemember (name); } Copyright W. Howden 36

Alternative Object Behaviors Mechanisms: Polymorphic pattern, states, decorator wrapper Issues: i) how are the alternatives defined? ii) set of peer alternatives, or alternative to a given existing behavior? iii) when is an alternative behavior established for an object? iv) does the new behavior require the creation of a new object? Copyright W. Howden 37

Polymorphic Alternative subtypes for object of interest Related set of alternatives Set at object creation time New object with selected behavior State Based Alternative subtypes for associated state variable Related set of alternatives Changed during run time Same object with new behavior Decorator New class definition using object of interest in the definition Single new kind Changed during run time New object with altered behavior Copyright W. Howden 38