Introduction
Inheritance is a class-only feature in Swift that lets one class, the subclass, acquire the properties, methods, and other characteristics of another class, the superclass. This enables code reuse and the modeling of 'is-a' relationships, such as a Dog being a kind of Animal. Swift supports only single inheritance — a class can have at most one direct superclass — unlike some languages that permit multiple inheritance.
Cricket analogy: An all-rounder like Ravindra Jadeja inherits both a bowler's and batsman's traits from a shared cricketer 'superclass,' modeling an 'is-a' relationship, but Swift only allows one direct superclass, unlike mixing traits from two unrelated sports.
Syntax
class Animal {
var name: String
init(name: String) { self.name = name }
func makeSound() {
print("Some generic animal sound")
}
}
class Dog: Animal {
override func makeSound() {
print("\(name) says Woof!")
}
}Explanation
Dog: Animal declares Dog as a subclass of Animal, so Dog automatically inherits the name property and the makeSound() method. The override keyword is mandatory whenever a subclass provides its own implementation of a method, property, or subscript that already exists in the superclass; omitting override when you mean to override, or using it when there is nothing to override, is a compile-time error. Inside an overriding method, super.makeSound() can be called to invoke the superclass's original implementation before or after adding new behavior. The final keyword can be applied to a class, method, or property to prevent it from being subclassed or overridden any further.
Cricket analogy: 'FastBowler: Bowler' inherits the bowl() method automatically, but adding a signature yorker requires the mandatory 'override' keyword, and calling 'super.bowl()' first, like Jasprit Bumrah adding his variation atop the base action; 'final' would stop anyone subclassing FastBowler further.
Example
class Animal {
var name: String
init(name: String) { self.name = name }
func makeSound() { print("...") }
}
class Dog: Animal {
override func makeSound() {
super.makeSound()
print("\(name) says Woof!")
}
}
let d = Dog(name: "Rex")
d.makeSound()Output
...
Rex says Woof!Key Takeaways
- Only classes support inheritance in Swift; structs and enums do not.
- A class can have at most one direct superclass — single inheritance only.
- The override keyword is required when redefining an inherited method or property.
- super.method() invokes the superclass's implementation from within an override.
- final prevents a class from being subclassed, or a method/property from being overridden further.
- Subclasses inherit stored properties, computed properties, and methods from their superclass.
Practice what you learned
1. How many direct superclasses can a Swift class have?
2. What keyword is required when a subclass redefines an inherited method?
3. How do you call a superclass's implementation of a method from within an override?
4. What does the final keyword do when applied to a class?
5. Which Swift types support inheritance?
Was this page helpful?
You May Also Like
Classes in Swift
Classes are reference types in Swift that support inheritance and are managed automatically via ARC.
Structs vs Classes in Swift
Structs use value semantics while classes use reference semantics — the defining choice you make for every custom Swift type.
Protocols in Swift
Learn how Swift protocols define blueprints of methods and properties that structs, classes, and enums can conform to.
Protocol-Oriented Programming in Swift
Explore Swift's protocol-oriented design philosophy, which favors composing small protocols over deep class inheritance.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics