Tiyasi Acharya
Why OOPs?

• Spaghetti code.
• Mass of tangled jumps and conditional
  branches.
• Not designed for efficiency.
Performance




Time/Size




            Complexity
Performance



                         OOPs

Time/Size




            Complexity
How did OOPs come into the picture?




                           Abstraction




‘Emulate the working of a brain’         ‘Abstract out the details’
Internal Model of a Car
Internal Model of a Car




This is what computer scientists tried to bring into OOPs- ABSTRACTION!
Abstraction
Abstraction
This is how the human brain handles the complexity of the world.
Abstraction
This is how the human brain handles the complexity of the world.




                                          How is this achieved in OOPs?
Inheritance
Process by which 1 object acquires the properties of another object.

                      Super Class: Four-wheeler Vehicles
                      Sub Class: Truck, Car, Bus.
Encapsulation
“Hides” incredible amount of complexity by encapsulating it.
Encapsulation
What was it hiding?
Polymorphism
A feature that allows 1 interface to be used for a general class of actions.

                                Steering Wheel




         Power Steering Wheel              Steering wheel of an Electric car
Another example of Polymorphism

           Dog Smell
Class and Objects
OOPs in Objective C
Class and Objects
A class consists of members: data and methods.


                                     Class Name

@interface Employee: NSObject
{
    int empId;
    char *name;                       Members: Data
}

-(int) empId;
                                    Members: Methods
-(int)lengthOfService:date;

@end
Objects



id date=[ [ Date alloc ] init ];     New Date object allocated

[ date release];
                                      Releasing the variable
Inheritance
                                  Superclass Rectangle.m
                                 #import “Rectangle.h”
   Superclass Rectangle.h
                                 @implementation Rectangle
@interface Rectangle: NSObject
{                                -(id) init
    int length;                  {
    int width;                   if(self=[super init])
}                                    {
                                         length = 8;
-(int) area;                             width = 5;
                                     }
@end                                 return self;
                                 }

                                 -(int) area
                                 {
                                     int area1=length * width;
                                 }
                                 @end
Inheritance
Subclass printAreaOfRectangle.h
#import “Rectangle.h”

@interface printAreaOfRectangle: Rectangle

-(void) printVal;

@end




 Subclass printAreaOfRectangle.m
 #import “printAreaOfRectangle.h”

 @implementation printAreaOfRectangle
 (void) printVal
 {
     NSLog(@”Area = %d”,[self area]);
 }
 @end
Encapsulation

• All the data members are ‘Protected’.
• Data is encapsulated, can be accessed only through
  setter/getter methods.
Polymorphism

main
{
    Window *W = [[Window alloc] init];
    View *V = [[view alloc] init];

    [W flush];
    [V flush];
}
Dynamic binding + Message Passing

              main
              {
                  Window *W = [[Window alloc] init];
                  View *V = [[view alloc] init];

                   [W flush];
                   [V flush];

              id anotherObj = W;                           Dynamic Binding

              [anotherObj flush];                            Message Passing
              }




The flush message is passed to the variable anotherObj that is dynamically bound during runtime.
Summary
The OOPs features that Objective C exhibits are as follows:


  •   Abstraction
  •   Encapsulation
  •   Class
  •   Object
  •   Inheritance
  •   Polymorphism
  •   Message passing
  •   Dynamic Binding
Thank you

OOPS features using Objective C