Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.

Slides:



Advertisements
Similar presentations
Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
Inheritance Inheritance Reserved word protected Reserved word super
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Introduction to Inheritance Fall 2005 OOPD John Anthony.
1 Types Object Oriented Programming Spring 2007.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping and Inheritance.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
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. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Dynamic Binding Implementation Object-Oriented Programming Spring
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Cs2220: Engineering Software Class 13: Behavioral Subtyping 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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.
Sections Inheritance and Abstract Classes
Object-oriented Programming in Java
Inheritance and Polymorphism
Subtyping Rules David Evans cs205: engineering software BlackBear
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Extending Classes.
Building Java Programs
Data Abstraction David Evans cs205: engineering software
Announcements & Review
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
More About Inheritance & Interfaces
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Chapter 11 Inheritance and Polymorphism Part 2
Lecture 13: Subtyping Rules Killer Bear Climber
Class 16: Concurrency Rules
Subtype Substitution Principle
Reasoning about Data Abstractions
CIS 110: Introduction to computer programming
Presentation transcript:

cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans

Schedule Updates PS4 is now due on Monday, October 11 (October 12: Reading day) Start thinking about project ideas Once you have an idea for your project, can substitute parts of your project for programming parts of PS

Kinds of Abstraction Procedural Abstraction Abstraction hides details of computations One procedure abstracts many information processes Abstraction by Specification Abstraction hides how a computation is done One specification can be satisfied by many procedures Data Abstraction Abstraction hides how data is represented One datatype can be implemented many ways

Subtyping Filter BlurFilter BlurFilter is a subtype of Filter Filter is the supertype of BlurFilter BlurFilter  Filter Subtype Abstraction allows us to abstract many possible datatypes with their supertype.

Subtype Substitution If B is a subtype of A, everywhere the code expects an A, a B can be used instead. Filter f = new BlurFilter(); Filter f; BlurFilter bf;... f = bf; bf = f;

Applying a Filter Filter f = loadFilter(command); int idx = images.getSelectedIndex(); if (idx < 0) { reportError("An image must be selected to apply an effect."); return; } f.setImage(workingImages.get(idx), (String) imagesModel.get(idx)); Image result = f.apply(); if (result == null) { reportError("Error applying filter"); } else { addImage(result, f.getImageName() + "/" + f.getFilterName()); } from ps4/GUI.java // EFFECTS: Returns a Filter object // associated with the input name. private Filter loadFilter(String fname); // EFFECTS: Returns a Filter object // associated with the input name. private Filter loadFilter(String fname);

Supertype Specification public abstract class Filter { // OVERVIEW: A Filter represents an image and provides a technique for altering it. // A Filter may be in one of three states: uninitialized, initialized, // and applied. An initialized or applied filter has an associated image; // and a Pixels object that represents the pixel data (possibly modified // by the filter) in the image. public Filter() // EFFECTS: Initializes this to an uninitialized filter. final public void setImage(Image p_image, String p_name) // REQUIRES: this is uninitialized // MODIFIES: this // EFFECTS: Sets the image for this to p_image; sets this to the initialized state. public String getImageName() // EFFECTS: Returns the image name associated with the filter. public String getFilterName() // EFFECTS: Returns the name of the filter.... All subtypes must implement the supertype’s specification. But, they can provide different implementations.

Method Dispatch Assume B is a subtype of A If both A and B have a method filter which method should be called? A a = new A (); B b = new B (); a.filter(); b.filter(); a = b; a.filter() Calls class A’s filter method Calls class B’s filter method

Dynamic Dispatch Search for the method up the type hierarchy, starting from the actual (dynamic) type of the object A B A a = new A (); B b = new B (); a.filter(); b.filter(); a apparent type actual type b apparent type actual type

Dynamic Dispatch B A a = new A (); B b = new B (); a.display (); b.display (); a = b; a apparent type actual type b apparent type actual type Now: apparent type of a is A, actual type of a is B A

Apparent and Actual Types Apparent types are associated with declarations Never change Actual types are associated with objects Always a subtype of the apparent type Can change which subtype it is Compiler does type checking using apparent type JVM does method dispatch using actual type How can we change the actual type of a variable? How can we change the apparent type of an expression?

Downcasting Casting changes the apparent type. The VM must check at runtime that the actual type is a subtype of the cast type (if not, ClassCastException). Filter f = new Filter(); BlurFilter bf = new BlurFilter(); f = bf; bf = f; bf = (BlurFilter) f; bf = (AddFilter) f; Compiler type mismatch error ClassCastException

Implementing a Subtype public class BlurFilter extends Filter public String getFilterName() { return "blur"; }... } public class BlurFilter extends Filter public String getFilterName() { return "blur"; }... } public abstract class Filter {... public String getFilterName() { return "basic"; }... } public abstract class Filter {... public String getFilterName() { return "basic"; }... } Supertype Subtype

