iOS

iOS Lesson 13: Inheritance in Swift

So in this knowledge bit, we are going to talk about Inheritance in Swift.  Just like real life, inheritance also has similar meanings to “Inherit from parent”. It ultimately means that there will be some methods and properties are going to be inherited from parent to a child (class).

Open up the playground and click on “Getting started with a playground”

We are going to call this one “Inheritance”. Delete the “hello, playground” line.

Inheritance in Swift:

Inheritance in swift is basically same as principal of object-oriented programming. It is a way to get access to some methods and properties of other classes by setting up a parent-child behavior. The class who wants to inherit becomes the child and the class who gets inherited becomes parent class.

These classes can access properties, methods that belong to their parent (superclass). These classes also have an option to provide an overridden implementation of their superclass methods.

Example:

Code:

var Make = 1

var Model : String?

 

var HorsePower : Double = 0

var speed = 200

 

func Drive(){

print("Driving the vehicle")

}

 

func Brake(){

print("Applying brake on Vehicle")

}

Inheritance in Swift example

Here, we have a default class for a vehicle. It has following variables and methods.

Variables:

  • Make
  • Model
  • Speed
  • HorsePower

Methods:

  • Drive
  • Brake
  • Init

PS: Init() is the constructor. It gets called whenever we create the object for a class.

This class is ready to get inherited, let’s create a subclass/child class that will inherit vehicle class.

SubClass:

class Starlet : Vehicle{

}       

Explanation:

The syntax for defining the subclass is by adding a colon after class name and then the name of the class that it wants to inherit from.

We have defined a class “Starlet” and we have inherited from “Vehicle”. So, basically, Starlet will have access to “Vehicle” variables and methods.

There is nothing in the starlet class but you can access the Make, Model variables of the vehicle class inside the Starlet.

 

Syntax:

override init() {

super.init()

Make = 21

}

There is only one thing that you’ll need to do when trying to access the properties of the base/parent class. You will have to call its init method to initialize the base values that you can override the values on your own.

So every class have an initialization function

Code for Starlet class:


class Starlet : Vehicle{

override init() {

super.init()

print("Hello, This is the starlet  ")

Model = "as2"

Make = 2

speed = 100

}

 
override func Drive(){

print("Driving starlet at \(speed)")

}

override func Brake() {

print("put brake on starlet")

}

}

 

Explanation:

Here, we have defined our own version of the init for a starlet. But, as Starlet is inheriting from Vehicle we would need to call vehicle init function explicitly by using super.init(). It will initialize the default values from Vehicle and then can move forward with the custom modifications done by Starlet class.

We have also modified the Brake and Drive function in our starlet class.

Additional Information:

Now, If we would create an instance/object of the child class. Its parent will be called by default. We are going to look for more details about how to prevent child and other classes from accessing the properties and methods.

Let’s create an instance of “Starlet”

 

Code:

 



       let ab = Starlet()

ab.Brake()

ab.Drive()

ab.Model

ab.Make

 

Inheritance in swift example - Code for Starlet class:

 

Here, we have just used let keyword to define an object of type Starlet. We can access the properties and methods of an object by putting a dot after its name.

Conclusion:

So in this knowledge bit, We have covered Inheritance and gone through how we can access methods and properties using the dot operator. In the next knowledge bit, we are going to look at polymorphism. You can get all the code from here.

Please Like, share and subscribe for more!

Click to comment

Most Popular

To Top