Binghamton University. CS-140 Fall Dynamic Types

Similar documents
What is Inheritance?

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

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

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

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Inheritance -- Introduction

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Computer Science II (20082) Week 1: Review and Inheritance

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Inheritance and Polymorphism

Practice for Chapter 11

Rules and syntax for inheritance. The boring stuff

Classes and Inheritance Extending Classes, Chapter 5.2

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Inheritance and Polymorphism

CS260 Intro to Java & Android 03.Java Language Basics

Inheritance (continued) Inheritance

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

Lecture 4: Extending Classes. Concept

CS-202 Introduction to Object Oriented Programming

Programming in C# Inheritance and Polymorphism

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

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

Java: introduction to object-oriented features

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

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

Computer Science II (20073) Week 1: Review and Inheritance

More Relationships Between Classes

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Exercise: Singleton 1

First IS-A Relationship: Inheritance

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

More On inheritance. What you can do in subclass regarding methods:

Binghamton University. CS-140 Fall Subtyping!

Introductory Programming Inheritance, sections

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

JAVA Programming Language Homework I - OO concept

Programming Languages and Techniques (CIS120)

CIS133J. Working with Numbers in Java

COP 3330 Final Exam Review

25. Generic Programming

Lecture Set 4: More About Methods and More About Operators

Object Oriented Programming. Java-Lecture 11 Polymorphism

C++ Important Questions with Answers

Example: Count of Points

CS Programming I: Inheritance

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Class, Variable, Constructor, Object, Method Questions

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Lecture Set 4: More About Methods and More About Operators

Polymorphism. Agenda

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Comp 249 Programming Methodology Chapter 8 - Polymorphism

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

1 Shyam sir JAVA Notes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Objects and Iterators

CSE 431S Type Checking. Washington University Spring 2013

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Inheritance. A key OOP concept

C++ Inheritance and Encapsulation

Operators and Expressions

Handout 9 OO Inheritance.

BBM 102 Introduction to Programming II

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Object-Oriented Programming More Inheritance

Inheritance and Polymorphism in Java

An Introduction to Subtyping

Inheritance & Polymorphism

Inheritance and Polymorphism

PROGRAMMING LANGUAGE 2

CH. 2 OBJECT-ORIENTED PROGRAMMING

CLASS DESIGN. Objectives MODULE 4

Inheritance and Polymorphism

Object Orientated Programming Details COMP360

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Polymorphism. Polymorphism is an object-oriented concept that allows us to create versatile software designs

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Object-Oriented ProgrammingInheritance & Polymorphism

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Software Engineering. Prof. Agostino Poggi

Overview of Java s Support for Polymorphism

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Computer Science 210: Data Structures

Programming overview

Accelerating Information Technology Innovation

Java Object Oriented Design. CSC207 Fall 2014

Review for the. Final Exam. Final Exam. December 8, Final Format. Final Format. Ways to get an A on the Final. A Crash Course in Java

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Inheritance (an intuitive description)

Method Resolution Approaches. Dynamic Dispatch

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

Transcription:

Dynamic Types 1

Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; A subtype may be assigned where the supertype is expected 2

Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; Static Type: bref is declared as a reference to a Bird object Dynamic Type: bref is a reference to a Duck object! 3

Static Type Evaluated at compile time Almost always specified by the programmer Used by the compiler to issue warning and error messages 4

Implicit Up-cast Cast : Treat a Duck object as if it were an object of type Bird bref = (Bird) quack; Up-cast : Cast of a sub-type to a super-type (widening) Down-cast: Cast of a super-type to a sub-type (narrowing) Implicit Up-cast : Compiler performs the up-cast without direction from the programmer bref = quack; No compiler messages! Note Compiler requires EXPLICIT down-cast 5

Dynamic Type Evaluated at run-time Java keeps track of the ACTUAL type of a reference, even when the reference has been implicitly up-cast to a super-type. Most often, the type specified after new Duck quack = new Duck(); Or, result of method return value Dynamic type is used to select the VMT Enables polymorphism! Dynamic type of quack Dynamic type checked at run time when explicit down-cast occurs 6

Down-casting Requires sophisticated programmer to get it right Programmer tells compiler: Even though you think this is the super-type, I know that it s really the sub-type. If the programmer is wrong, run-time errors occur! Use instanceof to ensure you ve got it right if (bref instanceof Duck) { Duck bref2 = (Duck) bref; See also: Univesity of Texas Java Class 7

Casting and Operator Precedence Cast occurs before normal operators (double) sum / array.length; But. occurs before cast (double) array.length Extra parenthesis required to override. (Alpha)var1.mk(); // cast result of var1.mk() to Alpha ((Alpha)var1).mk(); // cast var1 to Alpha // Invokes Alpha class mk method 8

Static AND Dynamic Types You must pay attention to both static and dynamic types Static types are used by the compiler to determine whether instructions are legal Dynamic types are used for method dispatch 9

Private Methods A private method is not visible from outside the class If a private method is invoked from inside the class it will ALWAYS run the class method, even if the receiver is a sub-type that overrides that method! 10

Example Private Method public class PARENT { private void m1() { System.out.println( parent ); } public static void main(string[] args) { Parent p = new Child(); p.m1(); } } public class Child extends Parent { public void m1() { System.out.println( child ); } } Prints : parent @Override causes compiler error 11

Default Method Visibility If you do not specify private, public, or protected, you get package private visibility package private methods behave like public methods within the package package private methods behave like private methods if invoked from outside the package i.e. invisible 12

Protected Methods Behave like public methods if invoked within the package Visible within a descendant class, even if outside the package Invisible outside package from non-descendant classes! 13

Method Visibility Overridable Invocable Private No Within class Package Private Within package Within package Protected Within package or descendant class Within package or descendant class Public Yes Everywhere 14

Dynamic Dispatching When a method is invoked, uses the VMT associated with the dynamic type to find out what method to invoke Enables polymorphism Dynamic dispatching does NOT occur for private methods! In this case, private methods are only visible within the class Dynamic dispatching does NOT occur for static methods Static methods resolved from <classname>.class 15