Skip to content

Latest commit

 

History

History

section17-extension

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Extension 1 ~ 6

1) What is extension?

Answer:

  • Way of adding new functionality without subclassing is known as Extension.
class A {

	func aFunc() {

	}

}

extension A {

  func bFunc() {

	}

}

let a: A = A()
a.aFunc()
a.bFunc()

2) What is the difference between Subclassing (Inheritance) and Extension?

Answer:

  • No overriding in extension
  • No stored properties in extension (with computed property though, you can)
  • Newly added functionality is available to all instances of that class
class A {

	func aFunc() {

	}

}

extension A {

  func bFunc() {

	}

}

let a: A = A()
a.aFunc()
a.bFunc()

3) Can we use stored property in extension?

Answer:

  • No
class A {

}

// Error
extension A {
	var b: Int = 3
}

4) Can we use computed property in extension?

Answer:

  • Yes, you can declare property with get set
class A {

}

extension A {
	var b: Int {
		get {
			return 3
		}
	}
}

5) Can we override existing methods using Extension?

Answer:

  • No, overriding the existing functionality is not allowed in Extension.

6) When to choose Extensions over inheritance?

Answer:

  • Go for extension when you want to add new functionality that should be available for all instances of that class.
  • Go for subclassing when you want to extend the functionality of a class and that should be available for only newly created class. When you want to override the functionality of a class, go for subclassing.

Table Of Contents

Section 1, Data Type

Section 2, Operator

Section 3, Conditional Statement

Section 4, Enum

Section 5, functions

Section 6, struct

Section 7, initializers

Section 8, closures

Section 9, OOP

Section 10, static type vs dynamic type

Section 11, optional

Section 12, generic

Section 13, subscript

Section 14, access specifier

Section 15, higher order function

Section 16, delegate

Section 17, extension

Section 18, Memory Management

Section 19, protocols

Section 20, collections

Section 21, KVO and KVC

Section 22, Exception Handling

Section 23, Framework

Section 24, Objective-C