Object Oriented Programming in Swift

363

In this article, you’ll find a helpful tutorial Object-Oriented Programming. The object-oriented programming is core concepts if you want to learn any of the languages.

In every programming language, three main concepts are mostly used inheritance polymorphism and encapsulation. These are concepts of object-oriented programming.

You will learn in this section

  • Class
  • Initializers
  • Properties
  • Object
  • Inheritance
  • Polymorphism

Alt tag Object Oriented Programming swift

Inheritance

The inheritance is a mechanism in which derived class can access the properties of the base class. The simple like parent-child relationship. Using inheritance you will access the properties and method of the parent class. The reusability is important inheritance concepts. The two common types of inheritance are used which are single inheritance and multiple inheritances.

Encapsulation

The Encapsulation is the fundamental concepts of object-oriented programming swift. In Encapsulation data and member are work on a single unit. This is also called data hiding. The idea behind the encapsulation is that the variable is not used outside the object hide some information internal access of the object.

Polymorphism

The polymorphism is used in a various situation when someone acts in a different form. The polymorphism means the object behave differently according to the data type. Example like a mobile phone where you can call any person to send a message the goal is common communication but approach is different.

Prerequisite

“It will good to cover my previous articles before this. Its help to learn more about IOS development.”

What is class

A class is a set of common properties that differentiated from others by kind. Every class has the blueprint that describes the character and properties of these things like mobile phone class, printer class vehicle class.

Class Example

A class contains two majour componet.

  • Properties: What are the properties of the class.
  • Behavior: The action of the class.

Alt tag Object Oriented Programming swift

Instagram is the real example of object-oriented programming. You will post a picture on that also see the like of the picture. You also can delete the picture. All these things are similar to the object orientation.

what is object

A class is the same as the template or blueprint. When you create an actual tangible instance of the class that instance of the class is called object.

  • Identity = is a name.
  • State = determined by the values of its attributes.
  • Behavior = Determined by how the object acts or reacts to requests messages from other objects.

Alt tag Object Oriented Programming swift

The following is an example of an object. This is a simple example in which create a class.  The define the properties of the class also create the object that accesses the properties.

Initializers

The Initizers function are normally used to initialized value in the data member of the class. when the program is executed. The is also called auto-initialized.

  • Initialized a value
  • To assign the properties to the values
  • “The initialize an instance for a particular data type ‘init()’ is used”. You also paste the argument to the Initializers.

The following example we had created two types of Initializers.  The first Initializers with had no argument second Initializers had tree argument.

Syntax

Example

Designated Initializers

The designated initializers ensure that all the variable is initialized before used. Every class must have one designated initializer. The Designated initializers are the essential initializers for a class. An assigned initializer completely instates all properties presented by that class and calls a suitable superclass initializer to proceed with the introduction procedure up to the superclass chain.

 Convenience Initializers

They should call an assigned initializer of a similar class. These are discretionary and can be utilized for a custom setup. They are composed in a similar style, yet with the comfort modifier set before the init keyword.

Properties

The properties in the object-oriented programming are some sort of data members. You can declare public and private also protected. But if we can declare the public field in a property then anybody can access our field in a program.

but if you can declare the private field in a property then no one can access our field in a program. but always member function declared as a private. The data member always declares as a public.

What is inheritance

The inheritance the second most major element of the object-oriented programming. In the inheritance, the code of existing class uses others to make a new class.

  • It saves time to write code for multiple time.
  • The inheritance means received.
  • In an inheritance new class write such way it can access the member and of the existing class.
  • The class can access the member of the existing class is called the drive class.
  • The existing class also called the parent class.

In this example, I implement inheritance in swift. We had created two classes that are Car and BMW. The car class is parent class that having one property and one method that can use the child class. We had created the object of each class to access the properties. I implemented this example in swift compiler if you have not a mac book.

What is polymorphism

The Polymorphism is another most important feature of object-oriented programming. the ability of the object to behave differently. The concepts of polymorphism use when parent-child relationship involved.

  • Many
  • forms
  • The poly means many and morphism means form.
  • The Code is independent of the interface.
  • The behavior is determinate by the actual class implementing the interface.

The following example we had created three class. The shape class contains the comma properties that is parent class. The triangle class is the child that inherited the properties of the shape class. The last class rectangle that also inherited the properties of the shape class.

The ‘calculate area’ conman function that behaves differently in each class is a real example of polymorphism. The Polymorphism is the ability for the object of different classes related by inheritance to respond differently to the function class.

Conclusion

In this post, I had explained all the concepts of object-oriented programming swift with an example. I had explained what is inheritance where we can use it. How can create a class? using class how to access the object of the class. The different between initializer and properties. If you want to learn swift these concept are commonly used. for more object-oriented click here.