one slide summary inheritance and godel s proof
play

One-Slide Summary Inheritance and Godel's Proof Inheritance allows - PDF document

One-Slide Summary Inheritance and Godel's Proof Inheritance allows a subclass to share behavior (methods and instance variables) with a superclass. A class hierarchy shows how subclasses inherit from superclasses. Typically a single


  1. One-Slide Summary Inheritance and Godel's Proof • Inheritance allows a subclass to share behavior (methods and instance variables) with a superclass. • A class hierarchy shows how subclasses inherit from superclasses. Typically a single ultimate class, such as object , lies at the top of a class hierarchy. • A subclass can be used whenever a superclass is expected. This is called subtyping . • An axiomatic system provides a way to reason mechanically about formal notions. An incomplete system fails to prove some true statements. An inconsistent system proves some false statements. • Any interesting logical system is incomplete: there is a true statement that cannot be proved in it. #2 Outline • Inheritance • Subtyping • PS6 • Mechanical Reasoning • Axiomatic Systems Inheritance • Paradoxes • Gödel #3 #4 Overriding Inherited Behavior Hey, Scooby! public class Dog { public class Dog { public Dog(String n) { name = n; } public Dog(String n) { name = n; } public String name; public String name; public void bark() { println(“wuff wuff”); } } public void bark() { println(“wuff wuff”); } } public class TalkingDog extends Dog { public class TalkingDog extends Dog { public void speak(String words) { public void bark() { println(“wuff wuff, but I could speak!”); } println(name + “ says “ + words); public void speak(String words) { } } // inherits all Dog fields and methods println(name + “ says “ + words); } } Dog judy = new Dog(“Judy”); Dog judy = new Dog(“Judy”); judy.bark(); judy.bark(); wuff wuff wuff wuff judy.speak(“salve atque vale!”); TalkingDog scooby = new TalkingDog(“Scooby”); Type Error! scooby.bark(); TalkingDog scooby = new TalkingDog(“Scooby”); wuff wuff, but I could speak! scooby.bark(); scooby.speak(“solve the mystery!”); wuff wuff Scooby says solve the mystery! scooby.speak(“solve the mystery!”); Scooby says solve the mystery! #5 #6

  2. Dynamic (= Run-Time) Types Static (= Compile-Time) Types public class Dog { public class Dog { public Dog(String n) { name = n; } public Dog(String n) { name = n; } public String name; public String name; public void bark() { println(“wuff wuff”); } } public void bark() { println(“wuff wuff”); } } public class TalkingDog extends Dog { public class TalkingDog extends Dog { public void bark() { println(“wuff wuff, but I could speak!”); } public void bark() { println(“wuff wuff, but I could speak!”); } public void speak(String words) { public void speak(String words) { println(name + “ says “ + words); println(name + “ says “ + words); } } } } Dog judy = new Dog(“Judy”); Dog judy = new Dog(“Judy”); TalkingDog scooby = new TalkingDog(“Scooby”); TalkingDog scooby = new TalkingDog(“Scooby”); Dog myDog; // static type myDog = Dog Dog myDog; // static type myDog = Dog myDog = judy; // dynamic type MyDog = Dog myDog = judy; // dynamic type MyDog = Dog myDog.bark(); myDog.speak(“i am the very model”); wuff wuff Type Error: Dog has no method speak() myDog = scooby; // dynamic type myDog = TalkingDog myDog = scooby; // dynamic type myDog = TalkingDog myDog.bark(); myDog.speak(“of a modern major-general”); wuff wuff, but I could speak! Type Error: Dog has no method speak() #7 #8 Types and Objects Speaking About Inheritance • Whether or not you can even call a method • Inheritance is using the definition depends on the static type of the object! of one class to define another Dog – The static type is the declared type of the class. variable in the source code: • TalkingDog inherits from Dog. • Dog myDog = new TalkingDog(); // static type Dog • TalkingDog is a subclass of Dog. • myDog.speak(“hello”); // Type Error TalkingDog • The exact behavior of a method depends on • The superclass of TalkingDog is the dynamic type of the object! Dog. – The dynamic type is X if you wrote new X() : • These all mean the same thing! • Dog myDog = new TalkingDog(); // dynamic TalkingDog • myDog.bark(); // wuff wuff, but I could speak! #9 #10 Subtyping Subtyping Exercise • Subtyping: An object of a subclass can be public class Animal { ...getMass()... } public class Dog extends Animal { ..bark().. } used whenever an object of a superclass is public class TalkingDog extends Dog { ..speak().. } expected. void myMethod(Animal a, Dog d, TalkingDog t) { // Which are valid? – Intuition: The subclass will only ever have “more Animal zooPet = a; zooPet.getMass(); stuff”, so this is safe. Inheritance can only add zooPet = d; zooPet.getMass(); new behavior or change old behavior. zooPet = t; zooPet.getMass(); Dog myDog = a; myDog.bark(); – This is called the Barbara Liskov Substitution myDog = d; myDog.bark(); Principle. myDog = t; myDog.bark(); • “Dog myDog = new TalkingDog();” works TalkingDog cartoon = a; cartoon.speak(); because TalkingDog is a subclass of Dog! cartoon = d; cartoon.speak(); cartoon = t; cartoon.speak(); #11 #12

  3. Subtyping Exercise Problem Set 6 public class Animal { ...getMass()... } • Make an adventure public class Dog extends Animal { ..bark().. } game by programming public class TalkingDog extends Dog { ..speak().. } with objects. void myMethod(Animal a, Dog d, TalkingDog t) { // Which are valid? • Many objects in our Animal zooPet = a; zooPet.getMass(); zooPet = d; zooPet.getMass(); game have similar zooPet = t; zooPet.getMass(); properties and Dog myDog = a; myDog.bark(); behaviors, so we use myDog = d; myDog.bark(); inheritance. myDog = t; myDog.bark(); TalkingDog cartoon = a; cartoon.speak(); cartoon = d; cartoon.speak(); cartoon = t; cartoon.speak(); #13 #14 Object-Oriented Terminology PS6 Classes • An object is an entity that packages state and procedures. SimObject • A constructor is a procedure that creates new objects. PhysicalObject Place • The state variables that are part of an object are called instance variables . MobileObject • The procedures that are part of an object are called methods . We invoke (call) a method. • Inheritance allows one class to refine and OwnableObject Person reuse the behavior of another. • Subtyping allows a subclass to be used where Student PoliceOfficer a superclass is expected. #15 #16 Review-ish: Python Dictionaries Dictionary Example • The dictionary abstraction provides a lookup >>> d = {} # new empty dictionary table. >>> d['UVA'] = 1818 # make new entry • Each entry is a pair: >>> d['UVA'] = 1819 # update entry < key , value > >>> d['Cambridge'] = 1209 • The key must be an immutable object. The >>> d['UVA'] value can be anything. 1819 • dictionary [ key ] evaluates to the value >>> d['Oxford'] associated with key KeyError: 'Oxford' – Running time is approximately constant! – (e.g., “associative array” or “hash table”) #17 #18

  4. Pencil & Paper: Histograms Histogram Example • Define a procedure histogram that takes a def histogram(text): text string as input. The procedure returns a >>> d = d = {} dictionary in which each word in the input histogram(declaration) words = text.split() >>> show_dictionary(d) string is mapped to the number of times it for w in words: of: 79 occurs in that string. the: 76 if w in d: • Hints: to: 64 d[w] = d[w] + 1 – Iterate over each word, putting it in a dictionary. and: 55 If you haven't seen it before, its count is 1. else: ... Otherwise, increment its count. d[w] = 1 >>> 'here we go'.split() return d ['here', 'we', 'go'] #19 #20 Java HashMaps Author Fingerprinting • [...] a comparison of phrases used in The Reign of King • We can do the same thing in Java: Edward III with Shakespeare’s early works proves conclusively that the Bard wrote the play in collaboration with Thomas d = {} # new empty dictionary Kyd, one of the most popular playwrights of his day. [...] He discovered that playwrights often use the same patterns of d['UVA'] = 1818 # make new entry speech, meaning that they have a linguistic fingerprint. The d['UVA'] # get a value program identifies phrases of three words or more in an author’s known work and searches for them in unattributed 1819 plays. In tests where authors are known to be different, there are up to 20 matches because some phrases are in common usage. When Edward III was tested against Shakespeare’s HashMap<String, Integer> d = new HashMap<String, Integer>; works published before 1596 there were 200 matches. d.put(“UVA”, 1818); – Jack Malvern, “Computer program proves Shakespeare d.get(“UVA”); didn't work alone, researchers claim”, The Times of London , 12 Oct 2009 1819 #21 #22 Liberal Arts Trivia: Physics Liberal Arts Trivia: Chemistry • Name the vector quantity in physics measured • Give the common name for hydragyrum, a in radians per second. The direction of the heavy metal element. It is the only element vector is perpendicular to the plane of that is liquid at standard temperature and rotation and is usually specified by the “right pressure and is often used in the construction hand rule”. of sphygmomanometers. In the 18 th to 19 th centuries it was used to make felt hats, and the psychological symptoms associated with its poisoning are sometimes used to explain the phrase “mad as a hatter”. • Bonus: What does a sphygmomanometer measure? #23 #24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend