Introduction
This is part Eight of the "Swift Programming - Zero To Hero" series. In this article we will learn about Object Oriented Programming in Swift. Please kindly follow my previous article series in Swift from the below link,
- Swift Programming - Zero To Hero - Part One
- Swift Programming - Zero To Hero - Part Two
- Swift Programming - Zero To Hero - Part Three
- Swift Programming - Zero To Hero - Part Four
- Swift Programming - Zero To Hero - Part Five
- Swift Programming - Zero To Hero - Part Six
- Swift Programming - Zero To Hero - Part Seven
The following is the flow of this write-up.
- What is OOP
- Encapsulation
- Polymorphism
- Inheritance
- Classes and Objects
- Properties
- Methods
- Inheritance
- Overriding
- Base class
- Conclusion
What is OOP
Object Oriented Programming (OOP) is a programming technique where you specify various objects and define the interaction among them to implement progrm logic. Classes and Objects are the two main elements of OOP. Objects are mostly the instances of the classes, are used to interact with one another to design applications and computer program.
Encapsulation
Encapsulation is a protective mechanism by which members of a class; i.e., methods and variables, are prevented from begin accessed by members of another classes.
Polymorphism
Ability to take more than one form is know as Polymorphism. One Method behaves in different forms.
Inheritance
One class acquires the characteristics of an existing class is known as Inheritance. Objects of one class can derive part of their behavior from another (base or parent) class.
Classes and Objects
In object-oriented programming, a class is used for creating objects, member variables and implementations of member functions and methods. In other words, a class is like a blueprint, it defines the data and behavior of a type.
Syntax
- class ClassName {
- }
- class Student {
- }
- var student = Student()
Properties
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. In other words,
The Student class has a TotalStudents property that has a default value of 50 as Strongly Type values of Integer
Classes and instances can have associated values named properties.
Example
- class Student{
- var TotalStudents: Int = 50
- }
Methods
Methods are similar to functions, they belongs to classes or objects and usually expresses the verbs of the objects/class.Methods add behaviour to classes and instances.
Inheritance
A class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass.
Overriding
When two or more methods (functions) have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class is called method Overriding.
Base class
A class that does not inherit from another class is called a base class.
Conclusion
In this article we have learned Object Oriented Programming in a theoretical manner. Hope this was very useful. In my next article, we will see more examples on OOP with programs in Swift.

Sagar Pandurang KapPosted Dec 13, 2017, 12:17 AM
Yes it was so much helpful.It brush up my OOP's concepts.Thanks.keep Posting..