​Swift is just a powerful and intuitive development language for macOS, iOS, watchOS and tvOS....

Page 141

Swift

init() { super.init(stm1: 93, results: 89) } }

let marksobtained = display() marksobtained.print() When we run the above program using playground, we get the following result: Mark1:93, Mark2:89 Class 'StudDetails' is defined as super class where student marks are declared and the subclass 'display' is used to inherit the marks from its super class. Sub class defines students marks and calls the print() method to display the students mark.

Overriding Accessing the super class instance, type methods, instance, type properties and subscripts subclass provides the concept of overriding. 'override' keyword is used to override the methods declared in the superclass.

Access to Super class Methods, Properties and Subscripts 'super' keyword is used as a prefix to access the methods, properties and subscripts declared in the super class. Overriding

Access to methods,properties and subscripts

Methods

super.somemethod()

Properties

super.someProperty()

Subscripts

super[someIndex]

Methods Overriding Inherited instance and type methods can be overridden by the 'override' keyword to our methods defined in our subclass. Here print() is overridden in subclass to access the type property mentioned in the super class print(). Also new instance of cricket() super class is created as 'cricinstance'. class cricket { func print() { println("Welcome to Swift Super Class") 129


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.