CSE331 SU Final Simple Summary

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Identity and Equality Based on material by Michael Ernst, University of Washington.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Exceptions and Assertions (Slides by Mike Ernst and David Notkin) 1.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Exceptions Handling Exceptionally Sticky Problems.
Exceptions and assertions CSE 331 University of Washington.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
(c) University of Washington13-1 CSC 143 Java Specifications: Programming by Contract Reading: Ch. 5.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
Polymorphism (generics) CSE 331 University of Washington.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION GENERICS/PARAMETRIC POLYMORPHISM Autumn 2011 We We Abstraction.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 10 Assertions & Exceptions.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
James Wilcox / Winter 2016 CSE 331 Software Design and Implementation Lecture 14 Generics 2.
Introduction to Exceptions in Java CS201, SW Development Methods.
Summary prepared by Kirk Scott
Generics, Exceptions and Undo Command
Handling Exceptionally Sticky Problems
Generics and Subtyping
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CSE 331 Software Design and Implementation
Week 4 Object-Oriented Programming (1): Inheritance
Type Abstraction SWE Spring 2009.
Parametric Polymorphism and Java Generics
SWE 332 Last Modified Spring 2010 Paul Ammann
Chapter 19 Generics Dr. Clincy - Lecture.
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Generics (Parametric Polymorphism)
Java Programming Language
CSE 331 Software Design and Implementation
Section 6: Midterm Review
CSE 331 Software Design and Implementation
CSE 331 Software Design & Implementation
Type Abstraction Liskov, Chapter 7.
Subtype Polymorphism, Subtyping vs
Generic programming in Java
Effective Java, 3rd Edition Chapter 10: Exceptions
Reasoning About ADTs, Assertions and Exceptions
Exceptions.
CSE 331 Software Design and Implementation
CMSC 202 Generics.
Chapter 11 Inheritance and Polymorphism Part 2
Handling Exceptionally Sticky Problems
Effective Java, Chapter 9: Exceptions
Exceptions 10-May-19.
Lecture 13: Subtyping Rules Killer Bear Climber
Type Abstraction SWE Spring 2013.
Exception Handling.
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

CSE331 SU Final Simple Summary Matt XU

Subtyping Stuffs

Subtypes are substitutable Subtypes are substitutable for supertypes Instances of subtype won't surprise client by failing to satisfy the supertype's specification Instances of subtype won't surprise client by having more expectations than the supertype's specification This follows the “Principle of Least Surprise” We say that B is a true subtype of A if B has a stronger specification than A This is not the same as a Java subtype Java subtypes that are not true subtypes are confusing and dangerous But unfortunately common poor-design 

Subtyping vs. subclassing Substitution (subtype) — a specification notion B is a subtype of A iff an object of B can masquerade as an object of A in any context About satisfiability (behavior of a B is a subset of A’s spec) Inheritance (subclass) — an implementation notion Factor out repeated code To create a new class, write only the differences Java purposely merges these notions for classes: Every subclass is a Java subtype But not necessarily a true subtype Java lets you make a subclass that is not a true subtype If you try to do this, you will end up with a vey interesting contradiction in your spec... stay tuned It’s up to the programmer to ensure that subclasses are proper subtypes Java isn’t smart enough to do this for you (Java would allow you to make a superclass Cat and subclass Animal) you know this is wrong some cases are subtler

Cheat Sheet B is a true subtype of A. How do I code this up? Use java subclassing! (B extends A) B is not a true subtype of A, but shares a lot with A. How do I code this up? It's tempting to use java subclassing when B is not a true subtype of A (Square/Rectangle) avoid it, since you might run into issues like the square/rectangle issue But I don't want to duplicate all the code in A. Duplication is evil. you're right! try Composition. (B has a A) B is a true subtype of A, but has an entirely different implementation. I don't want to inherit anything, but Java needs to know they're the same type for polymorphism to work. How do I code this up? A and B should implement the same interface.

Cheat Sheet B is a true subtype of A, but A is an existing class that I can't modify and it's not subclass-ready (Hashtable/InstrumentedHashTable) Composition will be helpful here too! (B has a A) And, if possible, have B implement the same interface as A, for polymorphism. D is a true subtype of A and of T. Java only has single inheritance. How do I code up this relationship? Use interfaces. D can implement interface A and interface T. Or extend one as a class and implement the other as an interface.

Assertions & Exceptions

What to do when something goes wrong Fail early, fail friendly Goal 1: Give information about the problem To the programmer – a good error message is key! To the client code: via exception or return-value or … Goal 2: Prevent harm Abort: inform a human Perform cleanup actions, log the error, etc. Re-try: Problem might be transient Skip a subcomputation: Permit rest of program to continue Fix the problem? Usually infeasible to repair from an unexpected state Ask: What are some things that could go wrong in your program? Raise hand if you have an example of what could go wrong. Also share ideas about which strateg(ies) you might use to prevent harm, or if you’re not sure, that’s fine too. Some possible answers: Abort: File not found Retry: network error Skip: Junit tests: one test in the suite fails Fix: I don’t know

