AngelScript

Inheritance and polymorphism

AngelScript supports single inheritance, where a derived class inherits the properties and methods of its base class. Multiple inheritance is not supported, but polymorphism is supprted by implementing interfaces.

All the class methods are virtual, so it is not necessary to specify this manually. When a derived class overrides an implementation, it can extend the original implementation by specifically calling the base class' method using the scope resolution operator. When implementing the constructor for a derived class the constructor for the base class is called using the `super` keyword. If none of the base class' constructors is manually called, the compiler will automatically insert a call to the default constructor in the beginning. The base class' destructor will always be called after the derived class' destructor, so there is no need to manually do this.

      // A derived class
      class MyDerived : MyClass
      {
        // The default constructor
        MyDerived()
        {
          // Calling the non-default constructor of the base class
          super(10);

          b = 0;
        }

        // Overloading a virtual method
        void DoSomething()
        {
          // Call the base class' implementation
          MyClass::DoSomething();

          // Do something more
          b = a;
        }

        int b;
      }

Todo:
  Show how the polymorphism is used with cast behaviours

## Extra control with final and override

A class can be marked as 'final' to prevent the inheritance of it. This is an optional feature and mostly used in larger projects where there are many classes and it may be difficult to manually control the correct use of all classes. It is also possible to mark individual class methods of a class as 'final', in which case it is still possible to inherit from the class, but the finalled method cannot be overridden.

      // A final class that cannot be inherited from
      final class MyFinal
      {
        MyFinal() {}
        void Method() {}
      }

      // A class with individual methods finalled
      class MyPartiallyFinal
      {
        // A final method that cannot be overridden
        void Method1() final {}

        // Normal method that can still be overridden by derived class
        void Method2() {}
      }

When deriving a class it is possible to tell the compiler that a method is meant to override a method in the inherited base class. When this is done and there is no matching method in the base class the compiler will emit an error, as it knows that something wasn't implemented quite the way it was meant. This is especially useful to catch errors in large projects where a base class might be modified, but the derived classes was forgotten.

      class MyBase
      {
        void Method() {}
        void Method(int) {}
      }

      class MyDerived : MyBase
      {
        void Method() override {}      // OK. The method is overriding a method in the base class
        void Method(float) override {} // Not OK. The method isn't overriding a method in base class
      }