Kodeco Forums

Object Oriented Programming in Swift

Learn how object oriented programming works in Swift by breaking things down into objects that can be inherited and composed from.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/599-object-oriented-programming-in-swift

Thank you, Cosmin and the RW team for a great tutorial.
I have a questione in the second challengee ewhen you call
bassGuitar.amplifier.unPlug() the volume goes down to 0 because that’s how we defined it in the getter part of the Amplifier base class.
and when you plug it again:
bassGuitar.amplifier.plugIn()
the volume goes to 5 as described in the tune() method.

My question is, how does it goe again to 5, while I didn’t call the tune() function when I plugged it again?

My expectation was it will still at 0 euntil I call the tune again or directly change the volume value.

The plugIn() method sets the isOn property to true, so the volume getter sets its corresponding value accordingly.

@shogunkaramazov
This is a very informative and easy-to-follow article, thank you!

In the Access Control section, you mention the word module. What is a module and is it different to a file?

You can read more about modules and access control here: Access Control — The Swift Programming Language (Swift 5.7)

Thank you @shogunkaramazov!

Thank you very much Cosmin @shogunkaramazov, this article was very well written. It provided very useful information about implementing OOP in swift, I’m new to it, and it’s indeed being very good to my OOP Swift learning. Congratulations!

Thank you for your very kind words - much appreciated! :]

Hi Cosmin @shogunkaramazov. Would you be so kind to help me understand why I’m having the error below? When I added the ElectricGuitar class according to the exercise, the initialization of the amplifier property is returning the error: “Initializer does not override a designated initializer from its superclass”. So I was wondering why amplifier instance is being initialized within the overridden init clause along the rest of the Guitar superclass properties. I believe that the Amplifier class is not being inherited here, right? As you explained, it’s a “has-a relationship”, so trying to override the amplifier instance wouldn’t make the compiler look up for it within its parent Guitar class? Did I get it right? So what is the correct way to initialize the amplifier instance here? I tried the way below, but I’m not sure if t’s the correct way to go.

Thank you very much for your attention.

class ElectricGuitar: Guitar {
    let amplifier: Amplifier
    init(brand: String, stringGauge: String = "light", amplifier: Amplifier) {
        self.amplifier = amplifier
        super.init(brand: brand, stringGauge: stringGauge)
    }
    override func tune() -> String {
        amplifier.plugIn()
        amplifier.volume = 5
        return "Tune \(brand) electric with E A D G B E"
    }
    override func play(_ music: Music) -> String {
        let prepareNotes = super.play(music)
        return "Play solo \(prepareNotes) at volume \(amplifier.volume)."
    }
}

Sorry for my late reply on this. The code above works fine for me. Please let me know if you are still having questions or issues regarding the whole thing. Thank you! :]

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]