Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.

Slides:



Advertisements
Similar presentations
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
Inheritance CMPS Overview Stream classes File objects File operations with streams Examples in C++ and Java 2.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Advanced Object-Oriented Programming Features
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
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.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
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.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Object-Oriented Programming Chapter Chapter
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
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 Chapter 11 Object-Oriented.
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.
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,
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
L 3.1 Forms of Inheritance Inheritance is used in a variety of way and for a variety of different purposes. Inheritance for Specialization Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
ISBN Chapter 12 Support for Object-Oriented Programming.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Advanced Programming Behnam Hatami Fall 2017.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes

Understanding Inheritance (Budd's UOOPJ, Ch. 8) This is a set of slides to accompany chapter 8 of Timothy Budd's textbook Understanding Object-Oriented Programming with Java, Updated Edition (Addison-Wesley, 2000) Created: 14 August 2004

Motivation for Inheritance Use inheritance to create new software structures from existing software units to:  improve productivity  enhance quality 1

Generality and Specialization in Software Development Conflict:  Specific projects usually require very specialized software  Reusability usually requires very general software Resolution?  Inheritance allows general software to be specialized for a project 2

Abstract Idea of Inheritance 3 Material Object Non-Living Thing Rock Air Living Thing Plant Animal Reptile Mammal Human Being DentistRoy Shopkeeper Flo WriterJohn Cat Dog Platypus

Practical Meaning of Inheritance In programming languages, inheritance means  Data and behavior of parent class are part of child  Child class may include data and behavior not in parent With respect to the parent class, a child class is, in some sense  An extension – larger set of properties  A contraction – more specialized (restricted) objects 4

Idealized Image of Inheritance Consider  Subclass instances must possess all data areas of the parent  Subclass instances must implement all functionality of the parent Thus  Subclass instance should be indistinguishable from parent class instance—child can be substituted for parent 5

Principle of Substitutability If C is a subclass of P, instances of C can be substituted for instances of P in any situation with no observable effect. 6

Subclass and Subtype Subtype  Class that satisfies principle of substitutability Subclass  Something constructed using inheritance, whether or not it satisfies the principle of substitutability. The two concepts are independent  Not all subclasses are subtypes  Sometimes subtypes are constructed without being subclasses 7

Forms of Inheritance Specialization  Child class is a special case (subtype) of parent Specification  Parent class defines behavior implemented in the child, but not parent Construction  Parent class used only for its behavior -- child class is not subtype -- no is-a relationship to parent Generalization  Child class modifies or overrides some methods of parent, extends the behavior to more general kind of object Extension  Child class adds new functionality to parent, but does not change any inherited behavior Limitation  Child class limits some of the behavior of parent Variance  Child and parent class are variants of each other -- inheritance to allow code sharing -- arbitrary relationship Combination  Child class inherits features from more than one parent -- multiple inheritance 8

Forms of Inheritance Specialization Child class is a special case (subtype) of parent  Most common form of inheritance  Example: Professor is specialized form of Employee  Child may override behavior of parent to specialize  Child satisfies specification of parent in all relevant aspects  Preserves substitutability 9

Forms of Inheritance Specification behavior Parent class defines behavior implemented in the child, but not parent  Second next most common form of inheritance  Example: class StackInArray gives implementations for method signatures defined in abstract class Stack Java: StackInArray extends Stack  Example: class ArrayRankedSeq gives implementations for method signatures defined in interface RankedSequence Java: ArrayRankedSeq implements RankedSequence  Subclasses are realizations of incomplete abstract specification  Defines common interface for group of related classes  Preserves substitutability 10

Forms of Inheritance Construction Parent class used only for its behavior — child class is not subtype — no is-a relationship to parent  Sometimes used for convenience, but discouraged  Example: extending List class to develop Set, without "hiding" unneeded methods  Example: extending a byte-based I/O stream to a stream for handling other objects  Often violates substitutability  More common in dynamically typed languages (e.g., Smalltalk) than in statically typed (e.g., Java)  Can sometimes use aggregation instead 11

Forms of Inheritance Generalization Child class modifies or overrides some methods of parent, extends the behavior to more general kind of object  Sometimes used for convenience (or necessity), but discouraged  Example: graphics Window generalized to ColorWindow (with background color)  Opposite of specialization—violates substitutability  Used when must build from fixed, difficult-to-modify set of classes  Where possible, invert class hierarchy or use aggregation 12

Forms of Inheritance Extension Child class adds new functionality to parent, but does not change any inherited behavior  Useful technique to give new behaviors to existing base class that cannot be modified  Example: StringSet extends Set, adding string-related methods (e.g, prefix search)  Preserves substitutability 13

Forms of Inheritance Limitation Child class limits some of the behavior of parent  Sometimes used for convenience, but strongly discouraged  Example: extends, replacing unneeded methods to give error messages  Example: Stack extends DoubleEndedQueue, replacing unneeded methods to give error messages  Violates substitutability  Used when must build from fixed, difficult-to-modify set of classes  Avoid when possible, perhaps use aggregation 14

Forms of Inheritance Variance Child and parent class are variants of each other—inheritance to allow code sharing— arbitrary relationship  Sometimes used for convenience, but discouraged  Example: graphics class extending to share similar control code  Example: graphics Tablet class extending Mouse to share similar control code  Violates substitutability  Better to define more general parent class like  Better to define more general parent class like PointingDevice 15

Forms of Inheritance Combination Child class inherits features from more than one parent—multiple inheritance  Example: might inherit from both and  Example: GraduateInstructor might inherit from both GraduateStudent and Faculty  Often difficult to understand and to implement language  Often use to "mix-in" specification of another role or protocol  Java has single inheritance via subclassing (extends), but multiple inheritance for specification via interface implementations (implements) 16

Inheritance and Assertions Suppose C is a subtype of P   P and C have interface invariants I and IC, respectively   meth() is a public method of P with precondition Q and postcondition R   meth() in C has precondition QC and postcondition RC Subtype C should not violate I, Q, and R   IC implies I – may strengthen invariant – extend interface and data   Q implies QC – may weaken precondition – expand valid inputs   RC implies R -- may strengthen postcondition – restrict valid outputs Abstract preconditions can enable controlled "strengthening" of precondition   Consider BoundedStack inheriting from an unbounded Stack class   Give method push() a "not full" precondition – always true in Stack   Refine "not full" in subclass BoundedStack to be true or false 17

Inheritance and Assertions Interface invariants meth() Pre- condition Post- condition P (Parent) IQR C (Child) ICQCRC IC=>IQ=>QCRC=>R strengthenweakenstrengthen 18

Trees versus Forests Two common views of class hierarchies Tree  All classes are part of single class hierarchy  Advantage: root's functionality inherited by all objects – all have basic functionality  Disadvantage: tight coupling of classes, large libraries for an application  Languages: Java's classes, Smalltalk, Objective C, Delphi Object Pascal Forest  Classes only placed in hierarchies if they have a relationship – many small hierarchies.  Advantage: smaller libraries of classes for application, less coupling possible  Disadvantage: no shared functionality among all objects  Languages: Java's interfaces, C++, Apple Object Pascal 19

Tree versus Forest A B C D EFG Tree 20

Trees versus Forests Forest A B C D EFG X Y Z 21

Inheritance in Java Tree-structured class hierarchy Forest-structured interface hierarchy Modifiers for classes/interfaces Visibility modifiers for class/interface features 22

Inheritance in Java Tree-structured Class Hierarchy Root class is Object Other classes extend exactly one other class   default is Object Declaration uses keyword extends after class name 23 Object Material_Object Non_living_ThingLiving_Thing ……

Inheritance in Java Forest-structured Interface Hierarchy interface defines an interface specification implements after class name to promise implementation of interface – inheritance for specification An interface extends zero or more other interfaces A class implements zero or more interfaces public interface Queue { // signatures of public methods // Queues must provide } public class QueueAsLinkedList implements Queue { // includes implementations of // the Queue methods } 24

Inheritance in Java Modifiers for Classes/Interfaces abstract classes cannot be instantiated – interfaces are abstract by default final classes cannot be extended public classes/interfaces are visible everywhere, otherwise only visible within current package 25

Inheritance in Java Visibility Modifiers for Class/Interface Features public features accessible from anywhere in program private features accessible from inside class only Default-access (i.e., "friendly") features accessible from inside the current Java package protected features accessible in package or inside any child class 26

Inheritance in Java public abstract class Stack { // extends Object by default // data definitions plus signatures and // data definitions plus signatures and // possibly implementations of methods // possibly implementations of methods} public class StackInArray extends Stack { // extended features plus overridden implementations } public interface Queue { // signatures of public methods Queues must provide } public class QueueAsLinkedList implements Queue { // includes implementations of the Queue methods } 27

Facilities of Root Class Object Minimum functionality for all objects include equals(Object obj) is obj the same as receiver? toString() converts the object to a string value hashCode() return a default hashcode for the object getClass() return an identifier for the class of the object First three above are often overridden in classes. 28

Benefits of Inheritance Software reusability (among projects) Code sharing (within a project) Increased reliability (resulting from reuse and sharing of well-tested code) Consistency of interface (among related objects) Rapid prototyping (quickly assemble from pre- existing components) Polymorphism and frameworks (high-level reusable components) Information hiding 29

Costs of Inheritance Execution speed Program size Message-passing overhead Program complexity 30

Acknowledgement 31 This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).” This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).”