Dynamic Dispatch Filter f = loadFilter(command); int idx = images.getSelectedIndex(); if (idx < 0) { reportError("An image must be selected to apply an effect."); return; } f.setImage(workingImages.get(idx), (String) imagesModel.get(idx)); Image result = f.apply(); if (result == null) { reportError("Error applying filter"); } else { addImage(result, f.getImageName() + "/" + f.getFilterName()); } from ps4/GUI.java

Overriding Methods public abstract class Filter {... protected abstract void filter(); // REQUIRES: this must be initialized // MODIFIES: this // EFFECTS: alters the image in a manner specified by the filter.... } public class BlurFilter extends Filter protected void filter() // MODIFIES: this // EFFECTS: Blurs the pixels in this by averaging their colors // with those of the surrounding pixels. {... } public class BlurFilter extends Filter protected void filter() // MODIFIES: this // EFFECTS: Blurs the pixels in this by averaging their colors // with those of the surrounding pixels. {... } public class FlipFilter extends Filter protected void filter() // MODIFIES: this // EFFECTS: Replaces the pixels in image with their mirror image // (flips horizontally around center). public class FlipFilter extends Filter protected void filter() // MODIFIES: this // EFFECTS: Replaces the pixels in image with their mirror image // (flips horizontally around center). public abstract class MultiFilter extends Filter {... protected void addImage (Image p_image) ; } public abstract class MultiFilter extends Filter {... protected void addImage (Image p_image) ; } public class AddFilter extends MultiFilter protected void filter() // MODIFIES: this // EFFECTS: Replaces each pixel in the image with the // bitwise or of the corresponding pixels in all the images. public class AddFilter extends MultiFilter protected void filter() // MODIFIES: this // EFFECTS: Replaces each pixel in the image with the // bitwise or of the corresponding pixels in all the images.

Subtyping vs. Inheritance Inheritance Reusing the implementation of one type to build a new datatype Subtyping Defining a new type that can be used everywhere the supertype is expected These are very different notions, but often confused! It is possible to have inheritance without subtyping, and to have subtyping without inheritance.

Subtyping/Inheritance in Java extends: both subtyping and inheritance implements: just subtyping class B extends A {... } B is a subtype of A B inherits from A class C implements D {... } C is a subtype of D class B extends A {... } B is a subtype of A B inherits from A class C implements D {... } C is a subtype of D

Is it possible to get inheritance without subtyping? Not conveniently. But, this reuses most of B’s implementation without allowing A objects to be used where B is expected. public class A { // rep is a B private B rep; public A() { rep(); } public int method(int x) { return rep.method(x); }... // same for all B methods you want to “inherit” } public class A { // rep is a B private B rep; public A() { rep(); } public int method(int x) { return rep.method(x); }... // same for all B methods you want to “inherit” }

Java’s Type Hierarchy java.lang.Object java.lang.String java.lang.Object is the ultimate supertype of every object type. java.util.AbstractCollection java.util.AbstractList java.util.ArrayList

7 October 2003CS 201J Fall 2003 Java 3D Class Hierarchy Diagram RotationPathInterpolator PathInterpolator Interpolator Selector Node Leaf SceneGraphObject Not at all uncommon to have class hierarchies like this!

All Classes are Subtypes public class Graph {... } public class Graph {... } public class Graph extends Object {... } public class Graph extends Object {... } really means: public class java.lang.Object { public boolean equals(Object o) {... } public String toString() {... }... // 7 other methods } public class java.lang.Object { public boolean equals(Object o) {... } public String toString() {... }... // 7 other methods }

Why Subtyping is Scary Reasoning about correct code now requires thinking about all possible subtypes! Substitution Principle (Behavioral Subtyping): imposing limits on the possible specifications of subtypes to make this possible!

Charge Subtyping – Allow one type to be used where another type is expected Inheritance – Reuse implementation of the supertype to implement a subtype Thursday: – When is it safe to say B is a subtype of A? Now: project ideas!