
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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.
- Related Articles
- What is a base class in C#?
- How to create an unordered_map of user defined class in C++?
- How I can check if class attribute was defined or derived in given class in Python?
- What is a virtual base class in C++?
- How to explicitly call base class constructor from child class in C#?
- Virtual base class in C++
- How to create an unordered_set of user defined class or struct in C++?
- What is the base class for all exceptions in C#?
- What is the difference between class and structure in Swift?
- When should we create a user-defined exception class in Java?
- What is the base class for errors and exceptions in Java?
- What is the base class for all data types in C#.NET?
- What is the difference between Static func and Class func in Swift?
- Swift: Test class type in the switch statement
- Swift Program to Show Overloading of Methods in Class
