What is the difference between class and subclass in Swift ?
First of all, I want to explain the swift class, we should understand it by making examples with codes.
A Swift class is a blueprint for creating objects, defining properties and methods for those objects to use. It is a blueprint for creating instances, or objects, of a particular type. Classes are the building blocks of object-oriented programming and allow developers to define and encapsulate complex behaviors and data structures in a single unit. In Swift, a class can inherit from another class, allowing it to inherit its properties and methods, and can also conform to protocols, which define a set of required and optional methods and properties that a class must implement.
A class in Swift is a blueprint for creating objects (also known as instances) that have certain attributes (properties) and behaviors (methods). Classes allow you to define the data structure and logic of objects, which you can then use in your code.
Here’s an example of a simple class in Swift:
class Car {
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 startEngine() {
print("Vroom! The \\(make) \\(model) is starting its engine.")
}
}
In this example, Car
is a class that has three properties: make
, model
, and year
. It also has an initializer init(make: String, model: String, year: Int)
, which is used to set the values of the properties when an instance of Car
is created. Finally, it has a method startEngine()
that prints a message to the console.
To create an instance of the Car
class, you use the following code:
let myCar = Car(make: "Tesla", model: "Model 3", year: 2020)
Once you have an instance of the class, you can access its properties and call its methods. For example:
myCar.make
myCar.startEngine()
This is just a basic example of what you can do with classes in Swift. They can be much more complex and have many more properties and methods, and can also inherit from other classes and conform to protocols. But at their core, classes are an essential building block of object-oriented programming, allowing you to define and encapsulate data and logic in a single, reusable unit.
Here’s an example of a class in Swift that represents a person:
class Person {
var firstName: String
var lastName: String
var age: Int
init(firstName: String, lastName: String, age: Int) {
self.firstName = firstName
self.lastName = lastName
self.age = age
}
func fullName() -> String {
return firstName + " " + lastName
}
}
In this example, the Person
class has three properties: firstName
, lastName
, and age
. It also has an initializer init(firstName: String, lastName: String, age: Int)
, which is used to set the values of the properties when an instance of Person
is created. Finally, it has a method fullName()
that returns a string containing the person's full name.
To create an instance of the Person
class, you use the following code:
let john = Person(firstName: "John", lastName: "Doe", age: 35)
Once you have an instance of the class, you can access its properties and call its methods. For example:
print(john.firstName)
print(john.fullName())
This is just a basic example of what you can do with classes in Swift. They can be much more complex and have many more properties and methods, and can also inherit from other classes and conform to protocols.
Second, let’s start explaining subclass with code examples.
A subclass is a class that inherits properties and methods from another class, which is known as its superclass. In Swift, subclassing is a fundamental aspect of object-oriented programming and enables you to create new classes that are based on existing classes, but that add or modify the properties and methods of the superclass to fit your specific needs.
Here’s an example of a subclass 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 startEngine() {
print("Vroom! The \\(make) \\(model) is starting its engine.")
}
}
class Car: Vehicle {
var numberOfDoors: Int
init(make: String, model: String, year: Int, numberOfDoors: Int) {
self.numberOfDoors = numberOfDoors
super.init(make: make, model: model, year: year)
}
}
In this example, we have a superclass Vehicle
that defines three properties and a method. Then, we create a subclass Car
that inherits from Vehicle
. The Car
class adds a new property numberOfDoors
and overrides the init
method to include this new property. The super.init
call at the end of the init
method is used to call the initializer of the superclass and set the values of its properties.
Once you have a subclass, you can create instances of it just like you would with any other class. The difference is that instances of a subclass also have access to the properties and methods of its superclass. For example:
let myCar = Car(make: "Tesla", model: "Model 3", year: 2020, numberOfDoors: 4)
myCar.startEngine()
In this code, myCar
is an instance of the Car
class, and it has access to the properties and methods of both the Car
class and the Vehicle
superclass. So, you can call the startEngine()
method just as you would with an instance of the Vehicle
class.
Subclassing is a powerful feature in Swift that allows you to build complex and flexible class hierarchies, and to reuse and extend existing classes to fit your specific needs.
Here’s an example of a Person
class and a subclass called Student
:
class Person {
var firstName: String
var lastName: String
var age: Int
init(firstName: String, lastName: String, age: Int) {
self.firstName = firstName
self.lastName = lastName
self.age = age
}
func fullName() -> String {
return firstName + " " + lastName
}
}
class Student: Person {
var studentID: Int
init(firstName: String, lastName: String, age: Int, studentID: Int) {
self.studentID = studentID
super.init(firstName: firstName, lastName: lastName, age: age)
}
}
In this example, the Student
class is a subclass of the Person
class. The Student
class adds a new property studentID
and an initializer to set its value. The super.init
call at the end of the initializer is used to call the initializer of the Person
class and set the values of its properties.
You can create instances of the Student
class just like you would with any other class:
let jane = Student(firstName: "Jane", lastName: "Doe", age: 22, studentID: 12345)
Since Student
is a subclass of Person
, an instance of Student
also has access to the properties and methods of the Person
class:
print(jane.firstName)
print(jane.fullName())