Classes and Structures - OOP, Properties, Methods
Learn Swift classes and structures: OOP basics, properties, methods, constructors, and comparison with JavaScript
Classes and Structures: OOP, Properties, Methods
In this module, we explore object-oriented programming (OOP) in Swift and compare it with JavaScript's class and prototype system. Swift provides both classes (reference types) and structures (value types), each with their own use cases and features.
Table of Contents
- Introduction: Classes vs Structures
- Declaring Classes and Structures
- Properties
- Methods
- Initializers (Constructors)
- Inheritance and Polymorphism
- Value Types vs Reference Types
- Protocols (Interfaces)
- Advanced Features
- Exercises
- Key Takeaways
Introduction: Classes vs Structures
- Class: Reference type, supports inheritance, deinitializers, identity comparison, reference semantics.
- Struct: Value type, no inheritance, copied on assignment, value semantics, lightweight and preferred for simple data models.
Feature | JavaScript Class | Swift Class | Swift Struct |
---|---|---|---|
Reference Type | Yes | Yes | No |
Value Type | No | No | Yes |
Inheritance | Yes | Yes | No |
Deinitializer | No | Yes | No |
Mutability | Dynamic | Controlled | Controlled |
Protocols | No (interface) | Yes | Yes |
Use Case | All OOP | OOP | Data models, lightweight types |
Declaring Classes and Structures
Properties
Swift supports stored properties, computed properties, and property observers. JavaScript properties are always dynamic.
Methods
Both JavaScript and Swift support instance methods. Swift also supports static/class methods and mutating methods for structs.
Initializers (Constructors)
Swift uses init
for initializers. Structs get a memberwise initializer by default.
Inheritance and Polymorphism
Swift classes support single inheritance, while JavaScript classes also support single inheritance but can use mixins and composition.
Basic Inheritance
Polymorphism
Value Types vs Reference Types
This is a fundamental difference between Swift and JavaScript.
Reference Types (Classes)
Value Types (Structs)
Protocols (Interfaces)
Swift protocols are similar to JavaScript interfaces but more powerful.
Advanced Features
Property Wrappers and Computed Properties
Static and Class Methods
Exercises
Exercise 1: Create a Student Management System
Exercise 2: Implement a Shape Hierarchy
Key Takeaways
Swift OOP Advantages
- Value Types: Structs provide value semantics, preventing unexpected mutations
- Type Safety: Strong typing prevents runtime errors
- Protocols: Flexible interface system for both classes and structs
- Property Observers: Built-in support for property change notifications
- Memory Management: ARC handles memory automatically
Key Differences from JavaScript
- Value vs Reference: Swift distinguishes between value and reference types
- Inheritance: Only classes support inheritance in Swift
- Mutability: Swift structs require explicit mutating methods
- Protocols: Swift has formal protocol system vs JavaScript duck typing
- Memory Management: ARC vs garbage collection
Best Practices
- Use structs for simple data models and value semantics
- Use classes for complex objects that need inheritance
- Leverage protocols for flexible interfaces
- Prefer composition over inheritance when possible
- Use property observers for reactive programming
- Consider value types for thread safety and performance
Next Steps
In the next module, we'll explore protocols and extensions in Swift, including protocol-oriented programming, protocol extensions, and how they compare to JavaScript's approach to interfaces and mixins.
Control Flow - Conditionals, Loops, and Pattern Matching
Learn Swift control flow: conditionals, loops, switch statements, and pattern matching with JavaScript comparisons
Protocols and Extensions - Protocol-Oriented Programming
Learn Swift protocols and extensions: protocol basics, extensions, protocol-oriented programming, and comparison with JavaScript