3

I have two class A , B. B is the subclass of A. Can I use final for class B. I want to reduce the dynamic dispatch. What if there is method in class b that is overriding the class A method . How method dispatching will work?

class A {
     ///
}

final class B : Class A {

}

1 Answers1

1

Yes, You can

Swift gives us a final keyword just for this purpose: when you declare a class as being final, no other class can inherit from it. This means they can’t override your methods in order to change your behavior – they need to use your class the way it was written.

The final keyword is a restriction on a class, method, or property that indicates that the declaration cannot be overridden. This allows the compiler to safely elide dynamic dispatch indirection.

So you can use it.

Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26