How base-class is defined in Swift?


In Swift, you can define a base class using the class keyword. The base class can be used to implement common properties using a single class. You can define the properties and methods in a class like the one below −

class MyBaseClass {
   // properties and methods go here
}

A subclass is also defined by the class keyword. In a similar way, you can define the properties and methods in the class. To make it a subclass, you have to mention the parent class followed by the parent class name after the colon like the below −

class MySubclass: MyBaseClass {
   // additional properties and methods go here
}

A subclass inherits all the properties and methods of its base class, and can also add its own properties and methods.

Example

Here's an example of a base class in Swift −

class Vehicle {    
   var make: String
   var model: String
   var year: Int
    
   init(make: String, model: String, year: Int) {
      self.make = make
      self.model = model
      self.year = year
   }    
   func drive() {
      print("The \(make) \(model) is driving.")
   }
}

In this example, the Vehicle is the base class. It has three properties: make, model, and year, and an initializer that sets the values of these properties.

It also has a functioning drive() which can be called to print a message.

Example

Here's an example of a subclass that inherits from Vehicle −

class Car: Vehicle {    
   var numDoors: Int
   var isConvertible: Bool
    
   init(make: String, model: String, year: Int, numDoors: Int, isConvertible: Bool) {
      self.numDoors = numDoors
      self.isConvertible = isConvertible
      super.init(make: make, model: model, year: year)
   }    
   override func drive() {
      print("The \(make) \(model) is a car and made in the \(self.year) year.")
   }
}
let carObject = Car(make: "Tata", model: "Nano", year: 2016, numDoors: 4, isConvertible: true)
carObject.drive()

Output

The Tata Nano is a car and made in the 2016 year.

There are several advantages of using a base class in Swift

  • Reusability − With the help of superclasses with common features like properties and methods, you can share the code with multiple subclasses. Instead of writing common features increase the length of your code. The duplication of code might prove difficult to maintain.

  • Abstraction − You can create abstract interfaces using the base classes for a collection. This will help you to reduce the coupling between subclasses and superclasses. You can help hide the base implementation of a subclass from the code that uses it.

  • Polymorphism − Subclasses have the ability to override base class properties and methods while still maintaining interface compatibility. This makes it possible for objects from various subclasses to behave polymorphically, whereby they can be regarded as though they were objects from the base class.

  • Inheritance − A subclass can inherit every property and method from its base class, saving time by not having to declare them twice, and improving code readability. It will help you to reduce code duplication.

  • Code organization − Large codebases may become simpler to comprehend and browse when related classes are organised logically into basic classes. By making a single modification in the superclass, you may simply make changes throughout the entire application.

Conclusion

In conclusion, the class keyword in Swift can be used to define base classes. Additionally, by naming the parent class, you can define the subclasses. You may organize common properties and methods in a class by defining base classes. It offers flexibility to increase maintainability and reduces code duplication.

Using the class keyword, parent class and child class definitions are equivalent. You can write the shared code in the base class itself by creating a base class. To use base classes in Swift successfully, it's critical to comprehend their benefits and potential applications.

Updated on: 28-Feb-2023

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements