• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Tutorial – Swift Classes, Subclasses Part 2

Home ios Tutorial – Swift Classes, Subclasses Part 2

Tutorial – Swift Classes, Subclasses Part 2

Jun 28, 2015 | Posted by Andrew | ios, swift, tutorial, xcode |

In our last Classes tutorial we looked at making a brand new Pet class in Swift, This post will look at how to make a subclass for this pet class, so we can extend it and override methods. You can download the source code from the post here to avoid having to start from scratch.

Our pet class looks like the following:

Screen Shot 2015-06-28 at 5.29.11 pm

What we are going to do is add a new PetDog subclass that will inherent all the variables and functions from the pet class so it can use anything that the pet class has, as well as add some new functionality to it. Once completed our PetDog class will look like the following:

Screen Shot 2015-06-28 at 5.33.03 pm

Open the existing classes XCode project as given. Once done we are going to create a new blank Swift Class. Go to File–>New–>File and select “Swift File”. Name this class PetDog.

You will now have PetDog.swift that is empty except for import Foundation which you can delete as you don’t need it. Now we need to declare a new class again, however this time we need to make sure we are inheriting from the pet class. To do this we simply add : Pet after declaring the class name as follows.

class PetDog: Pet {
// Class code here....
}

By doing this our PetDog class now has access to the variables and methods in the Pet class. We are going to add one new variable to our PetDog class as follows:

class PetDog: Pet {
    var tailLength = Int()
}

We still need to initialize this class. Do as follows:

    init(name: String, tailLength: Int) {
        super.init(name: name, legs: 4)
        self.tailLength = tailLength
    }

You would have noticed this init looks different then the one contained in the Pet class. What’s this super.init all about?

Because PetDog is a subclass of Pet. Pet is the super (also known as the parent) of PetDog. Super.init will run the init in pet with the name and number of legs, as these properties (variables) are contained in the parent class. Since tailLength is a property of our PetDog class we can set it in here.

Finally in our PetDog function we are going to override sayMyName in Pet. Basically since PetDog has access to all of it’s super class method (Pet) – if we want that method to be different in the PetDog class we override it. This forces any instances of the PetDog class to use the sayMyName func in PetDog instead of Pet.

class PetDog: Pet {
    var tailLength = Int()
    
    init(name: String, tailLength: Int) {
        super.init(name: name, legs: 4)
        self.tailLength = tailLength
    }
    
    override func sayMyName() {
        println("Woof woof im \(name)")
    }
}

Once done we will go back to our ViewController and change the code to reflect the following ViewDidLoad method:

        var myPetCat = Pet(name: "Tom", legs: 4) // Setup a new Pet class instance, stored in myPetCat
        myPetCat.sayMyName()
        myPetCat.countLegs()
        myPetCat.removeLeg()
        myPetCat.countLegs()
        //myPetCat.name = "Tommy" // This is bad practice - you should have a method to set the name
        myPetCat.changeName("Tommy")
        myPetCat.sayMyName()
        
        println("----")
        
        var myPetDog = PetDog(name: "Max", tailLength: 5)
        myPetDog.sayMyName()
        myPetDog.countLegs()
        println(myPetDog.tailLength)

Run the code and you will see the following output:

Hey my name is Tom
I have 4 legs
I have 3 legs
Hey my name is Tommy
----
Woof woof im Max
I have 4 legs
5

As you can see in the above output when we run the sayMyName function on a myPetDog class it overrides the Pet class function. You can also see that it still has access to the methods of the parent class such as countLegs.

DownloadSourceCode

Tags: class
1
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Swift Protocols Made Easy Tutorial

Sep 18, 2017

Protocol based programming is a difficult concept to initially understand[...]

Tutorial – Swift Classes Introduction

Jun 28, 2015

Today we are going to look at classes and how[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next