Friday, September 21, 2018

Swift 4 Introduction Series 1.17 - Basic of Concept Class and Objects

Basic of Concept Class and Objects

We can also organize properties and method into a class. In addition, class allow us to organize the data objects similar to our real world classification.
Let's examine a real world object of transport vehicle. Since transport vehicle carries people or goods from point A to point B. We can surmise that the common characteristics are speed and load. However, transport vehicle can be further classified into various mode of transport such as land, air, sea and amphibious transport.


Each mode of transport can be further classified into different type of transport vehicle. Each type of transport vehicle can be further classified into different make and different manufacturer.


The chart below just simplified the description above.




While class is similar to structure, there is one feature that structure are not able to do it. Using class, we are able to  create a 2nd level subclass that inherit all the properties and method created in the upper class. Please also note that for class, we need to initialize all the properties.


In the example above, we can create a class of transport vehicle with properties of speed and load. In the next level, we can inherit all the features of the upper class and add property such as the mode of transport. We can create next level of subclass by differentiating different types of  vehicle.


The table below briefly explore what are the properties for each class. The properties that are common will be kept at higher level class. Properties that are more unique to a class will be added at the appropriate class.


Class
Parent Class
Properties
Transport Vehicle
NA
Speed
Maximum Load
No of Passenger
Land Vehicle
Transport Vehicle
Mode of Transport
Power by Gas/Electric/ Hybrid?
Air Vehicle
Transport Vehicle
Mode of Transport
Power by Gas/Electric/ Hybrid?
Sea Vehicle
Transport Vehicle
Mode of Transport
Power by Gas/Electric/ Hybrid?
Car
Land Vehicle
Number of Wheel
Number of Doors
Type of engine
Van
Land Vehicle
Number of Wheel
Number of Doors
Type of engine
Bus
Land Vehicle
Number of Wheel
Number of Doors
Type of engine
Truck
Land Vehicle
Number of Wheel
Number of Doors
Type of engine
X Brand
Car
Color
Price
Y Brand
Car
Color
Price


Beside transport vehicle, we can classified living things into mammal, reptile etc. Mammal can be further classified into ape, cats etc.


When we design programming objects, we put the most common features or variables at the base class. When we classified the objects into more specific class, then we introduce specific features that are unique to the subclass.


When we implement class definition, we define the base class as transport vehicle with some properties and methods. Then we use subclass to define the second level of classification. The subclass will be classified into different mode of transport such as land vehicle, sea vehicle or air vehicle.


When we implement subclass, we will inherit all the properties or method in the base class. This means that we do not need to define the properties or rewrite the methods that is in the base class.


We can also add additional functions/methods that are specific to that subclass. If we continue further subclassing, we just add on all the inherited properties and methods.


However, during subclassing certain function will change. For example, the move vehicle method cannot be the same for cars and plane. We can override the methods in the subclass. When we override the methods, the function name and the parameters must be the same.  

***

No comments:

Post a Comment