Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Wednesday, 7 December 2016

Let's play with Classes and Objects... :)


Hello Friends,

So far we have seen what is iOS , why to choose iOS developer as a profession, objective c overview also we made some new friends from objective c world i.e objective c syntaxes. So our journey has already started and we are enjoying the the path to destination .

As we are now familiar with the required syntax of objective c, I think now it's time to create our first class . We will be discussing about how to create classes , how to proceed for the class creation, how to decide the structure of class etc. So let's start...!!



Before going for coding , I would like to share my experience about dealing with classes. Like most of us, I have completed my Engineering in Computer Science. But being honest, till the end completion of four years of my engineering , I didn't know how to relate classes and objects in our real life scenarios. All I used to do is only study some popular example from the books and use them everywhere. So when I started learning OOP , it was very hard for to understand the use of classes and objects in real IT industry working. I used to think like how can we build a softwares or applications by just creating their blueprint on paper?? How can I describe a real life object in terms of coding? I have faced all such and many more doubts, so I wanted to clarify them as somebody may find it helpful at the earlier stage .




When we proceed for converting real life objects into programming , always sit back for few minutes and think about all the possible features that object can have in real life. For example , if we have to represent  Football in terms of coding. Start by writing down all the possible feature a football can have. Few of them can be color, size,manufacturer company, weight etc and the functions it can have will consists bouncing behaviour that is a football bounces.
Now we are clear about the properties and functionalities of the football object, we will proceed to create its object. Always create a class to represent such real life entity (Football in our case). So we will create a class whose name will be Football . We know that class is a collection of properties and functions, so we will create variables and constants of all the features/properties of Football and finally we will describe it's behaviour by creating a method called bounce.

You can see this thinking process in the above figure.

I hope you have got some idea about how to proceed for designing class structure.Now lets dive into coding . For better understanding we will create Student class and its objects.





Now the very first step we will do is , write down all the possible  features and functions of a student.
The list will look somewhat like this:

Properties:

1. Name
2. Roll Number
3. Address
4. Divison

Functions:

1. Do Homework
2. Write an examination
3. Play

We are now all set with the blueprint, so we can proceed for the coding.

First we will design the class.

Class declaration:


#import <Foundation/Foundation.h>//we will see about this in coming posts.For now you can ignore it

@interface Student : NSObject 
{
    //Properties
    int rollNumber;
    NSString *name;
    NSString *address;

}

//Functions (more appropriately Methods)
-(void)writeExamination;
-(void)doHomework;
-(void)play;

@end

You can now easily correlate the Student from real life with the class we have just designed.
This is just the class design, yet we will have to write its implementation.

Class implementation:



@implementation Student

//This is like constructor in other languages.
- (instancetype)initWithName:(NSString*)studentName rollNumber:(int)studentRollNumber andAddress:(NSString*)studentAddress
{
    self = [super init];
    if (self) {
        rollNumber = studentRollNumber;
        name = studentName;
        address = studentAddress;
    }
    return self;
}

-(void)writeExamination{
    //your logic here
}
-(void)doHomework{
    //your logic here
}
-(void)play{
    //your logic here
}

@end

The implementation will explain how exactly the methods are going to work. For example we can provide steps for writing an examination in writeExamination method.

We are ready with the blueprint and the implementation , so its time to use it with the help of an Object as we know class is nothing without its object.


int main(int argc, const char * argv[]) {
    //Here we are creating object of the student class
    Student *jenna = [[Student alloc]initWithName:@"Jenna" rollNumber:1 andAddress:@"Pune"];
    Student *john = [[Student alloc]initWithName:@"John" rollNumber:2 andAddress:@"Pune"];
  
/*
 alloc - is like malloc in C++, it allocates memory for   object.
 init - this method initialises the properties of a class with their default values.
 */
    
    [jenna doHomework];//Method calling with the help of object
    [jenna play];
    
    [john writeExamination];
    [john doHomework];
    [john play];
    
    return 0;
}


