Inheritance and Substitution (Budd chapter 8, 10)

Size: px
Start display at page:

Download "Inheritance and Substitution (Budd chapter 8, 10)"

Transcription

1 Inheritance and Substitution (Budd chapter 8, 10) 1

2 2 Plan The meaning of inheritance The syntax used to describe inheritance and overriding The idea of substitution of a child class for a parent The various forms of inheritance The cost and benefits of inheritance

3 Abstract idea of Inheritance 3 We motivated the idea of inheritance with a hierarchy of categories:

4 4 Practical Meaning of Inheritance Data members in the parent are part of the child Behavior defined in the parent are part of the child Note that private aspects of the parent are part of the child, but are not accessible within the child class.

5 5 Private, Public and Protected Levels of visibility modifiers: private: accessible only within the class definition (but memory is still found in the child class, just not accessible). public: accessible anywhere protected: accessible within the class definition or within the definition of child classes. default: package access Note: Java interprets protected to also mean accessible within same package

6 Inheritance is both Extension and Contraction Because the behavior of a child class may be strictly larger than the behavior of the parent, the child is an extension of the parent. (larger) Because the child can override behavior to make it fit a specialized situation, the child is a contraction of the parent. (smaller) This interplay between inheritance and overriding, extension and contraction, is what allows object-oriented systems to take very general tools and specialize them for specific projects. This interplay is ultimately the source of a great deal of the power of OOP. 6

7 7 The is-a Rule Our idealization of inheritance is captured in a simple rule-of-thumb. Try forming the English sentences ``An A is-a B''. If it ``sounds right'' to your ear, then A can be made a subclass of B. A dog is-a mammal, and therefore a dog inherits from mammal A car is-a engine sounds wrong, and therefore inheritance is not natural. but a car has-a engine.

8 Reuse of Code, Reuse of Concept 8 Why do we use inheritance? Reuse of code. Methods defined in the parent can be made available to the child without rewriting. Makes it easy to create new abstractions. Reuse of concept. Methods described in the parent can be redefined and overridden in the child. Although no code is shared between parent and child, the concept embodied in the definition is shared. An example of the latter, all graphical objects know how to draw.

9 9 Syntax for Inheritance Languages use a variety of different syntax to indicate inheritance: class Wall : public GraphicalObject -- c++ class Wall extends GraphicalObject -- Java class Wall : GraphicalObject -- C# (defclass Wall (GraphicalObject) () ) -- CLOS type Wall = object (GraphicalObject) -- Object Pascal class Wall < GraphicalObject -- Ruby

10 Trees vs Forests 10 There are two common views of class hierarchies: All classes are part of a single large class hierarchy. Thus, there is one class that is the original ancestor of all other classes. Huge tree Smalltalk, Java and Delphi Pascal do this. Every object is guarantee to have a minimum level of functionality. Classes are only placed in hierarchies if they have a relationship - results in a forest of many small hierarchies, but no single ancestor. C++, Objective-C, and Apple Object Pascal do this.

11 An Argument for Substitution 11 Consider the following argument: Instances of the subclass must possess all data members associated with the parent class. Instances of the subclass must implement, through inheritance at least (if not explicitly overridden) all functionality defined for the parent class. (They can also define new functionality). Thus, an instance of a child class can mimic the behavior of the parent class. Thus, an instance of a child class should be indistinguishable from an instance of a parent class if substituted in a similar situation. Principle of substitution Principle of Substitution: if class B is a subclass of class A, then it should be possible to substitute instances of class B for instances of class A in any situation with no observable effect. Isthisa validargument?

12 Subclass vs Subtype 12 This is not always a valid argument The problem with this argument is that a child class can override a method and make arbitrary changes. It is therefore useful to define two separate concepts: To say that A is a subclass of B merely asserts that A is formed using inheritance. To say that A is a subtype of B asserts that A preserves the meaning of all the operations in B. It is possible to form subclasses that are not subtypes; and form subtypes that are not subclasses.

