1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
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.
Generic programming in Java
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 10 Classes Continued
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.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
OO as a language for acm l OO phrase l Mental model of key concepts.
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Types in programming languages1 What are types, and why do we need them?
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
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.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
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.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
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.
CSCE 240 – Intro to Software Engineering Lecture 3.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Object-Oriented Concepts
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Component Based Software Engineering
Inheritance and Polymorphism
Type Abstraction SWE Spring 2009.
Object Oriented Programming
Extending Classes.
Week 6 Object-Oriented Programming (2): Polymorphism
Type Abstraction Liskov, Chapter 7.
Generic programming in Java
Java Programming Language
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
Type Abstraction SWE Spring 2013.
Presentation transcript:

1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware programs are (often) abstractions of some real process (e.g., accounting system)

Abstraction  Forget some things i.e. remove some details  Combine some things i.e. many-to-one mapping 2

Forget some things 3 hiring date

Many-to-One Mapping 4 bonus hiring date

Abstraction by Parameterization  A basic programming technique but an important concept in software engineering  Example h*h+w*w replaced with squares(h, w); 5

Abstraction by Specification  Abstract from implementation details to behaviour client can depend on Precondition:  Something assumed to be true on entry to the method Postcondition:  Something that is supposed to be true at the completion of the method 6

Specification Example float sqrt(float coef) { //PRECONDITION: coef > 0 //EFFECTS: Returns approximate square root of coef float ans = coef/2.0; int i= 1; while (i<7) { ans = ans – ((ans*ans-coef)/(2.0*ans)); i = i + 1; } return ans;} 7

Background Type Inheritance Subtype Supertype Polymorphism and Dynamic Dispatch Boolean logic – and, or, not, boolean variables (x,y,z) Sets – infinite sets, subset

Variables Definition: A name bound to an object – Allow programmers to manipulate objects – They are like a “box” where you can store objects Variables are not objects! new statements are not objects! Objects do not appear in the source code! while(x > 0) { myString = new String(“string” + x); println(myString); x--; }

Types as Sets We can understand certain aspects of subtyping by considering types as sets A type can be considered as a set consisting of all possible objects of that type In this way, a subtype is always a subset of a supertype We say a subtype is narrower and more specific than a supertype We say a supertype is wider and less specific

Declared Type and Runtime Type Each variable has a declared type ex. String str; – The declared type is like a label on the side of a box saying what could be inside the box Each object has a runtime type – The runtime type is the actual type of the object – The runtime type of an object never changes – The runtime type is the type of the new statement which created the object – Dynamic dispatch: Methods to be invoked are determined by runtime type of target

Reasoning using Declared Types You always know the declared type of a variable You don’t always know the runtime type of an object bound to a variable Allows you to write methods which abstract from the details of all possible subtypes Subtypes of List: – AbstractList, ArrayList, LinkedList, Vector AbstractListArrayListLinkedListVector void reverseList(List list) {... }

Substitution Principle Subtypes provide different behavior when they override supertype methods However! Subtypes must not break the interface specified by a supertype/(pre n postcon) – So programmers can write code and reason just using the supertype specification

Substitution Principle  Signature Rule /done by java  Methods Rule /pre n postcon  Properties Rule / object states overtime

Signature Rule Require that methods overridden by a subtype are compatible with the supertype method Compatibility depends on the language you are using Let’s consider Java – Arguments of overridden methods must have the same type – The return type of an override can be a subtype of the return type of the overridden method

Signature Rule Example class Object {... public Object clone() {... } } class String {... public String clone() {... } }

Methods Rule A subtype can weaken the precondition – A subtype can require less A subtype can strengthen the postcondition – A subtype can guarantee more Corresponds to the logical operators OR and AND ex. (x AND y) is a stronger statement than just x (x OR y) is a weaker statement than just x

Methods Rule Example String toUpper(String str); //PRE: x != null //POST: returns an upper case version of x A subtype could provide this specification for toUpper //PRE: no preconditions //POST: if x != null, return an upper case version of x and if x == null, return null

Properties Rule  So far we have only considered methods  Sometimes the state of objects needs to follow an invariant  Invariant: A fact which is always true about the state of an object  Object state: A fact which can be concluded based on the current values of the object’s fields

Properties Rule  Example: Set invariant: Sets cannot contain duplicate elements We should be able to conclude that no subtypes of Set will contain duplicate elements Notice that the invariant has consequences on the implementation of several methods We say the invariant crosscuts the methods

Properties Rule  Invariants must hold after execution of the constructor  Invariants must hold after the execution of each method  An invariant may be violated during the execution of a method, so long as the object is not executable by another thread  Example: on the board

Summary  Type hierarchies provide extensibility Code can be written in terms of a supertype, yet still work many all subtypes But only when the subtypes follow the substitution principle