title: In Swift, how do I prevent a function from being called on a subclass?
body: <p>I have a base class that stores a lot of other data objects within some data structures, and this class manages those data objects in my collections through a group of <code>add</code>/<code>remove</code> functions that keep my data structures in sync.</p>
  <p>Now I go to subclass this base class, and the subclass is just like the base class except that it has special rules for what kinds of data objects it will accept (it looks at the values on the data objects, and rejects them if the values aren't right). Because of this &quot;validity&quot; check, I created a new function just for the subclass called <code>change</code>. In the <code>change</code> function, it examines all of the incoming data objects and verifies that they are ok, and replaces all of the data objects in the data structures with these data objects.</p>
  <p>The problem is that I don't want someone to be able to make a subclass object and allow them to call the base class <code>add</code>/<code>remove</code> functions, because I only want the subclass to be able to be changed through the subclass's <code>change</code> function.</p>
  <p>I haven't found a good way to &quot;disable&quot; the use of those base class functions in the subclass. I can <code>override</code> the functions and implement empty implementations, but there's no feedback to the caller that the function didn't do anything. And if I use something like <code>fatalError</code>, it isn't compile time, it's runtime.</p>
  <p>My other thoughts are to break the functionality of the base class into multiple protocols, and change the base class to simply have all of the data structures, but conforming to none of the protocols and then have multiple subclasses, where one that wants the <code>add</code> functionality can inherit from the base and additionally conform to the <code>add</code> protocol, but one that doesn't want <code>add</code> or <code>remove</code> can inherit from the base, and simply not conform to any of the protocols and instead create their own <code>change</code> functions to modify the data structures.</p>
  <p>Here's a simpler hierarchy to explain:</p>
  <pre><code>class Parent {
    
    func doThis() { print(&quot;Parent Did This&quot;) }
    func doThat() { print(&quot;Parent Did That&quot;) }
    
  }
  
  class Child: Parent {
    
    override func doThis() {
      print(&quot;Child Did This&quot;)
    }
    
    // I want this to be a compile time error
    override func doThat() {
      print(&quot;Not Supported&quot;)
      return
    }
    
  }
  </code></pre>
  <p>Is there an easier way to &quot;hide&quot; a function in a subclass?</p>
  <h1>EDIT 1</h1>
  <p>To better explain my proposed solution, and whether or not there is an easier way to achieve it with my current hierarchy, here's what the hierarchy would have to look like using some protocols:</p>
  <pre><code>protocol CanDoThis {
    func doThis()
  }
  
  protocol CanDoThat {
    func doThat()
  }
  
  class Parent {
    // Important properties that all children should have
    var property1: Int = 0
    var property2: String = &quot;&quot;
  }
  
  class Child1: Parent, CanDoThis {
    func doThis() { print(&quot;Child1 Can Do This&quot;) }
  }
  
  class Child2: Parent, CanDoThat {
    func doThat() { print(&quot;Child2 Can Do That&quot;) }
  }
  
  class Child3: Parent, CanDoThis, CanDoThat {
    func doThis() { print(&quot;Child3 Can Do This&quot;) }
    func doThat() { print(&quot;Child3 Can Do That&quot;) }
  }
  </code></pre>