Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans www.cs.virginia.edu/cs205 Quiz Friday: classes through.

Slides:



Advertisements
Similar presentations
Space and Shape. rectangle Shape ? trapezium.
Advertisements

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 A picture is.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
Object Oriented Programming
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Computer Science 209 Software Development Equality and Comparisons.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
5/17/2015 OO Design: Liskov Substitution Principle 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
(c) University of Washington11-1 CSC 143 Java More on Exceptions Reading: Ch. 15.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
Gruppe Grøn Effective Java Items: 4, 12, 20, 28 36, 44, 52, 60 1.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
My Polygon Book By: Mrs. Gage Square Rectangle Equalateral Triangle
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping and Inheritance.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Types in programming languages1 What are types, and why do we need them?
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Interfaces and Inner Classes
Abstract Classes and Interfaces. Let’s say we are working on a video game together… Our job is to independently write two different enemies for the game.
Today’s lecture Review of chapter 8 Go over examples.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.
Tutorial 2 Shane Paul. Contact Details Shane Paul Rm: 378 Ext: 82766
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
1 ָ נן oop uException-Handling Mechanism – similar to C++ l throw e signals the occurrence of an exception lThe statement try/catch allows the calling.
Lecture 4: Type Systems.
Interfaces Professor Evan Korth.
CS102 – Exceptions David Davenport Latest: May 2015
Lecture 8: SmallTalking about Objects
Subtyping Rules David Evans cs205: engineering software BlackBear
Lecture 9: Exceptions in Java CS201j: Engineering Software
CMSC 202 Generics.
Lecture 13: Subtyping Rules Killer Bear Climber
Subtype Substitution Principle
Presentation transcript:

cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through today, Ch 7 (except 7.9)

2 cs205: engineering software Subtyping Cell ConwayLifeCell ConwayLifeCell is a subtype of Cell Cell is a supertype of ConwayLifeCell ConwayLifeCell ≤ Cell In Java API documentation: java.lang.Object Cell ConwayLifeCell

3 cs205: engineering software Subtype Substitution If B is a subtype of A, everywhere the code expects an A, a B can be used instead Examples: Cell c = c1; c1 must be a subtype of Cell (note A is a subtype of A) Cell c = new ConwayLifeCell (); ConwayLifeCell c = new Cell ();

4 cs205: engineering software Java’s Type Hierarchy Object String Cell ConwayLifeCell Object is the ultimate supertype of every object type.

5 cs205: engineering software Java Exception Hierarchy Object Throwable Exception RuntimeException IndexOutOfBoundsException IOException FileNotFoundException throws Throwable Objects thrown must be subtypes of Throwable Subtypes of RuntimeException are unchecked

6 cs205: engineering software Inheritance To implement a subtype, it is often useful to use the implementation of its supertype This is also called “subclassing” class B extends A B is a subtype of A B inherits from A class C implements F C is a subtype of F both subtyping and inheritance just subtyping No way to get inheritance without subtyping in Java

7 cs205: engineering software A Type Hierarchy Shape Quadrangle Triangle Rectangle Parallelogram Rhombus Square Equilateral EquilateralTriangle What are the subtypes of Parallelogram? What are the supertypes of Square?

8 cs205: engineering software A Class Hierarchy Shape Quadrangle Triangle Rectangle Parallelogram Rhombus Square Equilateral EquilateralTriangle

9 cs205: engineering software Reusing Implementations Shapes should have a setColor method Change Shape, Quadrangle, Parallelogram, Triangle, Equilateral, EquilateralTriangle, Rhombus, Rectangle, Square, etc. Change Shape others inherit new attribute and method automatically

10 cs205: engineering software Add isEquilateral method class Shape { public bool isEquilateral () { return false; } } class Equilateral { public bool isEquilateral () { return true; } }

11 cs205: engineering software Is a Rhombus equilateral? Shape Quadrangle Parallelogram Rhombus Equilateral isEquilateral? isEquilateral () { return false; } isEquilateral () { return true; } Inheritance can be tricky!

12 cs205: engineering software Solutions Java –Allow multiple supertypes using interfaces, but only one implementation –Pro: Safe and Simple, Con: Limits Reuse C++ –Allow it, let programmers shoot themselves if they want Eiffel –Explicit renaming or hiding (error if not done)

13 cs205: engineering software Java’s Solution: Interfaces Define a type with no implementation Classes can implement many interfaces: class B extends A implements I1, I2, I3 { … } means B is a subtype of A, I1, I2, and I3 B inherits the implementation of A

14 cs205: engineering software Example Interface public interface Comparable { int compareTo (Object o) { // EFFECTS: Compares this object with the specified // object for order. Returns a negative integer, zero, // or a positive integer as this object is less than, // equal to, or greater than the specified object. }

15 cs205: engineering software Java’s Sorting Routines public class java.util.Arrays { public static void sort (Object[] a, int fromIndex, int toIndex) // REQUIRES: All elements in a between // fromIndex and toIndex must // implement the Comparable interface. // EFFECTS: Sorts the elements of a between // fromIndex and toIndex into ascending // order, according to the natural ordering of // its elements (defined by compareTo).

16 cs205: engineering software Charge Subtyping –Allow one type to be used where another type is expected Inheritance –Reuse implementation of the supertype to implement a subtype Friday, Monday: –When is it safe to say B is a subtype of A?