CSSE501 Object-Oriented Development

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
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.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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.
Object-oriented Programming Concepts
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Computer Science I Inheritance Professor Evan Korth New York University.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
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.
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.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Chapter 8 More Object Concepts
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
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.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
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.
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.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
(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.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of Computer Science, Nipissing University.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
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,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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.
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)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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.
ISBN Chapter 12 Support for Object-Oriented Programming.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Understanding Inheritance
MSIS 670 Object-Oriented Software Engineering
Advanced Programming Behnam Hatami Fall 2017.
Presentation transcript:

CSSE501 Object-Oriented Development

Chapter 8: Inheritance and Substitution In this chapter we will start to investigate the concepts of inheritance and substitution The intuitive and practical meanings of inheritance The syntax used to describe inheritance and substitution Some of the various forms of inheritance The benefits and costs of inheritance

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

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

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 Note: Java interprets protected to mean accessible within same package

Inheritance in C++ Public inheritance E.g., Public base class members remain public in the derived class Protected base class members remain Protected in the derived class Private base class members remain private in the derived class class circle: public Shape { …… };

Inheritance in C++ (Cont.) Protected inheritance E.g., Public base class members become protected in the derived class Protected base class members remain protected in the derived class Private base class members are inaccessible in the derived class class circle: protected Shape { …… };

Inheritance in C++ (cont.) Public inheritance E.g., Public base class members become private in the derived class Protected base class members remain private in the derived class Private base class members are inaccessible in the derived class class circle: private Shape { …… };

Constructors in Inheritance (Java) Whenever a class definition does not have a constructor, Java automatically creates on with zero parameters (default constructor) However, once you add at least one constructor to a class, then no constructors are created automatically  Constructor methods are not inherited even though they are designated as "public" But, the subclass surly can use "super" to call superclass' constructors   If subclass' constructor doesn't call any superclass' constructor, Java will automatically call superclass' default constructor (with zero parameter) before executing the lines in the subclass' constructor If the superclass happens to have no default constructor (for example, when you add a constructor with at least one parameter and do not add one with zero parameters), you will meet a compilation error

Constructors in Inheritance (C++) When you instantiate a class object that has been derived from another class, a constructor for the base class is called first, followed by the derived class constructor, even if the base and derived class both have only default constructors If a base class does not contain a default constructor, then you must provide a constructor for every derived class, even if the derived class does not need a constructor for any other reason

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

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

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. An example of the latter from the case study in chapter 7, all graphical objects know how to draw.

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

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

Forms of Inheritance 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

Specialization Inheritance By far the most common form of inheritance is specialization Each child class overrides a method inherited from the parent in order to specialize the class in some way A good example is the Java hierarchy of graphical components in the Abstract Windowing Toolkit (AWT): Component Label Button TextComponent TextArea TextField CheckBox ScrollBar

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 Event Listeners ActionListener, MouseListener, and so on specify behavior, but must be subclassed

Example Public interface ActionListener { public void actionPerformed(ActionEvent e) {}; } public class Beeper ... implements ActionListener { ... //where initialization occurs: button.addActionListener(this); ... public void actionPerformed(ActionEvent e) Toolkit.getDefaultToolkit().beep(); }

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 Generally not a good idea, since it can break the principle of substitutability, but nevertheless sometimes found in practice (More often in dynamically typed languages, such as Smalltalk) An example might be subclassing the idea of a Set from an existing List class

Example public class Set … { public void clear() { … } { … } public boolean isEmpty() public void add(E e) public E remove(E e) …… public class List ... extends Set { ….. }

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 An example is Java Properties inheriting form Hashtable

Example public class Hashtable … { public int size() { … } public boolean isEmpty() public Enumeration keys() public Enumeration elements() …… public class Properties ... extends Hashtable { public Object setProperty (String key, String value) {…} public void load(InputStream inStream) throws IOException {…} public void save (OutputStream out, String header) {…} }

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 Generally not a good idea, since it breaks the idea of substitution. But again, it is sometimes found in practice 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

Example public class List … { public void insert (E element, Position p) { if ((!this.isEmpty()) && (p == front)) //insert the element in the front if ((!this.isEmpty()) && (p == end)) //insert the element in the end …… } public class Stack extends List { public void insert (E element) { if (!this.isEmpty()) //insert the element in the top …… }

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 Touchpad and Joystick Better solution, abstract out common parts to new parent class, and use subclassing for specialization

Summary of Forms of Inheritance Specialization The child class is a special case of the parent class; in other words, the child class is a subtype of the parent class. Specification The parent class defines behavior that is implemented in the child class but not in the parent class. Construction The child class makes use of the behavior provided by the parent class, but is not a subtype of the parent class. Generalization The child class modifies or overrides some of the methods of the parent class. Extension The child class adds new functionality to the parent class, but does not change any inherited behavior. Limitation The child class restricts the use of some of the behavior inherited from the parent class. Variance The child class and parent class are variants of each other, and the class-subclass relationship is arbitrary. Combination The child class inherits features from more than one parent class. This is multiple inheritance and will be the subject of a later chapter.

Benefits of Inheritance Software Reuse Code Sharing Consistency of Interface Software Components Rapid Prototyping Polymorphism Information Hiding

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

Chapter Summary In this chapter we have begun the exploration of inheritance, a topic we will continue through the next several chapters. Topics we have addressed have included the following: 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