0

I'm just starting to take up programming and am trying to understand the way a subclass could update a variable in a superclass. I have a family superclass that keeps track of family members and every time a member of the family is created the family members variable adds 1 to it. here is what i have.

I have tried a couple ways of doing this, including creating a update family members method in the superclass and calling it from the child classes in their init statements but it still didn't work. If anyone could help me or at least explain why its not updating I would be grateful. Thanks.

Oh, the playground states it runs the familymembers++ 4 times, but the variable is not keeping the updates. I have also tried creating two variable and having one update the other but still nothing.

EDIT:

Thank you. I will try and understand the concept better. I appreciate your help. Thanks :D

class Family {

    var familyMembers = 0

    init() {
        familyMembers++
    }

    func talk() {
        println("The family is talking")
    }

    func argue() {
        println("The family is arguing")
    }


}

class Son : Family {
    var hisName = ""
    init(name: String) {
        super.init()
        self.hisName = name
    }

    override func argue() {
        println("\(hisName) is arguing")
    }

}

class Mother : Family {
    var herName = ""
    init(name: String) {
        super.init()
        self.herName = name
    }
}
let barthelemy = Family()
let ayden = Son(name: "ayden")
let tasha = Mother(name: "tasha")
let jim = Son(name: "jim")


barthelemy.familyMembers

1 Answer 1

4

This is because each time you create a Family object or a subclass (so a Son or Mother), you're referencing a different Family: familyMembers++ gets called, but it's for the variable for each specific family member rather than a shared variable that is accessed by each object.

It seems like you're a little confused about what a subclass (and, by extension, inheritance) symbolizes: instead of thinking that a subclass belongs to its parent class, pretend that a subclass is a more specific version of its parent class. For example, a Jaguar would be a subclass of an Animal; a Sedan would be a subclass of a Car; to use your example, a Son would not be a subclass of a Family, but a subclass of a FamilyMember.

Consider the following example, in which we pass a common Family object to the FamilyMember constructor to keep track of the number of members:

import Cocoa

class Family {

    var familyMembers = 0

    func talk() {
        println("The family is talking")
    }

    func argue() {
        println("The family is arguing")
    }
}

class FamilyMember {
    init(family: Family) {
        family.familyMembers++
    }
}

class Son : FamilyMember {
    var hisName = ""
    init(family: Family, name: String) {
        super.init(family: family)
        self.hisName = name
    }
}

class Mother : FamilyMember {
    var herName = ""
    init(family: Family, name: String) {
        super.init(family: family)
        self.herName = name
    }
}

let barthelemy = Family()
let ayden = Son(family: barthelemy, name: "ayden")
let tasha = Mother(family: barthelemy, name: "tasha")
let jim = Son(family: barthelemy, name: "jim")


barthelemy.familyMembers // returns '3'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for the answer. If you have time could you explain what this line is doing super.init(family: family) I'm not understanding that out of this whole thing.. Thanks again!
Sure thing! super.init(family: family) is calling the constructor of the superclass and passing in the variable family as the first parameter (which itself was passed into the constructor). It may look a little confusing since family is showing up twice: the first time it's the name of the argument as defined in init(family: Family) -- the second time, it's what you're actually passing in as the argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.