Static and Dynamic Behavior Fall 2005 OOPD John Anthony.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

More on Classes Inheritance and Polymorphism
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.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Polymorphism.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
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.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
Implications of Substitution Fall 2005 OOPD John Anthony.
Introduction to Inheritance Fall 2005 OOPD John Anthony.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
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.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Inheritance in the Java programming language J. W. Rider.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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 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.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
BY:- TOPS Technologies
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,
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Abstract Classes and Interfaces
OOP What is problem? Solution? OOP
ATS Application Programming: Java Programming
Object Oriented Programming
Chapter 11 Class Inheritance
Chapter 11 Inheritance and Encapsulation and Polymorphism
Advanced Programming in Java
Presentation transcript:

Static and Dynamic Behavior Fall 2005 OOPD John Anthony

Topics Static vs. Dynamic Typing Static vs. Dynamic Typing Static Classes vs. Dynamic Classes Static Classes vs. Dynamic Classes Static vs. dynamic method Binding Static vs. dynamic method Binding Let’s forget about inheritance and polymorphism for a few moments….

Static vs. Dynamic Static almost always means fixed or bound at compile time, and cannot thereafter be changed. (consider OO without polymorphism) Dynamic almost always means not fixed or bound until run time, and therefore can change during the course of execution (consider every variable as a polymorphic variable)

Static vs. Dynamic Typing Statically Typed – variables have a declared type which is fixed at compile time. (consider Java) Dynamically Typed – types are associated with the values only. The variable is just a name. The variable can hold different types during the course of execution. (consider Smalltalk)

Benefits and Drawbacks Static and Dynamically typed languages have existed as long as there have been programming languages. 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. 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). 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. Both arguments have some validity, and hence both types of languages will continue to exist in the future.

Remember the polymorphic variable??? A variable (or reference) that can be attached to more than one type of object. A variable (or reference) that can be attached to more than one type of object. TeamMember member = new TeamMember(); InnovationLabArchitect architect = new InnovationLabArchitect(); InnovationLabArchitect architect = new InnovationLabArchitect (); polymorphic variable non polymorphic variable

Static Class vs. Dynamic Class In a statically typed language we say the class of the declaration is the static class. The static class is fixed at compile time and never changes. In a statically typed language we say the class of the declaration is the static class. The static class is fixed at compile time and never changes. The dynamic class of a variable is the class associated with the value it currently holds. The dynamic class can change during the course of execution. The dynamic class of a variable is the class associated with the value it currently holds. The dynamic class can change during the course of execution.

Remember… TeamMember InnovationLabArchitect member architect before after TeamMember member = new TeamMember(); InnovationLabArchitect architect = new InnovationLabArchitect(); InnovationLabArchitect architect = new InnovationLabArchitect (); member = architect; Static class Dynamic class

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….not the current dynamic receiver. class Animal { } class Dog extends Animal { void bark() { System.out.println("woof"); } // will generate error since mammals don't speak…although //the value Dog could respond to the message. Animal pet = new Dog; pet.bark();

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? 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. (instanceof in Java)  The task of assignment - can I then assign the value from the parent class to a variable declared as the subclass. (casting)

Static vs. Dynamic Method Binding Should the binding of a message be determined by the static class of a variable or the dynamic class of the value? Most OO languages use the static class to determine the legality of the message (compile time check). While the correct (method) implementation is determined by the dynamic class (run time).