A base class is a class that other classes can inherit from. It provides common properties and methods that subclasses can use or override.
Click to reveal answer
beginner
What is a subclass in Swift?
A subclass is a class that inherits from a base class. It can use or change the properties and methods of the base class and add new ones.
Click to reveal answer
beginner
How do you declare a subclass in Swift?
You declare a subclass by writing the subclass name followed by a colon and the base class name. For example: <code>class Dog: Animal { }</code>
Click to reveal answer
intermediate
What does the override keyword do in Swift subclasses?
The <code>override</code> keyword tells Swift that you want to replace a method or property from the base class with your own version in the subclass.
Click to reveal answer
beginner
Can a subclass access properties of its base class?
Yes, a subclass can access and use the properties and methods of its base class unless they are marked as private.
Click to reveal answer
How do you declare a subclass named Car that inherits from a base class Vehicle in Swift?
Aclass Car: Vehicle { }
Bclass Car inherits Vehicle { }
Cclass Car extends Vehicle { }
Dclass Car < Vehicle { }
✗ Incorrect
In Swift, you declare a subclass by writing the subclass name followed by a colon and the base class name.
What keyword must you use to replace a base class method in a subclass?
Aoverride
Breplace
Csuper
Dextend
✗ Incorrect
The override keyword is used to replace a method or property from the base class in the subclass.
If a property in the base class is marked private, can the subclass access it?
AYes, always
BOnly if subclass is in the same file
CNo, never
DOnly if marked <code>override</code>
✗ Incorrect
Private properties are not accessible by subclasses.
Which of these is true about subclasses?
AThey cannot add new properties
BThey can only use base class methods without changes
CThey must have the same name as the base class
DThey can add new properties and override methods
✗ Incorrect
Subclasses can add new properties and override existing methods from the base class.
What is the main benefit of using a base class and subclass?
ATo write the same code multiple times
BTo organize code and reuse common features
CTo make code slower
DTo prevent any changes in code
✗ Incorrect
Base classes and subclasses help organize code and reuse common properties and methods.
Explain what a base class and a subclass are in Swift and how they relate to each other.
Think about a parent and child relationship in real life.
You got /4 concepts.
Describe how you would override a method in a Swift subclass and why you might want to do that.
Imagine changing a recipe to make it your own.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a base class in Swift?
easy
A. To prevent other classes from inheriting
B. To provide a general template for other classes to build upon
C. To store data only without any functions
D. To create instances that cannot be used
Solution
Step 1: Understand the role of a base class
A base class defines common properties and methods that other classes can reuse.
Step 2: Compare options with this role
To provide a general template for other classes to build upon correctly states the base class is a general template. Other options describe incorrect or unrelated roles.
Final Answer:
To provide a general template for other classes to build upon -> Option B
Quick Check:
Base class = general template [OK]
Hint: Base class = general template for subclasses [OK]
Common Mistakes:
Thinking base class cannot be inherited
Confusing base class with instances
Assuming base class only stores data
2. Which of the following is the correct way to declare a subclass named Dog inheriting from a base class Animal in Swift?
easy
A. class Dog inherits Animal { }
B. class Dog extends Animal { }
C. class Dog: Animal { }
D. class Dog < Animal { }
Solution
Step 1: Recall Swift subclass syntax
In Swift, a subclass is declared using a colon (:) after the subclass name followed by the base class name.
Step 2: Match options to syntax
class Dog: Animal { } uses class Dog: Animal { }, which is correct. Other options use incorrect keywords or symbols.
Final Answer:
class Dog: Animal { } -> Option C
Quick Check:
Subclass syntax = colon (:) [OK]
Hint: Use colon (:) to inherit in Swift class declaration [OK]
Common Mistakes:
Using 'inherits' or 'extends' keywords (not Swift)
Using wrong symbols like <
Omitting the colon between subclass and base class
The subclass Car overrides the sound() method to return "Beep" instead of the base class "Vroom".
Step 2: Identify which method is called
When calling myCar.sound(), the overridden method in Car runs, returning "Beep".
Final Answer:
"Beep" -> Option A
Quick Check:
Overridden method runs = "Beep" [OK]
Hint: Overridden methods in subclass replace base methods [OK]
Common Mistakes:
Expecting base class method output
Confusing method override with overload
Thinking print shows class name
4. Identify the error in this Swift subclass declaration:
class Animal {
func speak() {
print("Animal sound")
}
}
class Cat: Animal {
func speak() {
print("Meow")
}
}
medium
A. Class names must be lowercase
B. Base class cannot have methods
C. Subclass cannot have methods with same name
D. Missing override keyword before speak() in subclass
Solution
Step 1: Check method overriding rules in Swift
When a subclass provides its own version of a base class method, it must use the override keyword.
Step 2: Identify missing override keyword
The speak() method in Cat lacks override, causing a compile error.
Final Answer:
Missing override keyword before speak() in subclass -> Option D
Quick Check:
Override keyword required for method override [OK]
Hint: Use override keyword when redefining base methods [OK]
Common Mistakes:
Forgetting override keyword
Thinking subclass methods cannot have same name
Assuming class names must be lowercase
5. You want to create a base class Employee with a property name and a method work() that prints "Working". Then create a subclass Manager that overrides work() to print "Managing team" and adds a new method holdMeeting() that prints "Meeting started".
Which code correctly implements this?
hard
A. class Employee {
var name: String
init(name: String) {
self.name = name
}
func work() {
print("Working")
}
}
class Manager: Employee {
override func work() {
print("Managing team")
}
func holdMeeting() {
print("Meeting started")
}
}
B. class Employee {
var name: String
func work() {
print("Working")
}
}
class Manager: Employee {
func work() {
print("Managing team")
}
func holdMeeting() {
print("Meeting started")
}
}
C. class Employee {
var name: String
init(name: String) {
self.name = name
}
func work() {
print("Working")
}
}
class Manager: Employee {
func work() {
print("Managing team")
}
func holdMeeting() {
print("Meeting started")
}
}
D. class Employee {
var name: String
init(name: String) {
self.name = name
}
func work() {
print("Working")
}
}
class Manager: Employee {
override func work() {
print("Managing team")
}
}
Solution
Step 1: Check base class implementation
The base class Employee must have a name property with initializer and a work() method printing "Working". class Employee {
var name: String
init(name: String) {
self.name = name
}
func work() {
print("Working")
}
}
class Manager: Employee {
override func work() {
print("Managing team")
}
func holdMeeting() {
print("Meeting started")
}
} and C have correct base class with initializer.
Step 2: Check subclass overrides and new method
The subclass Manager must override work() with override keyword and add holdMeeting(). class Employee {
var name: String
init(name: String) {
self.name = name
}
func work() {
print("Working")
}
}
class Manager: Employee {
override func work() {
print("Managing team")
}
func holdMeeting() {
print("Meeting started")
}
} correctly uses override and adds holdMeeting(). class Employee {
var name: String
init(name: String) {
self.name = name
}
func work() {
print("Working")
}
}
class Manager: Employee {
func work() {
print("Managing team")
}
func holdMeeting() {
print("Meeting started")
}
} misses override. class Employee {
var name: String
func work() {
print("Working")
}
}
class Manager: Employee {
func work() {
print("Managing team")
}
func holdMeeting() {
print("Meeting started")
}
} misses initializer in base class. class Employee {
var name: String
init(name: String) {
self.name = name
}
func work() {
print("Working")
}
}
class Manager: Employee {
override func work() {
print("Managing team")
}
} misses holdMeeting().
Final Answer:
Option A code correctly implements all requirements -> Option A
Quick Check:
Override + new method + initializer = class Employee {
var name: String
init(name: String) {
self.name = name
}
func work() {
print("Working")
}
}
class Manager: Employee {
override func work() {
print("Managing team")
}
func holdMeeting() {
print("Meeting started")
}
} [OK]
Hint: Use override keyword and add new methods in subclass [OK]
Common Mistakes:
Forgetting override keyword in subclass
Missing initializer in base class
Not adding new methods in subclass
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.