0% found this document useful (0 votes)
94 views

SWIFT Chapter11 Inheritance

The document discusses inheritance in Swift by defining classes like Shape, Square, Circle, and Sphere. Shape is the base class that is inherited by Square and Circle. Sphere inherits from Circle. The subclasses override functions like area() and description() from the base classes. Sphere also accesses the area() function of its superclass Circle using super.area(). Later, different shape objects are created and their description() functions are called to output properties like area and volume.

Uploaded by

Hien Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

SWIFT Chapter11 Inheritance

The document discusses inheritance in Swift by defining classes like Shape, Square, Circle, and Sphere. Shape is the base class that is inherited by Square and Circle. Sphere inherits from Circle. The subclasses override functions like area() and description() from the base classes. Sphere also accesses the area() function of its superclass Circle using super.area(). Later, different shape objects are created and their description() functions are called to output properties like area and volume.

Uploaded by

Hien Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 11: Inheritance

class Shape{ //No inherited from anything


var title="Shape"
func area() ->Double{
return 0.0
}
func description()->String{
return "I am a \(title). My area is \(area())"
}
}
class Square:Shape{
var sideLength = 0.0
override func area() -> Double{
return sideLength*sideLength
}
}
let square=Square()
square.sideLength=4
square.title = "SQUARE"
println(square.area())
println(square.description())

ACCESSING SUPER FROM A SUBCLAS


class Circle:Shape{
var radius=0.0
final override func area() -> Double{
return M_PI*radius*radius
}
}
class Sphere:Circle{
override func description() -> String{
return super.description()+".My volume is \(volume())"
}
func volume() ->Double{
return (4.0*super.area()*radius)/3.0
}
}
let sphere = Sphere()
sphere.title="Sphere"
sphere.radius=2.0
println(sphere.volume())
println(sphere.description())

Preventing Overrides

Class Identity
class Shape{ //No inherited from anything
var title="Shape"
func area() ->Double{
return 0.0
}
func description()->String{
return "I am a \(title). My area is \(area())"
}
}
class Square:Shape{
var sideLength = 0.0
override func area() -> Double{
return sideLength*sideLength
}
}
let square=Square()
square.sideLength=4
square.title = "SQUARE"
println(square.area())
println(square.description())
class Circle:Shape{

var radius=0.0
override func area() -> Double{
return M_PI*radius*radius
}
}
class Sphere:Circle{
override func description() -> String{
return super.description()+".My volume is \(volume())"
}
func volume() ->Double{
return (4.0*super.area()*radius)/3.0
}
}
var sphere1 = Sphere()
sphere1.title="Sphere"
sphere1.radius=2.0
var square1 = Square()
square1.title="Square"
square1.sideLength=5.0
// println(sphere.volume())
// println(sphere.description())

func printDescriptionForShape(shape:Shape){
println(shape.description())
}
println(printDescriptionForShape(sphere1))
println(printDescriptionForShape(square1))

Exercise
class Shape{ //No inherited from anything
var title="Shape"
func area() ->Double{
return 0.0
}
func description()->String{
return "I am a \(title). My area is \(area())"
}
}
class Square:Shape{
var sideLength = 0.0
override func area() -> Double{
return sideLength*sideLength
}
}

let square=Square()
square.sideLength=4
square.title = "SQUARE"
println(square.area())
println(square.description())
class Circle:Shape{
var radius=0.0
override func area() -> Double{
return M_PI*radius*radius
}
}
class Sphere:Circle{
override func description() -> String{
return super.description()+".My volume is \(volume())"
}
func volume() ->Double{
return (4.0*super.area()*radius)/3.0
}
}

class Cylinder:Circle{
var height:Double = 0.0

func volume() ->Double{


return super.area()*height
}
func surfaceArea() ->Double{
return 2*super.area()+2*M_PI*height
}
override func description() -> String {
return super.description()+". My volume is \(volume())"
}
}
var cylinder=Cylinder()
cylinder.radius=5
cylinder.height=10
cylinder.title="Cylinder"
println(cylinder.volume())
println(cylinder.description())
http://swiftstub.com/857054676/

You might also like