Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sealed Method inside an Abstract Class - 2D Game Kit

Discussion in 'Scripting' started by dart, Mar 7, 2019.

  1. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    I was reading the 2D Game Kit code and there's something that doesn't make sense for me (I'm not a C# Wizzard at all).

    The class SealedSMB inherits from StateMachineBehaviour (an internal Unity class), is declared as an abstract class and seals some methods. Here is the code

    Code (CSharp):
    1. public abstract class SealedSMB : StateMachineBehaviour
    2.     {
    3.         public sealed override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { }
    4.  
    5.         public sealed override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { }
    6.  
    7.         public sealed override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { }
    8.     }
    Then, the class SceneLinkedSMB inherits from SealedSMB and overrides the sealed methods. Isn't seal keyworkd used to prevent override? Do abstract classes have any exception to this rule?

    Here's the (relevant) code of SceneLinkedSMB class.

    Code (CSharp):
    1.     public class SceneLinkedSMB<TMonoBehaviour> : SealedSMB
    2.         where TMonoBehaviour : MonoBehaviour
    3.     {
    4.         protected TMonoBehaviour monoBehaviour;
    5.    
    6.         bool firstFrameHappened;
    7.         bool lastFrameHappened;
    8.  
    9.         ...
    10.  
    11.         public sealed override void OnStateEnter( Animator animator,  AnimatorStateInfo stateInfo,  int layerIndex,         AnimatorControllerPlayable controller)
    12.         {
    13.             firstFrameHappened = false;
    14.  
    15.             OnSLStateEnter(animator, stateInfo, layerIndex);
    16.             OnSLStateEnter(animator, stateInfo, layerIndex, controller);
    17.         }
    18.  
    19.         ...
    20.  
    21. }
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    The sealed keyword prevents sub types from overriding the method. The abstract method prevents the type from being instantiated. They're not mutually exclusive.

    As to why the author of the class decided to do it, I can only guess. I assume that the author doesn't want to allow subtypes to override the methods that only have 3 parameters, while allowing its generic subtype to override the method that has 4 parameters. It appears that the author is trying to wrap the unity methods and provide his own logic to supplement them.
     
  3. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    Thank you for your answer kru, but I what I don't understand is why the compiler doesn't give an error when compiling the SceneLinkedSMB class, since it is actually overrinding sealed methods from its base class, SealedSMB. Since the compiler is not giving any error, there's something more to it that I don't know.
    Thanks again
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The trick is that there's two separate methods with the same name but different argument lists, so SceneLinkedSMB is actually not overriding a sealed method. The fact that SealedSMB is abstract is just a red herring.
     
  5. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    You're absolutely correct, Antistone, I've completely missed that last parameter. Thanks