Swift Programming - Zero To Hero - Part Eight

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,
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  
  1. class ClassName {  
  2.   
  3. }  
 Example
  1. class Student {  
  2. }  
The definition above creates an empty class named Student. The only thing it can do is create new student objects: 
  1. var student = Student()  
In the example above student is an instance of the Student class. 
 
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,  
Classes and instances can have associated values named properties.
 
Example 
  1. class Student{  
  2.     var TotalStudents: Int = 50  
  3. }  
The Student class has a TotalStudents property that has a default value of 50 as Strongly Type values of Integer
 
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.