YouTip LogoYouTip

Swift Inheritance

Inheritance can be understood as a class acquiring the methods and properties of another class.\\n\\nWhen a class inherits from another class, the inheriting class is called the subclass, and the class being inherited from is called the superclass (or parent class).\\n\\nIn Swift, a class can call and access the methods, properties, and subscripts of its superclass, and can override them.\\n\\nWe can also add property observers to inherited properties.\\n\\n* * *\\n\\n## Base Class\\n\\nA class that does not inherit from any other class is called a Base Class.\\n\\nIn the following example, we define a base class StudDetails, which describes a student (stname) and their scores in various subjects (mark1, mark2, mark3):\\n\\nclass StudDetails { var stname: String! var mark1: Int! var mark2: Int! var mark3: Int! init(stname: String, mark1: Int, mark2: Int, mark3: Int) { self.stname = stname self.mark1 = mark1 self.mark2 = mark2 self.mark3 = mark3 }}let stname = "swift"let mark1 = 98let mark2 = 89let mark3 = 76let sds = StudDetails(stname:stname, mark1:mark1, mark2:mark2, mark3:mark3);print(sds.stname)print(sds.mark1)print(sds.mark2)print(sds.mark3)\\nThe output of the above program execution is:\\n\\nswift 988976\\n\\n* * *\\n\\n## Subclass\\n\\nA subclass refers to creating a new class based on an existing class.\\n\\nTo specify the superclass of a class, write the superclass name after the subclass name, separated by a colon (:). The syntax format is as follows:\\n\\nclass SomeClass: SomeSuperclass { // Class definition}\\n### Example\\n\\nIn the following example, we define a superclass StudDetails, and then use the subclass Tom to inherit from it:\\n\\nclass StudDetails{ var mark1: Int; var mark2: Int; init(stm1:Int, results stm2:Int) { mark1 = stm1; mark2 = stm2; } func show() { print("Mark1:\\\\(self.mark1), Mark2:\\\\(self.mark2)") }}class Tom : StudDetails{ init() { super.init(stm1: 93, results: 89) }}let tom = Tom() tom.show()\\nThe output of the above program execution is:\\n\\nMark1:93, Mark2:89\\n\\n* * *\\n\\n## Overriding\\n\\nA subclass can provide its own custom implementation for inherited instance methods, class methods, instance properties, or subscripts. This behavior is called overriding.\\n\\nWe can use the override keyword to achieve overriding.\\n\\n### Accessing Superclass Methods, Properties, and Subscripts\\n\\nYou can access the methods, properties, or subscripts of the superclass by using the super prefix.\\n\\n| Overriding | Accessing Methods, Properties, Subscripts |\\n| --- | --- |\\n| Method | super.somemethod() |\\n| Property | super.someProperty() |\\n| Subscript | super |\\n\\n* * *\\n\\n## Overriding Methods and Properties\\n\\n### Overriding Methods\\n\\nIn our subclass, we can use the override keyword to override the methods of the superclass.\\n\\nIn the following example, we override the show() method:\\n\\nclass SuperClass { func show() { print("This is super class SuperClass") }}class SubClass: SuperClass { override func show() { print("This is sub class SubClass") }}let superClass = SuperClass() superClass.show()let subClass = SubClass() subClass.show()\\nThe output of the above program execution is:\\n\\nThis is super class SuperClass, this is sub class SubClass\\n### Overriding Properties\\n\\nYou can provide custom getters (or setters) to override any inherited property, regardless of whether the inherited property is a stored or computed property.\\n\\nThe subclass does not know whether the inherited property is a stored or computed property; it only knows that the inherited property has a name and a type. Therefore, when you override a property, you must specify both its name and type.\\n\\nNotes:\\n\\n* If you provide a setter in your overridden property, you must also provide a getter.\\n\\n* If you do not want to modify the inherited property value in the overridden version's getter, you can directly return the inherited value via super.someProperty, where someProperty is the name of the property you are overriding.\\n\\nIn the following example, we define a superclass Circle and a subclass Rectangle. In the Rectangle class, we override the area property:\\n\\nclass Circle { var radius = 12.5 var area: String { return "Rectangle radius \\\\(radius) " }}// Inherited super class Circle, class Rectangle: Circle { var print = 7 override var area: String { return super.area + " ,But now it is overridden to \\\\(print)" }}let rect = Rectangle() rect.radius = 25.0 rect.print = 3print("Radius \\\\(rect.area)")\\nThe output of the above program execution is:\\n\\nRadius Rectangle radius 25.0 ,But now it is overridden to 3\\n\\n* * *\\n\\n## Overriding Property Observers\\n\\nYou can add property observers to an inherited property in a property override. This way, you will be notified when the inherited property value changes.\\n\\n**Note:** You cannot add property observers to inherited constant stored properties or inherited read-only computed properties.\\n\\nclass Circle { var radius = 12.5 var area: String { return "Rectangle radiusis \\\\(radius) " }}class Rectangle: Circle { var print = 7 override var area: String { return super.area + " ,But now it is overridden to \\\\(print)" }}let rect = Rectangle() rect.radius = 25.0 rect.print = 3print("Radius: \\\\(rect.area)")class Square: Rectangle { override var radius: Double { didSet { print = Int(radius/5.0)+1 } }}let sq = Square() sq.radius = 100.0print("Radius: \\\\(sq.area)")Radius: Rectangle radiusis 25.0 ,But now it is overridden to 3Radius: Rectangle radiusis 100.0 ,But now it is overridden to 21\\n\\n* * *\\n\\n## Preventing Overriding\\n\\nWe can use the final keyword to prevent them from being overridden.\\n\\nIf you override a final method, property, or subscript, a compile-time error will be reported.\\n\\nYou can mark an entire class as final by adding the final attribute before the class keyword (final class). Such a class cannot be inherited, otherwise a compile-time error will be reported.\\n\\nfinal class Circle { final var radius = 12.5 var area: String { return "Rectangle radiusis \\\\(radius) " }}class Rectangle: Circle { var print = 7 override var area: String { return super.area + " ,But now it is overridden to \\\\(print)" }}let rect = Rectangle() rect.radius = 25.0 rect.print = 3print("Radius: \\\\(rect.area)")class Square: Rectangle { override var radius: Double { didSet { print = Int(radius/5.0)+1 } }}let sq = Square() sq.radius = 100.0print("Radius: \\\\(sq.area)")\\nBecause the above example uses the final keyword which does not allow overriding, execution will report an error:\\n\\nerror: var overrides a 'final' var override var area: String { ^ note: overridden declaration is here var area: String { ^ error: var overrides a 'final' var override var radius: Double { ^ note: overridden declaration is here final var radius = 12.5 ^ error: inheritance from a final class 'Circle'class Rectangle: Circle { ^
← Swift InitializationSwift Subscripts β†’