Saturday, 31 October 2020

Swift program inheritance with polymorphism - overriding

 In this program inheritance is done by using method overriding


class Animal: NSObject {

    //Creating and storing variables

    var eyes:UInt8?

    var tongue:UInt8?

    var heart:UInt8?

    var tail:UInt8?

    func bodyParts()

    {

        print("No.of eyes = \(eyes!)\nTongue      = \(tongue!)\nHearts      = \(heart!)\nTails      = \(tail!)")

    }

}

class Tiger: Animal {

    

    //Creating and storing a variable

    var ears:UInt8?

    var legs:UInt8?

    var mouth:UInt8?

    //Overriding the function from parent calss

    override func bodyParts()//Polymorphism

    {

        print("No.of eyes = \(eyes!)\nMouth      = \(mouth!)\nTongue      = \(tongue!)\nHearts      = \(heart!)\nTails      = \(tail!)\nEars      = \(ears!)\nLegs     = \(legs!)")

    }

}

No comments:

Post a Comment