Swift has no abstract classes and methods. Instead, it offers protocols.
That's fine when your classes are either fully abstract or fully concrete.
But what is the best 'Swift' way to implement an abstract class that also has concrete methods?
Pseudo-code example:
class Animal {
abstract makeSound()
abstract eyeCount()
}
class Mammal : Animal {
override eyeCount { return 2 } // Let's assume all mammals have hard-coded 2 eyes...
class Cat : Mammal {
override makeSound { print "Meow!" }
}
class Dog : Mammal {
override makeSound { print "Woof!" }
}
In Mammal, I do want to implement the concrete method eyeCount()
because all mammals have 2 hard-coded eyes (supposedly) and I don't want to re-implement it in dog and cat. However, makeSound()
should only be implemented for Dog and Cat as mammals have varying voices.
How would you implement this in Swift? Thanks!