Swift - Custom Type Class Inheritance


To create a class that inherits from another, use : to mark the parent class.

class Car  : Vehicle { 
    var engineType = "V8" 


Classes that inherit from other classes can override functions in their parent class.

You can create subclasses that inherit most of the parent's functionality, but can specialize in certain areas.

For example, the Car class contains an engineType property.

Only Car instances will have this property.

To override a function, you redeclare it in your subclass and add the override keyword.

In an overridden function, to call back to the parent class's version of that function, use super keyword.

// Inherited classes can override functions 
override func description() -> String  { 
    let description = super.description() 
    return description + ", which is a car" 

Related Topics