CSCI-383 Object-Oriented Programming & Design Lecture 15.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Inheritance CMPS Overview Stream classes File objects File operations with streams Examples in C++ and Java 2.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Computer Science I Inheritance Professor Evan Korth New York University.
CSSE501 Object-Oriented Development
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
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.
Introduction to Object-oriented programming and software development Lecture 1.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Object Oriented Programming Principles Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-3.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 12 Support for Object oriented Programming.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Lecture 5 : Inheritance & Polymorphism Acknowledgement : courtesy of Prof. Timothy Budd lecture slides.
Object-Oriented Programming Chapter Chapter
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of Computer Science, Nipissing University.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Classes, Interfaces and Packages
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Lecture 12 Inheritance.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Advanced Programming Behnam Hatami Fall 2017.
Presentation transcript:

CSCI-383 Object-Oriented Programming & Design Lecture 15

Chapter 8 Inheritance and Substitution

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Abstract Idea of Inheritance  We motivated the idea of inheritance with a hierarchy of categories:

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Private, Public and Protected  There are now three 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Inheritance is both Extension and Contraction  Because the behavior of a child class is 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Reuse of Code, Reuse of Concept  Why do we use inheritance? Basically there are two major motivations: 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Trees vs Forests  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. Smalltalk, Java and Delphi Pascal do this 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd A portion of the Little Smalltalk Hierarchy

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd An Argument for Substitution  Consider the following argument: Instances of the subclass must possess all data areas 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, but that is unimportant for the present argument) Thus, an instance of a child class can mimic the behavior of the parent class. It therefore seems reasonable that a variable declared as a parent, should be able to hold a value generated from the child class

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Principle of Substitution If we have two classes, A and B, such that class B is a subclass of class A, it should be possible to substitute instances of class B for instances of class A in any situation and with no observable effect Note: The principle of substitutability is sometimes called Liskov substitutability, since one of the first people to describe the idea was Barbara Liskov, of MIT.

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Subclass vs Subtype  Of course, 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 (in some languages at least) form subtypes that are not subclasses

The two “subs”  A class A is a subtype of a class B if the principle of substitution holds for the relationship between the classes  A class A is a subclass of a class B is the substitution principle may or may not hold for the relationship between the classes

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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. In C++ an abstract method is called a pure virtual method abstract class Window {... abstract public void paint(); // child class must redefine... }

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Forms of Inheritance  Many 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Specialization Inheritance  By far the most common form of inheritance is for specialization  Each child class overrides a method inherited from the parent in order to specialize the class in some way

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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)

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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, we call it inheritance for generalization  The child class doesn't change anything inherited from the parent, it simply adds new features

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Inheritance for Variance  Two or more classes that seem to be related, but it is not clear who should be the parent and who should be the child Example: Mouse and TouchPad and JoyStick  Better solution, abstract out common parts to new parent class, and use subclassing for specialization

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Benefits of Inheritance  Software Reuse  Code Sharing  Improved Reliability  Consistency of Interface  Rapid Prototyping  Information Hiding

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Cost of Inheritance  Execution speed  Program size  Message Passing Overhead  Program Complexity  This does not mean you should not use inheritance, but rather than you must understand the benefits, and weigh the benefits against the costs