# Object Oriented Programming in Swift

**URL:** <https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071>\
**Category:** 🎓 Courses & Articles\
**Created:** [May 24, 2017, 1:03pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071 "2017-05-24T13:03:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [May 24, 2017, 1:03pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/1 "2017-05-24T13:03:03Z")

</div>

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](https://www.raywenderlich.com/599-object-oriented-programming-in-swift)

---

<div class="post-metadata">

**Author:** ![mennabah](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mennabah/32/100250_2.png) [@mennabah](https://forums.kodeco.com/u/mennabah)\
**Post date:** [May 25, 2017, 4:39pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/2 "2017-05-25T16:39:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [May 26, 2017, 12:00pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/3 "2017-05-26T12:00:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iavor](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/iavor/32/65145_2.png) [@iavor](https://forums.kodeco.com/u/iavor)\
**Post date:** [May 26, 2017, 8:29pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/4 "2017-05-26T20:29:05Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [May 29, 2017, 3:15pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/5 "2017-05-29T15:15:14Z")

</div>

You can read more about modules and access control here: [Access Control — The Swift Programming Language (Swift 5.7)](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html)

---

<div class="post-metadata">

**Author:** ![iavor](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/iavor/32/65145_2.png) [@iavor](https://forums.kodeco.com/u/iavor)\
**Post date:** [May 29, 2017, 9:47pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/6 "2017-05-29T21:47:30Z")

</div>

Thank you @shogunkaramazov!

---

<div class="post-metadata">

**Author:** ![felima1965](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@felima1965](https://forums.kodeco.com/u/felima1965)\
**Post date:** [August 29, 2017, 1:13pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/7 "2017-08-29T13:13:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 29, 2017, 1:26pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/8 "2017-08-29T13:26:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![felima1965](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@felima1965](https://forums.kodeco.com/u/felima1965)\
**Post date:** [September 9, 2017, 3:52am UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/9 "2017-09-09T03:52:41Z")

</div>

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)."
    }
}

```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [October 10, 2017, 12:01pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/10 "2017-10-10T12:01:34Z")

</div>

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! :]

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 14, 2017, 2:40pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/11 "2017-11-14T14:40:51Z")

</div>

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! :]

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 14, 2017, 2:40pm UTC](https://forums.kodeco.com/t/object-oriented-programming-in-swift/30071/12 "2017-11-14T14:40:55Z")

</div>

Closed on November 14, 2017, 2:40pm UTC