I have written comments wherever necessary. Yes, there are few things here which you will not find in any other programming language like alloc , NSObject , we are going to see all these in the coming posts. As this post was specifically for understanding classes and objects in action, so I have not explained those things here .

See you in the next post, till then Happy Coding..!!


Share:

Friday, 2 December 2016

OOP (OBJECT ORIENTED PROGRAMMIMG) CONCEPTS

Hello friends,

Today I am going to discuss about the most used programming paradigm i.e Object Oriented Programming (OOP).

There are basically two types of programming paradigms:

1) Procedure Oriented Programming
2) Object Oriented Programming

As this post is regarding OOP concepts, I will not discuss more about the procedure oriented programming, so below is just a simple comparison between both of theme.

Procedure Oriented Programming VS  Object Oriented Programming

Fig. OOP V/S PROCEDURAL PROGRAMMING

Now lets move on to the basic OOP concepts. But before that lets know about classes and objects.

What is a Class and Object..???

A class is used in object-oriented programming to describe one or more objects. It serves as a template for creating, or instantiating, specific objects within a program.

In short we can say that a Class is a blueprint for object creation. Class has no meaning if we don't have its' object.

To understand it better, lets take an example from our real life scenario.
Suppose an architect has to build a house for one of his clients. What will he do first is, he will create a blue print of the plan. Once he finalises all things ,like number of rooms, area for per room, terrace section, garden etc. the workers will start executing  the same plan in reality. It means the plan is blue print and the house which is going to be build is the object of that blue print. Unless and until workers build the house , that plan has no meaning. Again the architect can use the same plan to construct number of houses .

So in short, the plan is a Class and the house is an object of that Class.

Fig. CLASSES AND OBJECT IN REAL WORLD

The book definitions for class and object are :

Class: A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.

Object : Object is an instance of a class.

Now lets see the most important OOP concepts

1) Abstraction: 


Abstraction can be defined as the way of presenting necessary things by hiding unnecessary things.

For example consider a mobile phone. As a End user we have been provided a user interface like back button, app icons to launch app, call button,valume keys etc. We just press one of these to perform certain task. Lets say we want to make a call, we will just select a contact and press call button. This will do our job of calling someone. But we don't know how exactly the call is connected to the other person, how the data is transmitted etc. So these are the unnecessary informations from the user perspective and thats why these are hidden.







                                       

                                                             Fig. ABSTRACTION



Another example is restaurant. When we go to restaurant we are given a menu card. We order something from the menu card and we are served with the dishes.
Here the dish is the necessary thing that the customer is interested in. The cooking procedure of the dish is not necessary for the customer. So its the abstraction.


                          

Fig. ABSTRACTION

2) Inheritance: 

Inheritance means acquiring properties of parent class. The derived class has all the properties of base class plus its own properties.

For example , lets say in a Company there are employees with different roles such as Marketing Executive, Developer, Cashier etc. They all are employees but in addition to that they have some specific roles which they have to perform . So here the employee is the base class and the Marketing Executive, Developer, Cashier etc are the derived classes .


Fig. INHERITANCE

3) Polymorphism: 

Polymorphism is the ability to take more than one form.

For example Consider a human being. Every human being has some roles to play. If its a male then he may be a son, husband, a father, an employee. If its female then she may be a daughter, a sister, a wife,a mother, an employee. So its the ability of a human being to take more than one role, and thats what we call Polymorphism in programming terminology.

Another example in terms of programming is addition operator. The + operator can be used for adding numbers and same operator can be used to concate two strings.


Fig. POLYMORPHISM

4) Encapsulation: 

It means wrapping up of data into single entity (class).

Example: We all have taken some kind of capsule at least once in a life. This capsule is nothing but the encapsulation. That capsule may have various chemicals but they all are wrapped together in a single entity called Capsule.

Another example is any car. The car has many parts like wheels,stearing, seats etc. but they all are bounded together to make a single car.

            

Fig. ENCAPSULATION





Share: