COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
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.
Advertisements

Object Oriented Programming
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Computer Science 340 Software Design & Testing Inheritance & Design By Contract.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Issues.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
CS 211 Inheritance AAA.
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
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Liskov Substitution Principle
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
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.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Inheritance using Java
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Inheritance and Access Control CS 162 (Summer 2009)
(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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Five design principles
Subtype Polymorphism, Subtyping vs
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,
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Overview of C++ Polymorphism
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.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Testing Object-Oriented Software Concepts and Definitions
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Advanced Programming in Java
Object Oriented Programming in Python
Class Inheritance (Cont.)
Week 6 Object-Oriented Programming (2): Polymorphism
COMPUTER 2430 Object Oriented Programming and Data Structures I
Subtype Polymorphism, Subtyping vs
Object-Oriented Programming
METHOD OVERRIDING in JAVA
Software Design Lecture : 12.
Overriding Methods & Class Hierarchies
Overview of C++ Polymorphism
Chapter 8 Inheritance Part 2.
1.
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Inheritance and Polymorphism Prog 6

Liskov Substitution Principle (LSP) In object-oriented programming, the Liskov substitution principle is a particular definition of subtype that was introduced by Barbara Liskov in a 1987 conference keynote address entitled Data abstraction and hierarchy Liskov's notion of "subtype" is based on the notion of substitutability

Liskov Substitution Principle (LSP) The Liskov substitution principle is closely related to the design by contract methodology, leading to some restrictions on how contracts can interact with inheritance: Preconditions cannot be strengthened in a subclass. Require no more! Postconditions cannot be weakened in a subclass. Promise no less! A function using a class hierarchy violating the principle uses a reference to a base class, yet must have knowledge of the subclasses. Such a function violates the open/closed principle because it must be modified whenever a new derivative of the base class is created.

Liskov Substitution Principle (LSP) Two paraphrases that capture the essence A child can be used wherever a parent is expected For every overridden method in a child class: Require no more Promise no less It will be on Test 3 and the final

public class Animal { private String _id; private int numLegs; private float weight; public void grow() . . . } public class Frog extends Animal @Override // weight determines numLegs

public class AnimalList { private Animal[] list; private int count; public boolean add( Animal animal ) . . . } // Can FrogList be a sub-class of AnimalList? public class FrogList extends AnimalList // Can a fish be added to the list? @Override

public class AnimalList { private Animal[] list; private int count; // Any animal can be added to the lis. public boolean add( Animal animal ) . . . } public class FrogList extends AnimalList // Require More! @Override public boolean add( Frog frog )

public class AnimalList { private Animal[] list; private int count; // animal will be added to the list if not full public boolean add( Animal animal ) . . . } public class FrogList extends AnimalList // Promise Less! @Override // do not add if not Frog

public class BagOfFruit { private Fruit items[];   public void add ( Fruit x ) ... } public class BagOfApple extends BagOfFruit private Apple items[]; // How to override method add? Is a BagOfApple a BagOfFruit? Should BagOfApple be a subclass of BagOfFruit? NO!

Don't Overdo Inheritance Is your inheritance good ? the notion of "is-a“ LSP Circle can't inherit from Ellipse even though it "is-a” have to restrict overridden 2D "resize" BagOfApples is not a BagOfFruit restrict overridden add or allow bananas in the apple bag

Quiz 6