13 Syntax for Overriding 13 Some languages, such as C++, require that the programmer indicate in the parent class that overriding is a potential (virtual methods) Other languages, such as Object Pascal, require a modifier in the child class that overriding has taken place (override) Other languages (C#, Delphi) require indications in both parent and child. Some languages (Smalltalk) do not require any indication in either parent class or child class.

14 14 Interfaces and Abstract Classes An interface is similar to a class, but does not provide any implementation. A child class must override all methods. A middle ground is an abstract class. Here some methods are defined, and some (abstract methods) are undefined. A child class must fill in the definition for abstract methods An interface is like an abstract class in which all methods are abstract.

15 Forms of Inheritance 15 The choices between inheritance and overriding, subclass and subtypes, mean that inheritance can be used in a variety of different ways and for different purposes. Many of these types of inheritance are given their own special names. We will describe some of these specialized forms of inheritance. Specialization Specification Construction Generalization or Extension Limitation Variance

16 16 Specialization Inheritance By far the most common form of inheritance is for specialization. A good example is the Java hierarchy of Graphical components in the AWT: Component Label Button TextComponent CheckBox ScrollBar Each child class overrides a method inherited from the parent in order to specialize the class in some way.

17 17 Specification Inheritance If the parent class is abstract, we often say that it is providing a specification for the child class, and therefore it is specification inheritance (a variety of specialization inheritance). Example: Java abstract classes

18 18 Inheritance for Construction If the parent class is used as a source for behavior, but the child class has no is-a relationship to the parent, then we say the child class is using inheritance for construction. An example might be subclassing the idea of a Set from an existing List class. Generally not a good idea, since it can break the principle of substituability, but nevertheless sometimes found in practice.

19 19 Inheritance for Generalization or Extension If a child class generalizes or extends the parent class by providing more functionality, but does not override any method. It is called inheritance for generalization. The child class doesn't change anything inherited from the parent, it simply adds new features.

20 20 Inheritance for Limitation If a child class overrides a method inherited from the parent in a way that makes it unusable (for example, issues an error message), then we call it inheritance for limitation. For example, you have an existing List data type that allows items to be inserted at either end, and you override methods allowing insertion at one end in order to create a Stack. Generally not a good idea, since it breaks the idea of substitution. But again, it is sometimes found in practice.

21 21 Inheritance for Variance Two or more classes that seem to be related, but its not clear who should be the parent and who should be the child. Example: Mouse and Tablet Better solution, abstract out common parts to new parent class, and use subclassing for specialization.

22 22 Inheritance for Combination Subclass that represents a combination of features from two or more parent classes Example: teaching assistant has characteristics of both teacher and student Inheritance from two or more parent classes is known as multiple inheritance More complex than single inheritance

23 23 Benefits of Inheritance Software Reuse (avoids rewrite code) Code Sharing (many users: software-ic, many classes) Improved Reliability (the more the code is used ) Consistency of Interface (when 2 classes inherit an interface) Rapid Prototyping Polymorphism Information Hiding

24 24 Cost of Inheritance Execution speed Program size Message Passing Overhead Program Complexity (overuse: yo-yo problem) This does not mean you should not use inheritance, but rather than you must understand the benefits, and weigh the benefits against the costs.

25 25 Subclasses and Subtypes To assert that one class is a subclass of another is to simply say it was built using inheritance. It is a statement concerning how it was constructed. To assert that one class is a subtype of another is to say that it preserves the purpose of the original. It is a statement concerning meaning. The distinction between subtype and subclass is important because of their relationship to substitution.

26 26 What is a type? What do we mean when we use the term type in describing a programming language? A set of values. (The type int, for example, describes to ) A set of operations. (We can do arithmetic on ints, not on booleans). A set of properties. (If we divide 8 by 5 we are not surprized when the result is 1, and not 1.6). What about when we consider classes as a system for defining types?

27 27 The Problem of Defining Types Consider how we might define a Stack ADT: interface Stack { } public void push (Object value); public Object top (); public void pop (); Notice how the interface itself says nothing about the LIFO property, which is the key defining feature of a stack

28 28 The Definition of Subtype A subtype preserves the meaning (purpose, or intent) of the parent. Problem, meaning is extremely difficult to define. Think about how to define the LIFO characteristics of the stack.

29 29 The Substitution Paradox There is a curious paradox that lies at the heart of most strongly typed object-oriented programming languages. Substitution is permitted, based on subclasses. That is, a variable declared as the parent type is allowed to hold a value from a child type. Yet from a semantic point of view, substitution only makes sense if the expression value is a subtype of the target variable. Ifsubstitution only makes sense for subtypes and not for all subclasses, why do programming languages based the validity of assignment on subclasses?

30 The Undecidability of the Subtype Relationship 30 It is trivial to determine if one class is a subclass of another. It is extremely difficult to define meaning (think of the Stack ADT), and even if you can it is almost always impossible to determine if one class preserves the meaning of another. There is no procedure that can determine, in general, if two programs have equivalent behavior

31 31 Is This a Problem? What does it take to create a subclass that is not a subtype? The new classmustoverride at least one method from the parent It must preserve the type signatures But itmust violate some important property of the parent Is this common? Not likely. But it shows you where to look for possible problems.

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap In this chapter we will start to investigate the concepts of inheritance and substitution: The intuitive and practical

More information

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות עי דר גרא וייס Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס 2 Roadmap In this chapter we will start to investigate the

More information

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

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze

More information

Inheritance. Transitivity

Inheritance. Transitivity Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated

More information

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון Overriding עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap A method in a child class overrides a method in the parent class if it has the same name and type signature: Parent void method(int,float)

More information

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

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1 Object-Oriented Languages and Object-Oriented Design Ghezzi&Jazayeri: OO Languages 1 What is an OO language? In Ada and Modula 2 one can define objects encapsulate a data structure and relevant operations

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

CPS 506 Comparative Programming Languages. Programming Language

CPS 506 Comparative Programming Languages. Programming Language CPS 506 Comparative Programming Languages Object-Oriented Oriented Programming Language Paradigm Introduction Topics Object-Oriented Programming Design Issues for Object-Oriented Oriented Languages Support

More information

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd. L4: Inheritance Inheritance Definition Example Other topics: Is A Test, Reasons for Inheritance, C++ vs. Java, Subclasses and Subtypes 7 Forms of Inheritance Discussions Chapter 8 and 10 of Budd. SFDV4001

More information

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

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

Inheritance -- Introduction

Inheritance -- Introduction Inheritance -- Introduction Another fundamental object-oriented technique is called inheritance, which, when used correctly, supports reuse and enhances software designs Chapter 8 focuses on: the concept

More information

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc. Chapter 13 Object Oriented Programming Contents 13.1 Prelude: Abstract Data Types 13.2 The Object Model 13.4 Java 13.1 Prelude: Abstract Data Types Imperative programming paradigm Algorithms + Data Structures

More information

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap In this chapter we will examine how differences in static and dynamic features effect object-oriented programming

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming) C++ Programming: Introduction to C++ and OOP (Object Oriented Programming) 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Brief introduction to C++ OOP vs. Procedural

