0
0
Swiftprogramming~5 mins

Base class and subclass in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a base class in Swift?
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 { }
What keyword must you use to replace a base class method in a subclass?
Aoverride
Breplace
Csuper
Dextend
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>
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
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
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.