CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Semantics Static semantics Dynamic semantics attribute grammars
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
5/17/2015 OO Design: Liskov Substitution Principle 1.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
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. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Inheritance1 Inheritance Software re-use with derived classes.
Software Testing and Quality Assurance
Introduction to Inheritance Fall 2005 OOPD John Anthony.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Chapter 10 Classes Continued
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
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) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
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.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of CS, Nipissing University.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
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.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Chapter 3 Part II Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
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.
 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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Principles of programming languages 10: Object oriented languages
Type Abstraction SWE Spring 2009.
Type Abstraction Liskov, Chapter 7.
Semantics In Text: Chapter 3.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Type Abstraction SWE Spring 2013.
Presentation transcript:

CSSE501 Object-Oriented Development

Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts of subclass and subtype 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

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 The principle of substitutability is sometimes called Liskov substitutability, since one of the first people to describe the idea was Barbara Liskov, of MIT.

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 (syntax) To say that A is a subtype of B asserts that A preserves the meaning of all the operations in B (semantics)

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 surprised when the result is 1, and not 1.6)  What about when we consider classes (or interfaces) as a system for defining types?

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. Is the following a stack? class NonStack implements Stack { public void push (Object value) { v = value; } public Object top () { return v; } public void pop () { v = null; } private Object v = null; }

Subtype  Type A is a subtype of type B if A's specification implies B's specification Subtypes must satisfy the substitution principle, which means that any code that relies on B's specification will function properly if A's implementation is substituted for B  In order to satisfy substitutability, three properties must hold: Signature rule. The subtype must have all the methods of the supertype, with compatible signatures. The signatures for a method are compatible if:  the subtype's method returns the same or a subtype of the type returned by the supertype's method  the subtype's argument types are the same or supertypes of the supertype's arguments, and  the subtype throws no more exceptions than the supertype

Subtype (Cont.) Methods rule. A subtype's specification for any method must be at least as strong as its supertype's specification. So, a subtype method can weaken the precondition and can strengthen the postcondition. This is different from the signature rule because this has to do with specifications, while the methods rules only involves method signatures (the signatures rule could be checked by the compiler, but the methods rule could not) Properties rule. The subtype must preserve all properties that can be proved about the supertype. Invariants that are guaranteed in the supertype must also be guaranteed by the subtype.

Subclass vs. Subtype (Cont.)  It is possible to form subclasses that are not subtypes; and (in some languages at least) form subtypes that are not subclasses

The Definition of Subtype  So now we can better understand the concept of a 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

Subclasses are not Necessarily Subtypes  It is easy to create a subclass that is not a subtype -- think of NonStack  It is also possible to create subtypes that are not subclasses

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 derived 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  If substitution only makes sense for subtypes and not for all subclasses, why do programming languages based the validity of assignment on subclasses?

The Undecidability of the Subtype Relationship  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  One of the classic corollaries of the halting problem is that there is no procedure that can determine, in general, if two programs have equivalent behavior

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

Discussion  Which form of inheritance will definitely violate the rule of substitutability? Specialization Specification Construction Generalization Extension Limitation Variance

Chapter Summary  To say a class is a subclass of another simply asserts that is was built using inheritance  By itself, the subclass relationship says nothing about the behavior of the child in relation to the parent  The term subtype is used to describe a class that matches the behavior of another class  It is easy to build subclasses that are not subtypes. It is possible (although not as easy) to build subtypes that are not subclasses