More information

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

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance CS257 Computer Science I Kevin Sahr, PhD Lecture 10: Inheritance 1 Object Oriented Features For a programming language to be called object oriented it should support the following features: 1. objects:

More information

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

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction Lecture 13: Object orientation Object oriented programming Introduction, types of OO languages Key concepts: Encapsulation, Inheritance, Dynamic binding & polymorphism Other design issues Smalltalk OO

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

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

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Object Relationships

Object Relationships Object Relationships Objects can work together in three different types of relationships: Uses: An object can use another to do some work (association). Composition: A complex object may be composed of

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

More information

Concepts of Programming Languages

Concepts of Programming Languages Concepts of Programming Languages Lecture 10 - Object-Oriented Programming Patrick Donnelly Montana State University Spring 2014 Patrick Donnelly (Montana State University) Concepts of Programming Languages

More information

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation CSE 452: Programming Languages Data Abstraction and Object-Orientation Previous Lecture Abstraction Abstraction is the elimination of the irrelevant and the amplification of the essential Robert C Martin

More information

Multiple Inheritance לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Multiple Inheritance לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון Multiple Inheritance לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap In this chapter we will investigate some of the logical problems that can arise when a language allows a child class

More information

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language Categories of languages that support OOP: 1. OOP support is added to an existing language - C++ (also supports procedural and dataoriented programming) - Ada 95 (also supports procedural and dataoriented

More information

OOPLs - Inheritance. Inheritance

OOPLs - Inheritance. Inheritance OOPLs - Inheritance Desirable properties Models of inheritance Class-based: with single, multiple parents Delegation Mix-ins Functionality as code reuse as subtyping 1 Inheritance Data abstraction plus

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L 10.1 10.3 1 Interface Hierarchies Inheritance can

More information

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design Computer Science 4U Unit 1 Programming Concepts and Skills Modular Design Modular Design Reusable Code Object-oriented programming (OOP) is a programming style that represents the concept of "objects"

More information

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism Type Joseph Spring Discussion Languages and Type Type Checking Subtypes Type and Inheritance and 7COM1023 Programming Paradigms 1 2 Type Type denotes the kind of values that programs can manipulate: Simple

More information

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented

More information

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods Lecture 12 Summary Inheritance Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods Multiplicity 1 By the end of this lecture, you will

More information

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע "י ד"ר גרא וייס

Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע י דר גרא וייס Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע "י ד"ר גרא וייס 2 Roadmap In this chapter we will examine how differences

More information

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1 Inheritance Reuse of code Extension and intension Class specialization and class extension Inheritance The protected keyword revisited Inheritance and methods Method redefinition The composite design pattern

More information

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 17 Inheritance Overview Problem: Can we create bigger classes from smaller ones without having to repeat information? Subclasses: a class inherits

More information

Multiple Inheritance עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Multiple Inheritance עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון Multiple Inheritance עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap In this chapter we will investigate some of the logical problems that can arise when a language allows a child class to

More information

Object-oriented Programming. Object-oriented Programming

Object-oriented Programming. Object-oriented Programming 2014-06-13 Object-oriented Programming Object-oriented Programming 2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ 1 Overloading, Overriding POLYMORPHISM poly morphos many forms Overloading single method name having several alternative implementations. Overriding child class provides

More information

Conformance. Object-Oriented Programming Spring 2015

Conformance. Object-Oriented Programming Spring 2015 Conformance Object-Oriented Programming 236703 Spring 2015 1 What s Conformance? Overriding: replace method body in sub-class Polymorphism: subclass is usable wherever superclass is usable Dynamic Binding:

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented

More information

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D) DATA TYPES CS 403: Types and Classes Stefan D. Bruda Fall 2017 Algorithms + data structures = programs Abstractions of data entities highly desirable Program semantics embedded in data types Data types

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

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

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia Object Oriented Programming in Java Jaanus Pöial, PhD Tallinn, Estonia Motivation for Object Oriented Programming Decrease complexity (use layers of abstraction, interfaces, modularity,...) Reuse existing

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship The "is-a" Relationship Object classification

