Subclassing and Subtyping

Subclassing and Subtyping

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 and a subclass are very closely related, but there is a subtle difference we would like to cover. I Explain the difference between a subclass and a subtype. I Explain the terms covariant and contravariant. I Identify if two types have a subtyping relationship. I An integer is a kind of float, so we can say that integer is a subtype of float. Introduction Types and Objects How do Types Relate? I How can you tell if one type is a subtype of another? I Are integers subtypes of floats? (Or vice-versa?) I Characters / strings? I Squares / shapes? Introduction Types and Objects How do Types Relate? I How can you tell if one type is a subtype of another? I Are integers subtypes of floats? (Or vice-versa?) I Characters / strings? I Squares / shapes? I An integer is a kind of float, so we can say that integer is a subtype of float. Float Int Introduction Types and Objects Covariance I Some types take parameters, such as lists and trees. I If the subtype relationship varies according to the input type, the type is said to be covariant. I “Most” types containing parameters are covariant. Float [Float] Int [Int] Introduction Types and Objects Functions I Functions are an important exception! I The function type is covariant with respect to the output. If we are expecting a function that outputs a float, I can give you a function that outputs an integer without breaking anything. The reverse is not true! I The function type is contravariant with respect to the input. If we are expecting a function that takes a float, providing a function that takes an integer will fail or truncate the input. Int ! Float Int ! Int Float ! Float Float ! Int Introduction Types and Objects The Trouble with Objects ... Actually, there’s more than just this one! 1 public class A { 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 } I B.bar inherits from A. I But B.foo overwrites A.foo. I When A.bar calls B.foo, what will happen? Introduction Types and Objects Conclusions I Objects have a lot of flexibility and allow us to create useful abstractions. I They can be implemented using functions. Users of functional programming languages tend to avoid them. I These are useful enough in practice, and difficult enough to implement, that most modern languages now include them, including OCaml. (That’s where the O comes from.) I Inheritance can be tricky..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us