Avoiding errors A precondition prohibits misuse of your code Adding a precondition weakens the spec This ducks the problem of errors-will-happen Mistakes in your own code Misuse of your code by others Removing a precondition requires specifying more behavior Often a good thing, but there are tradeoffs Strengthens the spec Example: specify that an exception is thrown

Recall your experience Sometimes recalling what you have encountered this quarter and how you solved those program failures can help you strengthen your perception of these things.

Java’s checked/unchecked distinction Checked exceptions (style: for special cases) Callee: Must declare in signature (else type error) Client: Must either catch or declare (else type error) Even if you can prove it will never happen at run time, the type system does not “believe you” There is guaranteed to be a dynamically enclosing catch Unchecked exceptions (style: for never-expected) Library: No need to declare Client: No need to catch Subclasses of RuntimeException and Error Throwable Runtime Exception Error Exception Checked exceptions - TODO: slide illustrating use of unchecked exception in case where precondition is violated

Why catch exceptions locally? Failure to catch exceptions usually violates modularity Call chain: A  IntegerSet.insert  IntegerList.insert IntegerList.insert throws some exception Implementer of IntegerSet.insert knows how list is being used Implementer of A may not even know that IntegerList exists Method on the stack may think that it is handling an exception raised by a different call Better alternative: catch it and throw again “chaining” or “translation” Restate in a level of abstraction that the client can understand Do this even if the exception is better handled up a level Makes it clear to reader of code that it was not an omission

Generics

List<Number> and List<Integer> ? List<Integer> List<Number> ? interface List<T> { boolean add(T elt); T get(int index); } So type List<Number> has: boolean add(Number elt); Number get(int index); So type List<Integer> has: boolean add(Integer elt); Integer get(int index); Java subtyping is invariant with respect to generics Neither List<Number> nor List<Integer> subtype of other Not covariant and not contravariant - Subtype needs stronger spec than super - Stronger method spec has: - weaker precondition - stronger postcondition Pair-share - Discuss this with neighbors - Share thoughts/questions with group - relationship of List<T1> to List<T2> not covariant with relationship of T1 to T2 - will be illuminated in the next few slides.

Generic types and subtyping List<Integer> and List<Number> are not subtype-related Generic types can have subtyping relationships Example: If HeftyBag extends Bag, then HeftyBag<Integer> is a subtype of Bag<Integer> HeftyBag<Number> is a subtype of Bag<Number> HeftyBag<String> is a subtype of Bag<String> … - Subtyping relationships with generics are based on the base type, not the generic parameter

Reasoning about wildcard types Object o; Number n; Integer i; PositiveInteger p; List<? extends Integer> lei; First, which of these is legal? lei = new ArrayList<Object>(); lei = new ArrayList<Number>(); lei = new ArrayList<Integer>(); lei = new ArrayList<PositiveInteger>(); lei = new ArrayList<NegativeInteger>(); Which of these is legal? lei.add(o); lei.add(n); lei.add(i); lei.add(p); lei.add(null); o = lei.get(0); n = lei.get(0); i = lei.get(0); p = lei.get(0);

Reasoning about wildcard types Object o; Number n; Integer i; PositiveInteger p; List<? super Integer> lsi; First, which of these is legal? lsi = new ArrayList<Object>; lsi = new ArrayList<Number>; lsi = new ArrayList<Integer>; lsi = new ArrayList<PositiveInteger>; lsi = new ArrayList<NegativeInteger>; Which of these is legal? lsi.add(o); lsi.add(n); lsi.add(i); lsi.add(p); lsi.add(null); o = lsi.get(0); n = lsi.get(0); i = lsi.get(0); p = lsi.get(0);

Type erasure All generic types become type Object once compiled Big reason: backward compatibility with ancient byte code So, at run-time, all generic instantiations have the same type List<String> lst1 = new ArrayList<String>(); List<Integer> lst2 = new ArrayList<Integer>(); lst1.getClass() == lst2.getClass() // true Cannot use instanceof to discover a type parameter Collection<String> cs = new ArrayList<String>(); if (cs instanceof Collection<String>) { // illegal ... }

Assertions & Exceptions

Don’t hide errors Be systematic Recall your stories

Callbacks

The callback design pattern Going farther: use a callback to invert the dependency TimeToStretch creates a Timer, and passes in a reference to itself so the Timer can call it back This is a callback – a method call from a module to a client that it notifies about some condition The callback inverts a dependency Inverted dependency: TimeToStretch depends on Timer (not vice versa) Less obvious coding style, but more “natural” dependency Side benefit: Main does not depend on Timer ***Read the slides for observers

Design Patterns

Look at our section slides and handouts.

Also for the System Development stuffs

That’s it