subclassing and subtyping
play

Subclassing and Subtyping Dr. Mattox Beckman University of Illinois - PowerPoint PPT Presentation

Introduction Types and Objects Subclassing and Subtyping Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Introduction Types and Objects Objectives You should be able to ... The idea of a subtype


  1. Introduction Types and Objects Subclassing and Subtyping Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science

  2. Introduction Types and Objects Objectives You should be able to ... The idea of a subtype and a subclass are very closely related, but there is a subtle difference we would like to cover. ◮ Explain the difference between a subclass and a subtype. ◮ Explain the terms covariant and contravariant . ◮ Identify if two types have a subtyping relationship.

  3. An integer is a kind of fmoat, so we can say that integer is a subtype of fmoat. Introduction Types and Objects How do Types Relate? ◮ How can you tell if one type is a subtype of another? ◮ Are integers subtypes of fmoats? (Or vice-versa?) ◮ Characters / strings? ◮ Squares / shapes?

  4. Introduction Types and Objects How do Types Relate? ◮ How can you tell if one type is a subtype of another? ◮ Are integers subtypes of fmoats? (Or vice-versa?) ◮ Characters / strings? ◮ Squares / shapes? ◮ An integer is a kind of fmoat, so we can say that integer is a subtype of fmoat. Float Int

  5. Introduction Types and Objects Covariance ◮ Some types take parameters, such as lists and trees. ◮ If the subtype relationship varies according to the input type, the type is said to be covariant . ◮ “Most” types containing parameters are covariant. [Float] Float [Int] Int

  6. Introduction Types and Objects Functions ◮ Functions are an important exception! ◮ The function type is covariant with respect to the output. If we are expecting a function that outputs a fmoat, I can give you a function that outputs an integer without breaking anything. The reverse is not true! ◮ The function type is contravariant with respect to the input. If we are expecting a function that takes a fmoat, providing a function that takes an integer will fail or truncate the input. Int → Float Int → Int Float → Float Float → Int

  7. } public class A { } Introduction Types and Objects The Trouble with Objects ... Actually, there’s more than just this one! 1 2 public A foo( A x ) { ... } 3 public A bar() { /* calls foo ... */ } 4 5 public class B : A { 6 public B foo( B x ) { ... } 7 ◮ B.bar inherits from A . ◮ But B.foo overwrites A.foo . ◮ When A.bar calls B.foo , what will happen?

  8. Introduction Types and Objects Conclusions ◮ Objects have a lot of fmexibility and allow us to create useful abstractions. ◮ They can be implemented using functions. Users of functional programming languages tend to avoid them. ◮ These are useful enough in practice, and diffjcult enough to implement, that most modern languages now include them, including OCaml. (That’s where the O comes from.) ◮ Inheritance can be tricky.

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