More information

CS 251 Intermediate Programming Inheritance

CS 251 Intermediate Programming Inheritance CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?

More information

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? QUIZ Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? Or Foo(x), depending on how we want the initialization

More information

Subtyping (Dynamic Polymorphism)

Subtyping (Dynamic Polymorphism) Fall 2018 Subtyping (Dynamic Polymorphism) Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References PFPL - Chapter 24 Structural Subtyping - Chapter 27 Inheritance TAPL (pdf) - Chapter

More information

Implications of Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Implications of Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון Implications of Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap In this chapter we will investigate some of the implications of the principle of substitution in statically typed

More information

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

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every

More information

Inheritance and Polymorphism in Java

Inheritance and Polymorphism in Java Inheritance and Polymorphism in Java Introduction In this article from my free Java 8 course, I will be discussing inheritance in Java. Similar to interfaces, inheritance allows a programmer to handle

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship Object classification is typically hierarchical.

More information

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Patterns for polymorphic operations

Patterns for polymorphic operations Patterns for polymorphic operations Three small object structural patterns for dealing with polymorphism Alexander A. Horoshilov hor@epsylontech.com Abstract Polymorphism is one of the main elements of

More information

OVERRIDING. 7/11/2015 Budditha Hettige 82

OVERRIDING. 7/11/2015 Budditha Hettige 82 OVERRIDING 7/11/2015 (budditha@yahoo.com) 82 What is Overriding Is a language feature Allows a subclass or child class to provide a specific implementation of a method that is already provided by one of

More information

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static,

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes

More information

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods. Dan Grossman Autumn 2018

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods. Dan Grossman Autumn 2018 CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018 What next? Have used classes for OOP's essence: inheritance, overriding, dynamic

More information

CS 520 Theory and Practice of Software Engineering Fall 2018

CS 520 Theory and Practice of Software Engineering Fall 2018 Today CS 520 Theory and Practice of Software Engineering Fall 2018 Object Oriented (OO) Design Principles September 13, 2018 Code review and (re)design of an MVC application (and encapsulation) Polymorphism

More information

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007 Object Fundamentals Part Three Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007 1 Lecture Goals Continue our tour of the basic concepts, terminology, and notations

More information

Programming Languages 2nd edition Tucker and Noonan"

Programming Languages 2nd edition Tucker and Noonan Programming Languages 2nd edition Tucker and Noonan" Chapter 13 Object-Oriented Programming I am surprised that ancient and Modern writers have not attributed greater importance to the laws of inheritance..."

More information

Introduction to Inheritance

