CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.

Slides:



Advertisements
Similar presentations
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Advertisements

Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSSE501 Object-Oriented Development. Chapter 5: Messages, Instances and Initialization  This chapter examines OOP run-time features: Object Creation.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
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.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
A Survey of Object-Oriented Concept Oscar Nierstrasz.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
ISBN Chapter 12 Support for Object-Oriented Programming.
BY:- TOPS Technologies
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Types of Programming Languages
ATS Application Programming: Java Programming
Polymorphism Polymorphism
CS18000: Problem Solving and Object-Oriented Programming
Inheritance.
Workshop for Programming And Systems Management Teachers
Advanced Programming in Java
Presentation transcript:

CSSE501 Object-Oriented Development

Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic object-oriented programming languages Static versus Dynamic Typing Static and Dynamic Classes in Statically Typed Languages Static and Dynamic Method Binding in Statically Typed Languages

What do the terms Static and Dynamic Mean?  In Programming languages: Static almost always means fixed or bound at compile time, and cannot thereafter be changed. Dynamic almost always means not fixed or bound until run time, and therefore can change during the course of execution.

Static and Dynamic Typing  In a statically typed programming language (Java or Pascal), for example, variables have declared typed -- fixed at compile time.  In a dynamically typed programming language (Smalltalk or CLOS), a variable is just a name. Types are associated with values, not variables. A variable can hold different types during the course of execution. var x; // (1) x := 5; // (2) x := "hi"; // (3) var x := 5; // (1) var y := "hi"; // (2) var z := x + y; // (3)

Arguments For and Against  Static and Dynamically typed languages have existed as long as there have been programming languages. Arguments for and against: Static typing allows better error detection, more work at compile time and hence faster execution time. Dynamic typing allows greater flexibility, easier to write (for example, no declaration statements). Both arguments have some validity, and hence both types of languages will continue to exist in the future.

The Polymorphic Variable  The addition of object-oriented ideas in a statically typed languages adds a new twist. Recall the argument for substitution: an instance of a child class should be allowed to be assigned to a variable of the parent class: var pet : Mammal; fido : Dog; felice: Cat; begin pet := fido; // legal pet := felice; // legal fido := pet; // not legal!

Static Class and Dynamic Class  In a statically typed language we say the class of the declaration is the static class for the variable, while the class of the value it currently holds is the dynamic class.  Most statically typed OO languages constrain the dynamic class to be a child class of the static class. var pet : Mammal; fido : Dog; begin pet := fido; // static class is Mammal, dynamic class is Dog end;

Importance of Static Class  In a statically typed object-oriented language, the legality of a message is determined at compile time, based on the static class. A message can produce a compile error, even if no run-time error could possibly arise: class Mammal { } class Dog extends Mammal { void speak() { System.out.println("woof"); } } Mammal pet = new Dog(); pet.speak(); // will generate error, Mammals don't speak

Reverse Polymorphism  Polymorphism says we can assign a value from a child class to an instance of the parent class, but can this assignment then be reversed? Under what conditions? var pet : Mammal; fido : Dog; felice : Cat; begin pet := fido; // legal fido := pet; // is this legal? end; This is known as the problem of reverse polymorphism.

Two aspects of reverse polymorphism  There are two specific problems associated with the question of reverse polymorphism. The problem of identity - can I tell if a value declared as an instance of a parent class actually holds a value from a subclass. The task of assignment - can I then assign the value from the parent class to a variable declared as the subclass.  In some languages mechanisms are provided to address these two problems together, while in other languages they are separated.

The Container Problem  The task of reverse polymorphism is often encountered in connection with a collection of values - we have a list of items from the parent class (say a list of Mammals), and when we extract a value we need to know if it is a more specific type. Generally occurs in languages with a single inheritance tree, where the only type we may have associated with a value is the class “Object''. Solving this problem generally requires values to have “self knowledge” of their own type. In some languages they do, in some languages values do not.  Java, unfortunately, uses to term Container to mean a type of graphical component, and not a collection class, as is common in most other languages

Static and Dynamic Method Binding  Should the binding for information be associated with the static class of a variable or the dynamic class? Alice holds a small Mammal - asks Bill “does this animal give birth to live young''. Static answer - All mammals give birth to live young - therefore yes. What if the Mammal is a platypus? Dynamic answer - Platypus lay eggs, therefore no.  Even statically typed OOP languages can use dynamic binding. But may use static type to determine legality of operation.

public class Dog extends Mammal { void speak() { System.out.println("Woof!"); } public class Mammal{ void speak() { System.out.println("Ohhh!"); } public class Test { public static void main(String[] args) { Mammal pet = new Mammal(); Dog fido = new Dog(); pet = fido; pet.speak(); }

Documenting Method Binding  In many languages dynamic binding is the default. If a child class overrides a method in the parent, using the same type signature, then the selected method will be determined by the dynamic type.  In other languages (C++, Delphi, C#) the programmer must indicate which methods are dynamically bound and which are statically type. In C#, for example, this is done using the virtual keyword.

Merits of Static versus Dynamic Method Binding  Arguments concerning static versus dynamic binding mirror those concerning static versus dynamic typing. Efficiency - static binding uses least CPU cycles, dynamic binding requires more time. Error detection - static binding permits errors to be caught at compile time rather than run-time. Flexibility - dynamic binding permits greater flexibility, static binding creates rigidity and inhibits reuse.

Chapter Summary  A statically typed language associated types with variables, a dynamically typed language associates types with values.  Static typing gives better error detection, better run- time efficiency, less flexibility.  In a statically typed OO language, an object variable can still hold values from a child class.  The static class is the class of the declaration, the dynamic class is the class of the value currently held.  The legality of a message is checked using the static class.  A message can be bound to a method using either the static or dynamic class. Most languages use the dynamic class. Some languages allow the programmer to choose which method is used.