Swift Tutorial - Tutorialspoint

(backadmin) #1

}


}


class tennis: cricket {
override func print() {
println("Welcome to Swift Sub Class")
}
}

let cricinstance = cricket()
cricinstance.print()

let tennisinstance = tennis()
tennisinstance.print()

When we run the above program using playground, we get the following result:


Welcome to Swift Super Class
Welcome to Swift Sub Class

Property Overriding


You can override an inherited instance or class property to provide your own custom getter
and setter for that property, or to add property observers to enable the overriding property
to observe when the underlying property value changes.


Overriding Property Getters and Setters


Swift allows the user to provide custom getter and setter to override the inherited property
whether it is a stored or computed property. The subclass does not know the inherited
property name and type. Therefore it is essential that the user needs to specify in subclass,
the name and type of the overriding property specified in super class.


This can be done in two ways:


 When setter is defined for overriding property the user has to define getter too.

 When we don't want to modify the inherited property getter, we can simply pass
the inherited value by the syntax 'super.someProperty' to the super class.

class Circle {
var radius = 12.5
var area: String {
Free download pdf