Type Hierarchy. Lecture 6: OOP, autumn 2003

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

Chapter 5 Object-Oriented Programming

25. Generic Programming

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

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

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

Object Oriented Issues in VDM++

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

Java Object Oriented Design. CSC207 Fall 2014

Programming II (CS300)

6.170 Recitation #5: Subtypes and Inheritance

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

Practice for Chapter 11

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

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

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

Exercise: Singleton 1

Method Resolution Approaches. Dynamic Dispatch

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

More Relationships Between Classes

VIRTUAL FUNCTIONS Chapter 10

Inheritance, Polymorphism, and Interfaces

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Java 8 Programming for OO Experienced Developers

C++ Important Questions with Answers

Object Oriented Programming: Based on slides from Skrien Chapter 2

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS-202 Introduction to Object Oriented Programming

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

Introduction to Object-Oriented Programming

What is Inheritance?

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

PROGRAMMING LANGUAGE 2

Featherweight Java (FJ)

Chapter 10 Classes Continued. Fundamentals of Java

Lecture 4: Extending Classes. Concept

Computer Science 225 Advanced Programming Siena College Spring Topic Notes: Inheritance

First IS-A Relationship: Inheritance

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

Inheritance (continued) Inheritance

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Lecture Notes on Programming Languages

Programming II (CS300)

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Example: Count of Points

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

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

Inheritance and object compatibility

Class, Variable, Constructor, Object, Method Questions

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Introduction to Object-Oriented Programming

What are the characteristics of Object Oriented programming language?

Implements vs. Extends When Defining a Class

Example: Count of Points

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

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Inheritance and Polymorphism

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

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

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

CS111: PROGRAMMING LANGUAGE II

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Objects and Iterators

ITI Introduction to Computing II

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Java: introduction to object-oriented features

C++ Inheritance and Encapsulation

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

CS111: PROGRAMMING LANGUAGE II

15CS45 : OBJECT ORIENTED CONCEPTS

Inheritance and Polymorphism

CSE 431S Type Checking. Washington University Spring 2013

[ L5P1] Object-Oriented Programming: Advanced Concepts

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Concepts of Programming Languages

Subtypes and Subclasses

301AA - Advanced Programming [AP-2017]

Data Abstraction. Hwansoo Han

Object typing and subtypes

Announcements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.

ITI Introduction to Computing II

CSE 303: Concepts and Tools for Software Development

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Object Orientated Analysis and Design. Benjamin Kenwright

Inheritance and Testing Spring 2018 Exam Prep 4: February 11, 2019

Inheritance. Transitivity

CSE 307: Principles of Programming Languages

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

Week 5-1: ADT Design

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

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

Transcription:

Type Hierarchy Lecture 6: OOP, autumn 2003

The idea Many types have common behavior => type families share common behavior organized into a hierarchy Most common on the top - supertypes Most specific at the bottom - subtypes Many levels Typically depicted as upside-down tree subtype Substitutability => abstraction by specification Usage: Provide different implementations Extend class behavior supertype subtype

Inheritance in Java Inheritance mechanism Classes extend superclasses and implement interfaces Concrete classes - full implementation Abstract classes - partial abstract implementation, no objects Interfaces - only specification final methods - prevent inheritance abstract methods - no implementation Reimplementation <=> overriding Methods and attributes are inherited Visibility - private, public, protected The super( ) constructor and super keyword Only methods of the most specific class are executed

UML notation Person Teacher Student is-a Assistant Lecturer

Casting Upcasting/downcasting Widening for primitive types int x = 5; double y = x; Upcasting is implicit Student s = new Student(); Person p = s; Downcasting is explicit Student s = (Student) PersonSet.get(i); In java downcasting always checked at runtime if incorrect => throw ClassCastException

Overloading and overriding Overloading - abstract similar behavior in same class or subclass Reuse same name for similar behavior Needed for constructors Methods differ in parameters In Java no overloading on result type Overriding - replace functionality in subclasses Provide different implementations Methods have same signatures Special case of overloading Exist in most OO languages Main problem: How to determine the method to run? Compile-time: overloading resolution Run-time: dynamic binding

Overloading and overriding: example class X {} class Y extends X {} class A { public void f(x x) {} public void g(y y) {} } class B extends A { public void f(x x) {} public void g(y y) {} } class C extends B { public void f(y y) {} public void g(x x) {} } X Y A B C f(x) g(y) f(x) g(y) f(y) g(x) // varname = <apparent^actual> X xx = new X(); Y yy = new Y(); X xy = new Y(); C cc = new C(); B bb = new B(); B bc = new C(); cc.f(yy); // f in C cc.f(xx); // f in B cc.f(xy); // f in B bc.f(yy); // f in B bc.f(xx); // f in B bc.f(xy); // f in B cc.g(xx); // g in C cc.g(xy); // g in C bb.g(yy); // g in B bc.g(yy); // g in B

Overloaded method resolution in Java (1) Java uses dynamic binding, except final methods private methods Java tries to optimise dynamic binding with some pre-processing at compile time use apparent receiver and argument types to try to select a unique method aim: determine a unique method template to be used in the run-time dynamic binding search A method template: m: [ R, A, B, ] method receiver class parameter classes

Overloaded method resolution in Java (2) At compile-time, assemble a set of method templates whose receiver and parameters are type compatible with the call use apparent types of receiver and arguments start at receiver class and move upwards in hierarchy gather all templates that match, possibly widening receiver type or argument types Reduce this set to a unique most-specific template Most specific template => least type casts if any template in set has arguments or receiver that can be assigned to any other template in set, discard it repeat until one element left, or no more discards possible If unique most-specific template found, use it for run-time look-up If can t reduce to a unique match, report compile error

Dynamic binding (dispatching) Objects at run-time can be of subtypes Code can t execute methods directly Need to find actual implementation at run-time 1. At run-time, use actual type of receiver object to begin search for a method matching the template 2. Move upwards in hierarchy until find the first method matching template argument/parameter types must match exactly receiver run-time type must be subclass or same as template receiver type Use dispatch vectors

The substitution principle Enables reasoning using only supertype specification (abstraction again!) Any subtype object can be used in place of a supertype object without affecting using code Method/class design requirements: Signature rule - inherit all methods, compatible signature Methods rule -similar behavior Properties rule - preserve all superclass properties (e.g. invariants)

Substitution principle: the signature rule Guarantees type correctness for calls: correct for supertype => correct for subtype Enforced by Java compiler Subtypes have Same methods as in supertype Identical signatures Eventually less exceptions In principle can be relaxed - true suptyping Parameters of subtype methods - supertypes Results of subtype methods - subtypes

Substitution principle: the methods rule subtype's specification for any method must be at least as strong as its supertype's specification subtype method may weaken the precondition (require less) Less requires clauses Each clause is not stronger strengthen the postcondition (guarantee more) More clauses in postcondition Each clause can be stronger pre(super) => pre(sub) pre(super) & post(sub) => post(super) can not be checked by compiler

Substitution principle: the properties rule subtype must preserve all properties that can be proved about the supertype invariants guaranteed in supertype must be guaranteed by subtype subtypes may have stronger invariants - e.g sort order if only supertype methods/attributes considered, then any operation on subtype should result in same state as if supertype was modified