Subclasses and Subtypes CMPS 2143. Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.

Slides:



Advertisements
Similar presentations
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance1 Inheritance Software re-use with derived classes.
Software Testing and Quality Assurance
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Ch 13. Features Found Only in Java Timothy Budd Oregon State University.
Abstract Classes and Interfaces
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
OOP Languages: Java vs C++
Comparison of OO Programming Languages © Jason Voegele, 2003.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of CS, Nipissing University.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Mechanisms for Reuse CMPS OOP billed as technology that permits software to be constructed from general-purpose reusable components Two main mechanisms.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Lecture 12 Inheritance.
Interface, Subclass, and Abstract Class Review
03/10/14 Inheritance-2.
Types of Programming Languages
Interfaces and Inheritance
Slides by Steve Armstrong LeTourneau University Longview, TX
Java – Inheritance.
Based on slides by Alyssa Harding & Marty Stepp
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Presentation transcript:

Subclasses and Subtypes CMPS 2143

Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning or purpose of the child class A class is a subtype if the child class maintains the property of the principle of substitution. 2

Substitutability Feature of statically typed OO languages is that the type associated with the value held by a variable may not exactly match ▫ Examples:  GraphicalObject g = new Ball (…);  Room room = new King (…);  Person p = new Buddy (…); 3

Types Variable has a value that is a member of a set of values The name of the type is a handle we use to describe the set ▫ eg. 17 is an int We know more!!! ▫ Set of operations ▫ Set of properties (eg. int + int  int So a type has values, operations, properties… 4

Abstract Data Types (ADTs) Example: Stack ▫ Starting place to describe a stack is the interface – list all the operations ▫ Next – assign meaning in implementation  retrieve most recently added value (LIFO)  One could incorrectly implement the stack to satisfy the interface, but not the properties ▫ So the interface does NOT specify the properties and the implementation may not either 5

Subtype vs subclass A new class is a subtype if it provides all the operations of the parent class AND satisfies the properties associated with the parent class. ▫ Of course the child class is free to add new operations Subtype relationship is described purely in terms of behavior ▫ Says nothing about how new class defined or constructed 6

So a subtype is a subclass, BUT a subclass is not necessarily a subtype. Can also have a subtype that is not a subclass ▫ Dictionary is a subtype of Array – interface look the same, but Dictionary does not extend Array 7

Substitutability Paradox 1.A child class inherits all data fields defined in the parent. 2.A child class must recognize all behaviors associated with the parent (inherit directly or override) 3.Therefore, an instance of a child class can be used in any situation where an instance of a parent class is expected, with no observable difference. WEAK LINK in STEP 2! 8

Cont. Simply asserting the child class satisfies the interface common to the parent class does not ensure it will satisfy any of the properties of the parent. ▫ Example: Could inherit from Stack class, and violate the LIFO property. Language/Compiler CANNOT check this. ▫ Can ensure no inherited method is deleted (no mechanism to do this) ▫ Can ensure member data that is public is not made private ▫ BUT THAT’s IT. 9

Is this a problem? What would it take to create a subclass that is not also a subtype? ▫ Redefine an inherited operation while comprising some property without violating the type signature ▫ So yes, it can be done ▫ BUT, not commonly 10

Subclassing for construction A common situation where subclasses are created that are not subtypes is when inheritance is used purely for reusing code. Example ▫ class Dictionary : List {…}; ▫ Dictionary d = new Dictionary (..); ▫ Can determine how many elements are in a dictionary, because you could call the list method size d.size() 11

Cont. But now all the list methods can be used with this new class, and some may not be appropriate 12

Dynamically typed languages Subclasses vs subtypes (and types in general) used differently The set of operations that a variable of a certain type must recognize is termed its protocol. Any value passed as an argument to one its methods must usable within the method ▫ Example (C++ not dynamically typed, but templates close) template public T compareTo (T arg1, T arg2) { if arg1 < arg 2 then …. ▫ < has to be defined for T 13

Pre- and Postconditions Traditional way to specify behavior of ADT Precondition – states what must be true before a method can be executed Postcondition – states what is guaranteed to be true after the method is executed (as long as precondition was met) We can informally do this in comments We can use assertions (assert statement in C++) Eiffel more formal about it ▫ Child class CANNOT override pre- and post-conditions 14

Example: //precond: Stack s is not full //postcond: item is inserted at top of s, and s is // unchanged otherwise public void push (ItemType item); 15

Study questions – page , 9, 12 16