-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Learning Swift Second Edition
By :

A class can do everything that a structure can do except that a class can use something called inheritance. A class can inherit the functionality from another class and then extend or customize its behavior. Let's jump right into some code.
Firstly, let's define a class called Building
that we can inherit from later:
class Building { let squareFootage: Int init(squareFootage: Int) { self.squareFootage = squareFootage } } var aBuilding = Building(squareFootage: 1000)
Predictably, a class is defined using the class
keyword instead of struct
. Otherwise, a class looks extremely similar to a structure. However, we can also see one difference. With a structure, the initializer we created before would not be necessary because it would have been created for us. With classes, initializers are not automatically created unless all of the properties have default values.
Now let's look at how to inherit from this building class:
class...
Change the font size
Change margin width
Change background colour