Introduction to Inheritance Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits

More information

CS 520 Theory and Practice of Software Engineering Fall 2017

CS 520 Theory and Practice of Software Engineering Fall 2017 CS 520 Theory and Practice of Software Engineering Fall 2017 OO design principles September 14, 2017 Today Code review and (re)design of an MVC application OO design principles Information hiding (and

More information

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

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: has a CSE143 Sp Student. CSE 143 Java Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

Inheritance, Polymorphism, and Interfaces

Inheritance, Polymorphism, and Interfaces Inheritance, Polymorphism, and Interfaces Chapter 8 Inheritance Basics (ch.8 idea) Inheritance allows programmer to define a general superclass with certain properties (methods, fields/member variables)

More information

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel) Programmieren II Polymorphism Alexander Fraser fraser@cl.uni-heidelberg.de (Based on material from T. Bögel) June 4, 2014 1 / 50 Outline 1 Recap - Collections 2 Advanced OOP: Polymorphism Polymorphism

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

More information

Chapter 8: Class and Method Design

Chapter 8: Class and Method Design Chapter 8: Class and Method Design Objectives Become familiar with coupling, cohesion, and connascence. Be able to specify, restructure, and optimize object designs. Be able to identify the reuse of predefined

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis? Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator

More information

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson January 13, 2005 January 18, 2005 1 of 38 Lecture Goals Introduce the basic concepts of object-oriented analysis/design/programming

More information

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

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

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding Conformance Conformance and Class Invariants Same or Better Principle Access Conformance Contract Conformance Signature Conformance Co-, Contra- and No-Variance Overloading and Overriding Inheritance as

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 4: OO Principles - Polymorphism http://courses.cs.cornell.edu/cs2110/2018su Lecture 3 Recap 2 Good design principles.

More information

CSE341, Fall 2011, Lecture 26 Summary

CSE341, Fall 2011, Lecture 26 Summary CSE341, Fall 2011, Lecture 26 Summary Standard Disclaimer: This lecture summary is not necessarily a complete substitute for attending class, reading the associated code, etc. It is designed to be a useful

More information

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

More About Objects. Zheng-Liang Lu Java Programming 255 / 282 More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism

More information

Object-Oriented Concepts

Object-Oriented Concepts JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1 Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing

More information

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School Class Hierarchy and Interfaces David Greenstein Monta Vista High School Inheritance Inheritance represents the IS-A relationship between objects. an object of a subclass IS-A(n) object of the superclass

More information

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Inheritance CSC 123 Fall 2018 Howard Rosenthal Inheritance CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining what inheritance is and how it works Single Inheritance Is-a Relationship Class Hierarchies Syntax of Java Inheritance The super Reference

More information

Object Orientated Programming Details COMP360

Object Orientated Programming Details COMP360 Object Orientated Programming Details COMP360 The ancestor of every action is a thought. Ralph Waldo Emerson Three Pillars of OO Programming Inheritance Encapsulation Polymorphism Inheritance Inheritance

More information

Principles of Object-Oriented Design

Principles of Object-Oriented Design Principles of Object-Oriented Design 1 The Object-Oriented... Hype What are object-oriented (OO) methods? OO methods provide a set of techniques for analysing, decomposing, and modularising software system

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 21 Interfaces and Abstract Classes Overview Problem: Can we make inheritance flexible? Abstract methods Define methods that will be filled in by children

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Enhanced Class Design -- Introduction

Enhanced Class Design -- Introduction Enhanced Class Design -- Introduction We now examine several features of class design and organization that can improve reusability and system elegance Chapter 9 focuses on: abstract classes formal Java

More information

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

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor CSE 143 Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog

More information

Chapter 12. OOP: Creating Object- Oriented Programs. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

Chapter 12. OOP: Creating Object- Oriented Programs. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. Chapter 12 OOP: Creating Object- Oriented Programs McGraw-Hill Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. Objectives (1 of 2) Use object-oriented terminology correctly. Create

More information

Inheritance (an intuitive description)

Inheritance (an intuitive description) Inheritance (an intuitive description) Recall the Orange class properties found in Orange are also shared with other Fruits (e.g. Apple, Banana, Pineapple) We associate behavior as well as state with with

More information

Inheritance. OOP: Inheritance 1

Inheritance. OOP: Inheritance 1 Inheritance Reuse Extension and intension Class specialization and class extension Inheritance The protected keyword revisited Inheritance and methods Method redefinition An widely used inheritance example

More information

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

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

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information