0% found this document useful (0 votes)
12 views

Classes and Objects

CLASS AND OBJECTS IN ANDROID
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Classes and Objects

CLASS AND OBJECTS IN ANDROID
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Page

Q4.ClasAeA and Objects.

The main purpose of objecive- 0 is to add object


oi untation to the C pogramming kanguage
Clause ae the cenhral feature of objechive -C, thy
Support objct - oriented prog ramming , and are oftun
oalled a Usar - defined types.
The data and method within a cla cuc called
membrs of the cla

CharacteristicA of Objechive -C’


The cla is olefinud in
in two
two diferent
different secthiora namly
@intuface and @implemantatian
Almost everything is in the form of abjects.
Objects reciNe mesages,
meuagek, and are often refemd to al
recievers.

Objects contin instane VaioableA

Objects and instance vaniables have scope


Clawe hide Cun object's implemantatio n
Page

Class Definihond
d olus olafnithon starts wih the kyword
interfaca, followed tby the intuface (clasa) nouna
na
The clau body is en closed by a pair of urly
braces.

Sn objecive-C, all claes aue dleived fom the


base alau called NSObject
J+ is the Supu clo of al Objective l clousel
3+ providles bauic methocs Ilke memory cllocation and
initializaton

example
intuface Box: NSs Object
Tnstonce variables
doubje
double length; ung th of box
bread th ; Bread th of box

propurty
proputy (nonatomic, readwrite ) dlouble hight
end

Allocafing and initi ali zinq abjcts


clau vicdes the bluepnints for objecks
s0 bauically objects
80 are cated fam a cla.
Page

we olecare objecfs with the same sort of alacaration


that we use to oleclare variables of basic type

e Kample ’
Box boxd = [C Borallag init];erate boxs object of
the type Box
Box box 2 =[[ Box alloc] init]; lerate box2 object of
the type Box

dccesing he Data Members ’

The proprtius of objects of a cla cCun be accesed using


the direct membe acceA oprator()

Cxample
import
<Foundation /Foun dation.h)
intefae Box: NSObjeck

double lengthj
olovble breadl th
dou ble heightj
atormic radwrite ) double hight ;
propurty
(double) volu
end

implementahit Box synthasi ze huight j


(id) init
Page

self = [super init)


length =1.0,
bread h= 1.0;
retorn Self;
double) volume

retrn length* breadth * haight ,


3
end

int main

Box boxd = [l Box alloe] init);


Box * box2=[CBox alloc] init];
dovble volume= 0.0
box1. height = s.0j
box2. height =1o.0 ;
volune = Cbox1 volume]
NSLog ( Volume of box1 of" voluma);
volume = [bax2 volumu];
NSLog ( "Volome of box 2: af", veluma);
}

Function ’

Afonction is a group of statements that to geher


a task.
puform
fonction declaration tells com piler about he
onch on's name , retum type and parametes.

The fonchon clefinikon provides the actual bo dy of


funchion.
The fnctha n is called
called a a method

Method Declaration ’
Syratax
-(retorn-type) fonchon _na (argumunt Typ)
arguement Namei joining Argumant l(arguemnt Typ2)
arguement Nama2 joining Arguemnt N: (argunant TypeN)
arguemut NameN;

-(int) max: int) num1 ond Num 2 (int) num 2;

Method Defnihon
Syntax

-(retorn type) foncion- name (argumntType1)


arguemunt Name1 joining Ang umant2 : Carguemunt Type2)
arguemntName 2 joining ArguemuntN CargumuntType)
CuguementTNamaN

body of the funchion


Page

example program

# im port <Foundation Foundaion. h),


inturface Sample Clask NSobject
method ceclaration
-(int) max i (int) num 1 andNuna2i (int) num 2;

end

implemantation Sample
Cla
Zmehad dafnition
-(int)max: (int) numi and Num2int) nwm2

int result
if (num1 > num2)
result = num 1;
else
ruult = wml
hetum rsult

end

int main )

intaz l00
int b= 200;
int net
Sample Clas
net =
Samples ll SampleClau alloc]init
Lsarmple.
NSLoq Q"Max
max : a

val is :
and Numl ib]
%d n" ret);
return 0,
Poge

Log Handlinq
NSLoq metho d isis Used to print logs.
Sy ntax NSLog (Q"hulo );

eramplei # import <Foundation /Foundlati on .h)


int main )

NSLog( " Hello, wortd !") ;


returnOi
3

Debug Lag prints logs in a tive build


Whun we 0ompile and un the program in bp:debug moda,
she output is
is
ur custom
Debuq log, our custom addition qets printed
duing debug only
NSkog is pninted always
uhen we compile and orun our pragranin rlease
mode, the output is ’
NSLog is printed always

You might also like