Bird
Raised Fist0
Swiftprogramming~10 mins

Base class and subclass in Swift - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a base class named Vehicle.

Swift
class [1] {
    var speed: Int = 0
}
Drag options to blanks, or click blank then click option'
ACar
BTransport
CBike
DVehicle
Attempts:
3 left
💡 Hint
Common Mistakes
Using a subclass name like Car instead of the base class name Vehicle.
2fill in blank
medium

Complete the code to declare a subclass named Car that inherits from Vehicle.

Swift
class Car: [1] {
    var brand: String = ""
}
Drag options to blanks, or click blank then click option'
AVehicle
BMachine
CBike
DTransport
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong class name after the colon that is not the base class.
3fill in blank
hard

Fix the error in the subclass initializer to call the base class initializer.

Swift
class Vehicle {
    var speed: Int
    init(speed: Int) {
        self.speed = speed
    }
}

class Car: Vehicle {
    var brand: String
    init(brand: String, speed: Int) {
        self.brand = brand
        [1]
    }
}
Drag options to blanks, or click blank then click option'
Aself.init(speed: speed)
BVehicle.init(speed)
Csuper.init(speed: speed)
Dinit(speed)
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the base class initializer without super.
Calling self.init inside the initializer causing recursion.
4fill in blank
hard

Fill both blanks to override the base class method and add a new method in the subclass.

Swift
class Vehicle {
    func description() -> String {
        return "Vehicle moving at speed 0"
    }
}

class Car: Vehicle {
    override func [1]() -> String {
        return "Car moving at speed 0"
    }
    func [2]() -> String {
        return "This is a car"
    }
}
Drag options to blanks, or click blank then click option'
Adescription
Binfo
Cdetails
Dsummary
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name for the overridden method.
Forgetting the override keyword.
5fill in blank
hard

Fill all three blanks to create a subclass that overrides a method and calls the base class method inside it.

Swift
class Vehicle {
    func move() -> String {
        return "Vehicle is moving"
    }
}

class Bike: Vehicle {
    override func [1]() -> String {
        let baseMessage = super.[2]()
        return baseMessage + ", Bike is moving fast"
    }
    func [3]() -> String {
        return "Bike details"
    }
}
Drag options to blanks, or click blank then click option'
Amove
Cdetails
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Not calling the base class method inside the override.
Using different method names for override.

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

  1. Step 1: Understand the role of a base class

    A base class defines common properties and methods that other classes can reuse.
  2. 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.
  3. Final Answer:

    To provide a general template for other classes to build upon -> Option B
  4. 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

  1. 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.
  2. Step 2: Match options to syntax

    class Dog: Animal { } uses class Dog: Animal { }, which is correct. Other options use incorrect keywords or symbols.
  3. Final Answer:

    class Dog: Animal { } -> Option C
  4. 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
3. Consider this Swift code:
class Vehicle {
    func sound() -> String {
        return "Vroom"
    }
}

class Car: Vehicle {
    override func sound() -> String {
        return "Beep"
    }
}

let myCar = Car()
print(myCar.sound())

What will be printed when this code runs?
medium
A. "Beep"
B. "Vroom"
C. Compilation error
D. "Car"

Solution

  1. Step 1: Understand method overriding in subclass

    The subclass Car overrides the sound() method to return "Beep" instead of the base class "Vroom".
  2. Step 2: Identify which method is called

    When calling myCar.sound(), the overridden method in Car runs, returning "Beep".
  3. Final Answer:

    "Beep" -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify missing override keyword

    The speak() method in Cat lacks override, causing a compile error.
  3. Final Answer:

    Missing override keyword before speak() in subclass -> Option D
  4. 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

  1. 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.
  2. 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().
  3. Final Answer:

    Option A code correctly implements all requirements -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard