In this tutorial we are going to look at it into Object Oriented Concepts in swift language. As we all know in the modern programming languages uses OOPS, because it is very easy to design our code base layer.
1) The Basics of Object
To understand clearly let's start create new xcode project. To create new project open Xcode got to File/New/Project... then select Single View Application click next,
Name your project and choose language as Swift then proceed next.
After creating new project let's create new class as Vehicle, to create new class go to File/New File/iOS/Source/Cocoa Touch Class then name your class and choose NSObject as it's super class.
Now open Vehicle class and define the following characteristics of it and a initializer method.
Now let's see how to assign the characteristics and accessing them by its Object . Write the following code in ViewController.swift class.
After adding this code to ViewController.swift, run the project you can see the log info as below shown.
So now let's see in detail how an Object was created and added characteristics of it.
Creating an Object is nothing but preserving some memory space to the particular class and where we can define some properties of that particular class. Here in this example we have totally three objects say byke, bicycle and car of class Vehicle, Where each object holds the characteristics of different vehicles.
Now see, how to add instance and class methods associated to Vehicle class. We have the following methods/functions in Vehicle class.
2) Inheritance:
Before going to inheritance term you should know about the following terms,
Super Class - The class contains properties and methods to inherit from other classes itself.
Sub Class - The class contains properties and methods along with it's super class.
Now inheritance - Is ability of adopting features of super class to sub class.
For example: Take Vehicle as Super class and Car as sub class to the Vehicle, which means the Car has some basic features and characteristics of Vehicle but Vehicle doesn't has Car's behavior or characteristics.
To understand clearly let's create class as Car whose super class is Vehicle as shown below
In the inheritance we can directly adopt the methods of super class or else we can override the super class methods in sub class as shown below. The code states that the Car class is overrides the goForward() -> Bool function.
You can download practice project from here.
1) The Basics of Object
Note: In general what is an Object? Object is a material thing which can be seen and touchable.
To understand more about Object, let's consider a realtime example. Here in this tutorial consider a vehicle as our Object.
Vehicle 1
Vehicle 2
Vehicle 3
As you see we have different vehicles with their relative characteristics. In our programming language how do we define characteristics means by declaring properties to the object of a class.
Let's see what all are the characteristics vehicle has?
In general we can see the following characteristics of a vehicle
- Number of wheels
- Model
- Brand
- Year of manufacture
- Color
- Resource to drive the vehicle
To understand clearly let's start create new xcode project. To create new project open Xcode got to File/New/Project... then select Single View Application click next,
Name your project and choose language as Swift then proceed next.
After creating new project let's create new class as Vehicle, to create new class go to File/New File/iOS/Source/Cocoa Touch Class then name your class and choose NSObject as it's super class.
Now open Vehicle class and define the following characteristics of it and a initializer method.
class Vehicle: NSObject {var numberOfWheels = 0var modelName = "null"var brandName = "null"var modelYear = 0var powerSource = "null"var color = UIColor.blackColor()init(numberOfWheel aWheels:Int, modelName aModelName:String, brandName aBrandName:String, modelYear aModelYear:Int, powerSource aPowerSource:String, color aColor:UIColor) {super.init()self.numberOfWheels = aWheelsself.modelName = aModelNameself.brandName = aBrandNameself.modelYear = aModelYearself.powerSource = aPowerSourceself.color = aColor}}
Now let's see how to assign the characteristics and accessing them by its Object . Write the following code in ViewController.swift class.
let byke = Vehicle(numberOfWheel: 2, modelName: "FZ", brandName: "Yamaha", modelYear: 2016, powerSource: "Petrol", color: UIColor.blackColor())print("Brand of byke is \(byke.brandName)");let bicycle = Vehicle(numberOfWheel: 2, modelName: "i Series", brandName: "BMW", modelYear: 2012, powerSource: "Man Power", color: UIColor.blackColor())print("Bicycle was made in the year of \(bicycle.modelYear)");let car = Vehicle(numberOfWheel: 4, modelName: "i 8", brandName: "BMW", modelYear: 2008, powerSource: "Petrol", color: UIColor.grayColor())print("Car model is \(car.modelName)");
After adding this code to ViewController.swift, run the project you can see the log info as below shown.
So now let's see in detail how an Object was created and added characteristics of it.
Creating an Object is nothing but preserving some memory space to the particular class and where we can define some properties of that particular class. Here in this example we have totally three objects say byke, bicycle and car of class Vehicle, Where each object holds the characteristics of different vehicles.
Now see, how to add instance and class methods associated to Vehicle class. We have the following methods/functions in Vehicle class.
Here we have two instance methods and one class method. To declare class method we need to add class keyword and no need for instance method. To call instance method we have to create a object and to call class method no need of creating object.func goForward() -> Bool {return true}func goBackward() -> Bool {return false}class func stopMoving() -> Bool {return false}
let car = Vehicle(numberOfWheel: 4, modelName: "i 8", brandName: "BMW", modelYear: 2008, powerSource: "Petrol", color: UIColor.grayColor())print("Car model is \(car.modelName)");if car.goBackward() {print("Car is going back")}if Vehicle.stopMoving() {print("Stop the Car")}
2) Inheritance:
Before going to inheritance term you should know about the following terms,
Super Class - The class contains properties and methods to inherit from other classes itself.
Sub Class - The class contains properties and methods along with it's super class.
Now inheritance - Is ability of adopting features of super class to sub class.
For example: Take Vehicle as Super class and Car as sub class to the Vehicle, which means the Car has some basic features and characteristics of Vehicle but Vehicle doesn't has Car's behavior or characteristics.
To understand clearly let's create class as Car whose super class is Vehicle as shown below
In the above statement you can see the init method, the Car class doesn't has init method but it is inherits from Vehicle class.//Inheritancelet c = Car(numberOfWheel: 4, modelName: "i 8", brandName: "BMW", modelYear: 2008, powerSource: "Petrol", color: UIColor.grayColor())print(c.brandName)
In the inheritance we can directly adopt the methods of super class or else we can override the super class methods in sub class as shown below. The code states that the Car class is overrides the goForward() -> Bool function.
class Car: Vehicle {override func goForward() -> Bool {return false}}
You can download practice project from here.
0 Comments