Inheritance

Every class except Object, the hierarchy root, inherits from another class (its superclass). If you don’t specify one it defaults to Reference for classes and Struct for structs.

A class inherits all instance variables and all instance and class methods of a superclass, including its constructors (new and initialize).

  1. class Person
  2. def initialize(@name : String)
  3. end
  4. def greet
  5. puts "Hi, I'm #{@name}"
  6. end
  7. end
  8. class Employee < Person
  9. end
  10. employee = Employee.new "John"
  11. employee.greet # "Hi, I'm John"

If a class defines a new or initialize then its superclass constructors are not inherited:

  1. class Person
  2. def initialize(@name : String)
  3. end
  4. end
  5. class Employee < Person
  6. def initialize(@name : String, @company_name : String)
  7. end
  8. end
  9. Employee.new "John", "Acme" # OK
  10. Employee.new "Peter" # Error: wrong number of arguments for 'Employee:Class#new' (1 for 2)

You can override methods in a derived class:

  1. class Person
  2. def greet(msg)
  3. puts "Hi, #{msg}"
  4. end
  5. end
  6. class Employee < Person
  7. def greet(msg)
  8. puts "Hello, #{msg}"
  9. end
  10. end
  11. p = Person.new
  12. p.greet "everyone" # "Hi, everyone"
  13. e = Employee.new
  14. e.greet "everyone" # "Hello, everyone"

Instead of overriding you can define specialized methods by using type restrictions:

  1. class Person
  2. def greet(msg)
  3. puts "Hi, #{msg}"
  4. end
  5. end
  6. class Employee < Person
  7. def greet(msg : Int32)
  8. puts "Hi, this is a number: #{msg}"
  9. end
  10. end
  11. e = Employee.new
  12. e.greet "everyone" # "Hi, everyone"
  13. e.greet 1 # "Hi, this is a number: 1"

super

You can invoke a superclass’ method using super:

  1. class Person
  2. def greet(msg)
  3. puts "Hello, #{msg}"
  4. end
  5. end
  6. class Employee < Person
  7. def greet(msg)
  8. super # Same as: super(msg)
  9. super("another message")
  10. end
  11. end

Without arguments or parentheses, super receives all of the method’s parameters as arguments. Otherwise, it receives the arguments you pass to it.

Covariance and Contravariance

One place inheritance can get a little tricky is with arrays. We have to be careful when declaring an array of objects where inheritance is used. For example, consider the following

  1. class Foo
  2. end
  3. class Bar < Foo
  4. end
  5. foo_arr = [Bar.new] of Foo # => [#<Bar:0x10215bfe0>] : Array(Foo)
  6. bar_arr = [Bar.new] # => [#<Bar:0x10215bfd0>] : Array(Bar)
  7. bar_arr2 = [Foo.new] of Bar # compiler error

A Foo array can hold both Foo’s and Bar’s, but an array of Bar can only hold Bar and its subclasses.

One place this might trip you up is when automatic casting comes into play. For example, the following won’t work:

  1. class Foo
  2. end
  3. class Bar < Foo
  4. end
  5. class Test
  6. @arr : Array(Foo)
  7. def initialize
  8. @arr = [Bar.new]
  9. end
  10. end

we’ve declared @arr as type Array(Foo) so we may be tempted to think that we can start putting Bars in there. Not quite. In the initialize, the type of the [Bar.new] expression is Array(Bar), period. And Array(Bar) is not assignable to an Array(Foo) instance var.

What’s the right way to do this? Change the expression so that it is of the right type: Array(Foo) (see example above).

  1. class Foo
  2. end
  3. class Bar < Foo
  4. end
  5. class Test
  6. @arr : Array(Foo)
  7. def initialize
  8. @arr = [Bar.new] of Foo
  9. end
  10. end

This is just one type (Array) and one operation (assignment), the logic of the above will be applied differently for other types and assignments, in general Covariance and Contravariance is not fully supported.