Inheritance and object compatibility

Similar documents
Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Chapter 5 Object-Oriented Programming

Practice for Chapter 11

CS-202 Introduction to Object Oriented Programming

CS111: PROGRAMMING LANGUAGE II

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Conformance. Object-Oriented Programming Spring 2015

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

Inheritance and Polymorphism

25. Generic Programming

Polymorphism and Inheritance

Java Object Oriented Design. CSC207 Fall 2014

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance -- Introduction

Object-Oriented Concepts and Design Principles

301AA - Advanced Programming [AP-2017]

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

VIRTUAL FUNCTIONS Chapter 10

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int)

Software Engineering

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

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

Inheritance, Polymorphism, and Interfaces

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

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

[ L5P1] Object-Oriented Programming: Advanced Concepts

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

CS 520 Theory and Practice of Software Engineering Fall 2018

Object Oriented Programming: Based on slides from Skrien Chapter 2

Object-Oriented Concept

Type Analysis. Type Checking vs. Type Inference

Inheritance. Transitivity

More on Objects in JAVA TM

Polymorphism and Interfaces. CGS 3416 Spring 2018

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Introduction to Object-Oriented Programming

Strict Inheritance. Object-Oriented Programming Spring 2008

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

First IS-A Relationship: Inheritance

A Short Summary of Javali

Programming in C# Inheritance and Polymorphism

Object-oriented Programming. Object-oriented Programming

CSc 520. Principles of Programming Languages 45: OO Languages Introduction

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CS 320 Introduction to Software Engineering Spring March 06, 2017

8. Polymorphism and Inheritance

Chapter 10 Classes Continued. Fundamentals of Java

CS 520 Theory and Practice of Software Engineering Fall 2017

QUESTIONS FOR AVERAGE BLOOMERS

PROGRAMMING LANGUAGE 2

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object Oriented Issues in VDM++

Inheritance and Polymorphism

Programming II (CS300)

Principles of Object-Oriented Design

Inheritance and Polymorphism

Polymorphism. Arizona State University 1

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

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

More Relationships Between Classes

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

Inheritance and Polymorphism

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

CST242 Object-Oriented Programming Page 1

Classes and Inheritance Extending Classes, Chapter 5.2

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Inheritance (continued) Inheritance

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

CSC207H: Software Design SOLID. CSC207 Winter 2018

The major elements of the object-oriented model

Chapter 14 Abstract Classes and Interfaces

CPS 506 Comparative Programming Languages. Programming Language

CS558 Programming Languages Winter 2013 Lecture 8

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Strict Inheritance. Object-Oriented Programming Winter

ECE 3574: Dynamic Polymorphism using Inheritance

What is Inheritance?

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

CS 251 Intermediate Programming Inheritance

Object-Oriented Design II

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

Rules and syntax for inheritance. The boring stuff

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

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

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

3. Object-Oriented Databases

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

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Self-review Questions

G Programming Languages - Fall 2012

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Transcription:

Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can hold an object of a subclass subclass can be passed to an function requiring superclass reference/pointer

Inheritance: is-a Relationships Inheritance should only be used when an is-a relationship exists between the base and the derived class e.g. A Ball is a Sphere, so a Ball class could be derived from a Sphere class, however A Color is not a Ball (!!) so a Color class should not be derived from a Ball class (or vice versa) In programming terms a derived class should be type-compatible with all of its ancestor classes That is, an instance of a derived class can be used instead of an instance of the base class

When to use inheritance? An important principle in software design for writing code that is easy to maintain and re-use is the Open-Closed principle: Software entities like classes, modules and functions should be open for extension but closed for modifications In terms of classes, this means that creating a subclass should not necessitate changing the superclass Any subclass should comply with the (Liskov) Substitution Principle otherwise it may violate the Open-Closed Principle Reference: http://www.objectmentor.com/resources/articles/lsp.pdf

(Liskov) Substitution Principle The supertype s behavior must be supported by the subtypes: subtype objects can be substituted for supertype objects without affecting the behavior of the using code. i.e., functions that use pointers/references to base class objects must be able to use objects of derived classes without knowing it. Example: Rectangle and Square classes

When to use inheritance? The key is that an is-a relationship should apply to the behavior of objects, not to the real-world entities that the objects are modeling A square is a rectangle but The behavior of a Square object is not consistent with the behavior of a Rectangle object! The test, therefore, is that a subclass public behavior should conform to the expected behavior of its base class This is important because a client program may depend (or expect) that this conformity exists

Polymorphism Polymorphism means many forms A Subclass definition can include a redefinition of a superclass method The subclass method then overrides the superclass method If the calling object is a subclass object, the subclass version of the method is used This is useful if the subclass is required to perform the same action but in a different way Note that overriding is not the same as overloading An overloaded method is one where there is another method with the same name, but a different parameter list

Dynamic Binding The correct version of an overridden method is decided at execution time, not at compilation time, based on the type to which a pointer refers This is known as dynamic binding or late binding This allows a superclass reference to be used to refer to a subclass object while still eliciting the appropriate behavior which method is chosen is based on the object type rather than the pointer/reference type this allows the old code (code of the superclass) to call new code (code of the subclass)!

Java and Dynamic Binding In Java, all object variables are references to objects and all methods have dynamic binding A base class reference variable that refers to a subclass object will use the subclass methods But does not have access to subclass methods that do not exist in the base class Containers that store a base class can be used to store subclass objects When retrieving objects from such a container the subclass specific methods can be accessed by first casting the object to a subclass reference Note that all Java classes are subclasses of Object

C++ and Dynamic Binding Unlike Java, C++ object variables are not references/pointers unless explicitly declared so, therefore a C++ object variable uses static memory Dynamic binding can happen only for pointers and references to objects (which method to call will be decided base on the type of object not on type of pointer or reference). However, dynamic binding is not automatically used as in Java! Unless the method is declared virtual the static binding is used (i.e., depends on type of pointer or reference)