Lecture 12 Implementation Issues with Polymorphism.

Slides:



Advertisements
Similar presentations
Quality of a Class Abstraction: Coupling & Cohesion Michael L. Collard, Ph.D. Department of Computer Science Kent State University.
Advertisements

Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
UHD::CS3320::CHAP61 INTRODUCTION TO OBJECTS Chapter 6.
Module: Definition ● A logical collection of related program entities ● Not necessarily a physical concept, e.g., file, function, class, package ● Often.
Chapter 10 Classes Continued
Inheritance and Polymorphism CS351 – Programming Paradigms.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
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.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Internet Software Development Classes and Inheritance Paul J Krause.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Concepts of Software Quality Yonglei Tao 1. Software Quality Attributes  Reliability  correctness, completeness, consistency, robustness  Testability.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
(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.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Session 6 Comments on Lab 3 & Implications of Inheritance.
Peyman Dodangeh Sharif University of Technology Fall 2014.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Design issues for Object-Oriented Languages
C11, Implications of Inheritance
Object-Oriented Programming Concepts
Advanced Programming in Java
Coupling and Cohesion 1.
Inheritance and Polymorphism
OOP What is problem? Solution? OOP
Polymorphism.
Chapter 3: Using Methods, Classes, and Objects
Types of Programming Languages
Inheritance, Polymorphism and the Object Memory Model
Object Oriented Analysis and Design
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Support for Object-Oriented Programming
Overview of C++ Polymorphism
C++ Programming CLASS This pointer Static Class Friend Class
Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
CECS 130 Final Exam Review Session
Corresponds with Chapter 5
Advanced Programming in Java
Presentation transcript:

Lecture 12 Implementation Issues with Polymorphism

Memory Allocation Consider the code Consider the code class Person{ public: virtual void f(); private: Name n; } class Professor: public Person{ public: virtual void f(); private: Classes c; } Person per; Professor prof;

Allocation The assignment per = prof in C++ (remember, this is an object assignment) slices the extra fields off. The assignment per = prof in C++ (remember, this is an object assignment) slices the extra fields off.  The field c is gone  For members use static binding:  per.f(); //accesses Person  With virtual methods with pointers or refs use the dynamic information, e.g. Person *pper; Professor *pprof; pper=&prof; pper->f(); //accesses Professor f() pprof=&prof; pprof->f(); //clearly professor

Other Strategies Allocate the maximal amount for the largest subclass. Allocate the maximal amount for the largest subclass.  No slicing  Need to know all subclasses  Nobody does this Allocate memory for pointers Allocate memory for pointers  If the data is kept on the heap: allocated upon request  E.g. Professor = new Professor();  Referred by pointer or reference

Heap Allocation (ctd) Data is anonymous Data is anonymous  Assignment statements and equality tests are pointer/reference oriented  Copy semantics require new objects, pointer/reference not  In C++ the == operator can be overloaded to any meaning  Smalltalk & Java use pointer/reference semantics, but allow the use of clones

Cloning Implement a copy method Implement a copy method  Create a new object  Copy the existing object’s contents into it public class structure{ public structure copy(){ structure x = new structure(); x.setValue(getValue()); return x; } structure copy = obj.copy(); //cloning

Forms of Polymorphism Variable: variables can hold values from a base class and subclasses. Variable: variables can hold values from a base class and subclasses.  The dynamic type must be a subclass of the static type of the base class  C++ requires use of pointers and references for polymorphism Pure: Methods can have polymorphic parameters. Pure: Methods can have polymorphic parameters.  In Java and C++ need double dispatch  accept(Visitor v){return v.visitXXX(this); accepts any legal Visitor

Other Types of Polymorphism Overloading: Allow several function bodies, e.g. overloading “+” operator in C++ Overloading: Allow several function bodies, e.g. overloading “+” operator in C++  The function signature determines the body  Can use static and dynamic signatures Overriding: Change the semantics of the parent in the child Overriding: Change the semantics of the parent in the child  Different children can override in different ways  Ensures common interfaces

Other Types of Polymorphism Deferred Methods: Force the children to have method bodies (generalization of overriding) Deferred Methods: Force the children to have method bodies (generalization of overriding) class Shape{... void virtual draw() = 0;... } class Circle: public Shape{... void draw{drawCircle();}... }

Coupling and Cohesion Measure dependency between classes by coupling Measure dependency between classes by coupling  Dependency limits code reuse  Could be intentional: E.g. MVC and Listener Modules should minimize external dependencies, e.g. GUI and business logic Modules should minimize external dependencies, e.g. GUI and business logic Modules should address common behavior, i.e. internal cohesion Modules should address common behavior, i.e. internal cohesion

Coupling Measure of dependency between modules Measure of dependency between modules Types in order of desirability Types in order of desirability  Data Coupling: Output from one module is input to another  Control Coupling: Passing control flags between modules so that one module controls sequencing of processing in another  Global Data Coupling: Two or more modules share common data structures  Internal Data Coupling: One module directly modifies local data of another

Data Coupling Output from one module is the input from another Output from one module is the input from another E.g. using parameter lists to ship objects between classes E.g. using parameter lists to ship objects between classes  Object X passes Object O to Object Y  Objects O and Y are coupled  Change interface in O may require change in Y

Example class Y{ public void funnyStuff(Object O){... O.doSomethingFunny(String t);... }

Control Coupling A sends a message to B A sends a message to B B returns control information to A B returns control information to A class tryPrint{ public: int printFile(File toPrint){ if (toPrint is corrupt) return CORRUPTFLAG; else } tryPrint when = new tryPrint(); int result = when.printFile(popQuiz); if (result == CORRUPTFLAG)...

Global Data Coupling Modules has access to common data areas Modules has access to common data areas  E.g. external in C  Variables in public interfaces whose structure is public  Even items in a public interface whose values remain constant and whose implementations are hidden Modules are very tightly coupled Modules are very tightly coupled Reusability is severely limited Reusability is severely limited Hard to tell who updated the global data Hard to tell who updated the global data What happens when data structure is changed? What happens when data structure is changed?

Global Data Coupling Cure: Make a separate module with public methods to access the global data Cure: Make a separate module with public methods to access the global data Very bad form of coupling, especially with pointers to global dating because of aliasing Very bad form of coupling, especially with pointers to global dating because of aliasing Hard to understand classes in isolation Hard to understand classes in isolation

Internal Data Coupling Modules modify each other’s internal data Modules modify each other’s internal data Also dependencies between items that make up an object Also dependencies between items that make up an object The worst form of coupling-always to be avoided! The worst form of coupling-always to be avoided!

Other Forms of Coupling Interface Coupling (a form of Object Coupling): Referring to specific items in another object’s public interface (very weak form of coupling) Interface Coupling (a form of Object Coupling): Referring to specific items in another object’s public interface (very weak form of coupling) Outside Internal Coupling from Underneath: coupling between parent and child with respect to state or operations Outside Internal Coupling from Underneath: coupling between parent and child with respect to state or operations  Unwanted inheritance  Makes subclass complex