oops features using objective c

Post on 22-Apr-2015

5.160 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

There is an explanation of why OOPs and then there is detailing on how OOPs is used by Objective C.

TRANSCRIPT

Tiyasi Acharya

Why OOPs?

• Spaghetti code.• Mass of tangled jumps and conditional

branches.• Not designed for efficiency.

Performance

Complexity

Time/Size

Performance

Complexity

Time/Size

OOPs

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

AbstractionThis is how the human brain handles the complexity of the world.

AbstractionThis is how the human brain handles the complexity of the world.

How is this achieved in OOPs?

Inheritance

Super Class: Four-wheeler Vehicles

Sub Class: Truck, Car, Bus.

Process by which 1 object acquires the properties of another object.

Encapsulation

“Hides” incredible amount of complexity by encapsulating it.

Encapsulation

What was it hiding?

Polymorphism

Steering Wheel

Power Steering Wheel Steering wheel of an Electric car

A feature that allows 1 interface to be used for a general class of actions.

Another example of Polymorphism

Dog Smell

Class and Objects

OOPs in Objective C

Class and ObjectsA class consists of members: data and methods.

@interface Employee: NSObject{

int empId;char *name;

}

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

@end

Class Name

Members: Data

Members: Methods

Objects

id date=[ [ Date alloc ] init ];

[ date release];

New Date object allocated

Releasing the variable

Inheritance

@interface Rectangle: NSObject{

int length;int width;

}

-(int) area;

@end

Superclass Rectangle.h #import “Rectangle.h”

@implementation Rectangle -(id) init{

if(self=[super init]){

length = 8;width = 5;

}return self;

}

-(int) area{

int area1=length * width;}@end

Superclass Rectangle.m

Inheritance

Subclass printAreaOfRectangle.h

Subclass printAreaOfRectangle.m

#import “Rectangle.h”

@interface printAreaOfRectangle: Rectangle

-(void) printVal;

@end

#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;

[anotherObj flush]; }

Dynamic Binding

Message Passing

The flush message is passed to the variable anotherObj that is dynamically bound during runtime.

Summary

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

The OOPs features that Objective C exhibits are as follows:

Thank you

top related