Anda di halaman 1dari 56

FEATURES OF JAVA :-

1. SIMPLE:- Java is simple programming language learning and practising java is easy
because o its resemblance !it" # and #$$ also comple% topics o # and #$$ are
eliminated in java.
&.'(JE#)-'*I+)E,:- unli-e #$$. java is a purely object - orinted programming language .
java programs use objects and classes.
/.,IS)*I(0)E,:-Inormation is distributed on various computers on a net!or-. using
java .!e can !rite programs !"ic" capture inormation and distribute
it to clients
1.*'(0S):-Java programs !ill not cras" easily because o its e%ception "andling and its
memory management eatures.
2.SE#0*E:-Java enables t"e construction o virus ree and tamper -ree system.
*obust 314 Memory allocation -J5M6s class loader subsystem
3&4Memory deallocation -J5M6s garbage collector
7.8*#9I)#)0*E -+E0)*8L:-Java6s byte code is not mac"ine dependent. it can be run on
any mac"ine !it" any processor and !it" any ':S operating system.
;.P'*)8(LE:-Java programs give same results on all mac"ines
<.I+)E*P*E)E,:-Java programs are compiled to generate t"e byte- code .)"is byte code
can be do!nloaded. and interpreted by t"e interpreted in J5M.=I+ J5M 6t"e interpreted
convert t"e class ile into mac"ine code instruction =. but is slo!
>.9I?9)-PE*@'*M8+#E:-8long !it" interpreter t"ere is JI) 3just in time4compiler in J5M
!"ic" en"ance t"e speed o e%ecution JI) compiler is rote o increase t"e program
-e%ecution JI) compiler converts byte code into mac"ine Auic-ly
1B.M0LI) )9E*,E,:-!e can create several process in java . called 6t"reads6. t"is is an
essential eature to design server side programs
11. ,ynamic :- !e can develop programs in java !"ic" dynamically interact !it" t"e user
on internet 3e%ample :-applets4. ,ynamic means runtime
JVM Architecture
In J5M class loader sub system does t"e ollo!ing tas-:
314 It loads .class ile into memory 3*8M4
3&4It !ill veriy t"e (yte code Instructions o .class ile.
3/4t"en .it !ill allot t"e reCuired memory or t"e java program
314 )"is memory divide into 2 parts. call into runtime data areas. )"ey are method area:-
t"is memory bloc- stores class code and met"od code. Heap:- objects are created on "eap.
Java stacks:- )"ese are t"e places !"ere java met"ods are t"e e%ecuted.
Pc reisters:- program counter registers t"ee register stores memory address o t"e ne%t
instruction to be e%ecuted by t"e microprocessor. !ative method:- )"ese are t"e areas
!"ere native met"ods 3# or #$$ unctions4 are e%ecuted.
!OTE:- "ative method #i$raries 3# or c$$ "eader iles4:- ae copied into J5M by native
met"od interace !"ic" is a program.
324 E%ecutio" E"i"e:- contains Interpreter D JI) complier !"ic" converts t"e byte code
into mac"ine code so t"at t"e process !ill e%ecute and display t"e results.
HOTSPOT:- is a bloc- o statements let or e%ecution by JI) complier remaining
statements leaves or Interpreter.
&omme"ts:- #omments represents description o all eatures o a program comments.
Increase readability o a program t"ere are / types comments in java.
#omments are non e%ecutable statements.
'() Si"#e #i"e comme"ts:- )"ese are called single line comments and denotes
to 3::4
'*) Mu#ti #i"e comme"ts:- )"ese are called multi line comments and denotes
to3:EF.E:4.
'+) Severa# #i"e or ,ava docume"t comme"ts:- )"ese are called java
document is useul to
#reate an 8PI document rom a java program.
S-"ta% o. comme"ts:-
:EE )"is is description about
)"e classE:
#lass 888
G
:EE )"is is description about
)"e met"od E:
5oid met"od34
G
IIA:- H"at is an 8PI documentI
*eply:- 8PI 3application programming interace4 document is a "tml ile t"at contain
description o all t"e eatures o a sot!are. a product or a tec"nology.
Javadoc compiler create an APT document.
@irst program:-
:: program
class irst
G
Public static void main3String args JK4
G
System.out.println3L!elcome to tec"indiaM4
N
N
public static void main3String argsJK4
He "ave !rite t!o types:-
static public void main3String argsJK4
and
static public void main3String %JK4
argsJK-- it is an array
public static void main3String argsJK4
)"e values passed to a met"od are called arume"ts.
Void means does not "ave any value.
Main 34 met"od does not return any values.
O )o call a met"od in java:-
Step:-(
create an object to t"e class
synta%:- classname obj O ne! classname34P
e%p:- @irst O ne! @irst34P

object

'bject means memory to store data and calls t"e met"od34
Step:-* :- objname.met"odname 3obj.met"od4
.main34P
J5M !ill create object ater main met"od call
Static:-
Is a met"od t"at can be call and e%ecuted !it"out using object.
Pu$#ic:-
Is a members to outside t"e programs global scope.
Pri"t:-
Print34 met"od belong to PrintStream class
S-stem:- is a class.
out:- is a variable
pri"t:- is a met"od
ESystem.out is stands or mo"itor
ESystem.in is stands or ke-$oard
:: program to ind sum o t!o numbers
#lass Sum
G
Public static void main3String argsJK4
G
::vars

int %.yP
::store data

%O1BP
yO&2P
::add t"en
int Q O %$yP
::display result
System.out.print3=sum o t!o nosO=$Q4P
N
N
pri"t') method:- ,isplay t"e output and t"en retain3-eeps4 t"e cursor in t"e same line.
pri"t#"') method:- also display t"e output and t"en t"ro!s t"e cursor into t"e ne%t line.
IIA:- !"at is J*E 3java runtime environment4
ans:- J*E O J5M $ java library

!ote:-
314 Every java statements s"ould end !it" 3P4
3&4 Java is case sensitive programming language.
bac-slas"3 R 4 in java:-
escape seCuence meaning
Rn ne! line
Rt tab -ey
Rr enter -ey
Rb bac- space
R R R
R= =
R6 6
!ami" co"ve"tio" i" ,ava:-
314 pac-age names in java are !ritten in all small letters
e%ample:- java.a!t
java.io
java%.s!ing
3&4 Eac" !ord o class names and interace names start !it" capital letter.
e%ample:- String
,ataInputStream
8ctionListener
3/4 Met"od names start !it" a small letter. t"en eac" !ord start !it" a capital
println34. readLine34. read34. get+umberInstance34
314 variable names also ollo! t"e above rule
e%ample:- age. empname. employeeS+etSSal
324 constants s"ould be !ritten using by all capital letters
e%ample:- PI. M8TS58L0E.@'+).('L,
E8 constants represents i%ed value.
374 8ll -ey!ord s"ould be !ritten in all small letters
e%ample:- public. void. import
IIA:- !"ic" is t"e deault pac-age automatic import to your programI
ans:- java.lang pac-age is a deault pac-age import into every java program.
/0ATAT1PES/
,atatypes:- 8 datatype represents t"e type o data stored into memory3variable4.
,ata)ype Memory SiQe Min. and Ma%.
314 byte 1 (yte -1&< to $1&;
3&4 s"ort & (ytes -/&;7< to
$/&;7;
3/4 int 1 (ytes -&11;1</71<
to $&11;1</71;
314 long < (ytes ->.&%1B1< to
$>.&%1B1<
-------------------------------------------------------------------------------------------------------
-----
B as consider a positive number.
E%ample:- byte rnoO1BP long % O 12BL
2itera#:- )"e value being stored into t"e variable.
.#oat datat-ppes:- )"ese ,atatypes store numbers !it" decimal li-e /.1112>. -1.2.1BB.B.
-B.B etc.
i"t datat-pes:- )"ese datatypes store integer no. and t"e number do not "ave raction
part or decimal part
li-e 1BB. -1. ;2BBB.1&2. B etc.
,atatype MemorysiQe Min. and Ma%.
314 loat 1 (ytes 1.1e12 to /.1e/<
3&4 double < (ytes 1.>e./&1 to 1.<e/B<
E%ample:- loat pi O /.11&@P
IIA:- !"at is t"e dierence bet!een loat and decimal
ans:- loat datatype represents upto ; digits ater decimal points. accurately.
double datatype represents upto 12 digits ater decimal point accurately.
&har datat-pes:- )"is datatype represents a single c"aracter li-e 686.a.E.I.==
,atatype Memory siQe Min. D Ma%.
314 c"ar & bytes B to 722/2
e%ample:- c"ar c" O 6%6P
Stri" 0atat-pes:- is represents a group o c"aracters li-e:- name
String name O =Inet solv=P
String str O ne! String3=8 String=4P
3oo#ea" 0ata T-pes:- )"is datatype can represents only true values.
e%ample:- boolean responseOtrueP
4A c#ass is user de.i"ed 0atat-pes5
IIA:- H"at is 0nicodeI

8ns:- 0nicode is a stadard to include t"e alp"abates rom all t"e "uman languages into
t"e c"aracter set o java.0nicode system uses & bytes represents a single c"aracter.
Operator
Operator:- 8n operator is a symbol t"at perorms an operations.
U"iar- operator:- I an operator acts on single operands. it is called uniary operator.
3i"ar- operator:- I an operator acts on t!o operands. it is called binary operator.
Tr"ar- operator:- I an operator acts on t"ree operands. it is called trnary operator.
Operators i" JAVA:-
314 8rit"metic operators:- li-e as $ - E U
3&4 0nary operators:- li-e as - $$ --
3/4 8ssignment operators:- li-e as O $O -O :O UO
314 *elational operators:- li-e as V VO W WO OO XO
324 Logical operators:- li-e as DD YY X
374 (oolean operators:- D Y X
3;4 (it!ise operators:-
3a4 (it!ise compliment: Z
3b4 (it!ise and : D
3c4 (it!ise or: Y
3d4 (it!ise %or: [
3e4 (it!ise lets"it: VV
34 (it!ise rig"ts"it: WW
3g4 (it!ise Qero-ill rig"ts"it: WWW
3<4 )ernary operators3#onditional operators4 I :
3>4 ,ot operator or member operator3.4 :
3a4 )o reer to a class in a pac-age: java.util.,ate. java.lang.pac-age
3b4 )o reer to a met"od in a class Mat".po!34. emp.getSal34
3c4 )o reer to a variable in a class: Employee.+ame. emp.no
31B4 6"sta"ce o. Operator:- )o test !"et"er an object belongs to a class.
e%ample:- boolean % O emp instance o Employee
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOO
314 Arithmatic Operators:- )"ese operators perroms basic arit"matic operation.
'perator Meaning suppose E%ample
*esult
$ 8ddition aO1/. bO2 a$b
1<
- Sustraction a-b
<
E Multiplication aEb
72
: ,ivision a:b
&.7
U Modulus operator aUb
/
3&4 U"ar- Operators:- )"is operator negates t"e value o a variable.

int % O2P
System.out.println3-%4P
6"creme"t U"ar-:- )"is operator increases t"e value o a variable by 1.
int % O1P
$$% O &P
%$$ O /P
%O%$1 O1P
3$$4 beore t"e variable is called pre-i"cerme"atio"5
3$$4 ater t"e variable is called post-i"creme"tatio"5
4In a pre-incrementation. incrementation is done irst any ot"er operations are done at t"e
end.
4In post incrementation all t"e ot"er operations are done irst.Incrementation is done at t"e
end.
e%ample:- int % O1P int % O1P
System.out.println3%4P System.out.println3%4P
System.out.println3$$%4P System.out.println3%$$4P
System.out.println3%4P System.out.println3%4P
output:- 1. &. & 'utput:- 1. 1. &

aO1P bO& aO1PbO&P
aO$$bP aOb$$P
aO/.bO/ aO&.bO/
Ans:- H"at is t"e value o t"e olo!ing E%pression given aO;P
ans:- $$aEa$$P
3a4 1> 3b4 71 3c4 27 3d4 ;& 3e4 none o above
0ecereme"t U"ar-:- 3--4 t"is opeartor decreases t"e value o variable by 1.
int % O 1P
--% B
%-- -1
%O%-1 -&
Hriting 3--4 beore t"e variable is called pre-decrementation.
Hriting 3--4 ater t"e variable is called post-decrementation
3/4 Assie"me"t operators:- )"is operator stores t"e rig"t "and side value into let "and
side variable.
+ote:- e%panded notation compact notation
salOsal$2BBP sal$O2BBP
pOp-1B p-O1B
iOiE- iEO-
%O%:1B.2 %:O1B.2
jOjU& jUO&
314 Re#atio"a# Operators:- )"ese operators compare t!o Cuantities .t"ese operators are
used in construction o simple condition.
W -----W greater t"an
V -----W less t"an
WO -----W greater t"an or eCual
VO -----W less t"an or eCual
XO -----W not eCual to
i 3aVb4 statement is e%cutedP
--------------------
i 3%XO14 --------------------P
i 3salWO2BBB.BB4------------- P
324 2oica# Operators:- )"ese operators are used in construction o compou"d co"ditio"
is a combination o & or more
simple conditions.
DD --- and
YY ---- or
X ----- not
i3aWbYYbWcYYcWa4 i anyone condition is true t"an statement is e%ecutedP
i.'%77( 88 -77*) i. $oth co"ditio" is true tha" stateme"t is e%ecuted9
i 3X3%WO1BB44 % is less t"an 1BB condition is true t"an statement is e%ecutedP
374 3oo#ea" Operators:- )"ese operators act upon boolean data and t"ey return boolean
type result.

3;4 3it:ise Operators:- )"ese operators act upon individual bts o numbers.bits means
binary digits 3B or 14.
&o"vert ;< i"to $i"ar- diits is BB1B11B1 beacuse 12:&
*ule:- divide t"e number successively by & and ta-e t"e remonders
rom bottom to top.
co"vert ==(=((= i"to decima# "o5 s-stem
*ule:- multiply eac" digit by t"e po!ers o & and ta-e t"e some
o t"e products
B B 1 B 1
1 B 1
&po!; &po!7 &po!2 &po!1 &po!/
&po!& &po!1 &po!B
-------------------------------------------------------------------------------
------------------------
/& B <
1 B 1

(it!ise compliment 'perator:-3Z4 is tilled symbol. )"is operator gives t"e compliment orm
o a number.
e%p:- int % O1BP J 1B comliment o -11K
Z T -W O -11 Jone value increase !it" negative
signK
(it!ise and3D4:- )"is operaotr perorms anding operation on individual bits o numbers.
!e use Truth ta$#e:- 8 relation bet!een a table t"at represents relation
bet!een input bits and output bits.


e%ample:- %O1B. yO11. %DyOI
%O B B B B1 B 1 B
yO B B B B 1 B 1 1
-------------------------
B B B B 1 B 1 B O 1B 8ns
(it!ise or 3Y4 'perator:- )"is operator perorms oring operation on individual bits o
numbers.
e%ample:- %O1B. yO11. %YyOI
%O B B B B1 B 1 B
yO B B B B 1 B 1 1
-------------------------
B B B B 1 B 1 1 O 11 8ns
(it!ise %or 3[4 'perator:- means e%clusive or:- )"is operator perorms %oring operations
on t"e individual bits o numbers.
e%ample:- %O1B. yO11. %[yOI
%O B B B B1 B 1 B
yO B B B B 1 B 1 1
-------------------------
B B B B B B B 1 O 1 8ns 3,epend on 'dd no.14
3[ is a cap or called caret or circumle%4
(it!ise lets"it3VV4 'perator:- )"is operator s"its t"e bits o no. to!ards #e.t speciy no.
o times.
int % O1BP % VV & O I
/&$< O 1B 8ns.
(it!ise rig"ts"it3WW4 operator:- )"is operator s"its t"e bits o no. to!ards riht speciy
no. o times.
int % O1BP % WW & OI
% O & ans.
::program bit!ise operators
class (its
G
public static void main3String argsJK4
G
byte %.yP
%O1BP
yO11P
System.out.println3=Z%O=$3Z%44P
System.out.println3=%DyO=$3%Dy44P
System.out.println3=%YyO=$3%Yy44P
System.out.println3=%[yO=$3%[y44P
System.out.println3=%VV&O=$3%VV&44P
System.out.println3=%WW&O=$3%WW&44P
N
N
Ter"ar- operator or co"ditio"a# operator:- )"is operator is called )ernary operatots
beacuse it acts upon / variables. it is aslo called
conditional operator beacuse it represents a
conditional statement.
Synta%:- var O e%p1 I e%p&I : e%p/P
e%ample: ma% O3aWb4 t"en ma% O aP else ma%ObP
i 3aWb4 t"en ma% OaP else ma%ObP
0ot Operator or Mem$er operator'5):- )"is operator is denote by 3.4
314 pac-age.class
java.io.(uered*eader J (uered*eader is member o java.io pac-ageK
3&4 class.var or object.var
System.out e.id
3/4 class.met"od or object.met"od
mat".sCrt3 4 e.getSal3 4
6"sta"ce o. Operator:- )o test !"et"er an object belongs to a class.
Sysnta%:- boolean var O obj instance o class+ameP
e%p:- % Oe instance o EmployeeP
Instance o operator only or t"e c"ec-ing o object !"ic" belong to t"is class.
"e: Operator:- Synta%:- classname obj O ne! classname3 4P
Employee e O ne! Employee 3 4P
EJ5M creates objects only at runtime.
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOO
/&O!TRO2 STATEME!TS/
Se>ue"tia# E%ecutio":- E%ecuting t"e statements one by one is called seCuential
e%ecution or linear E%ecution.
E%ecuting t"e statements randomly and repeatedly is called random e%ecution *andom
E%ecution.*andom E%ecution is useul to develop critical
and comple% program. *andom E%ecution is possible t"roug" control statements.
&o"tro# Stateme"ts:- are t"e statements !"ic" c"ange t"e lo! o t"e e%ecution.)"ese
are t"e ollo!ing control statements:-
1. I...........else statements
&. do !"ile loop
/. !"ile loop
1. or loop
2. or eac" loop
7. s!itc" statements
;. brea- statements
<. continue statements
>. return statements
4!otes:- 8 Loop is repeatedly several times.
8 statement is e%ecuted only once.
(5 6. 555e#se stateme"ts:- )"is statements is useul to e%ecutes a tas-. depending upon
!"et"er a condition is true or not.
Sysnta%:- i3condition4
statement 1P
Jelse statement &PK
::program test i a number is $ve or -ve
class ,emo
G
public static void main3String argsJK4
G
int %P :: declaration
%O-2P :: intialiQation
i3%OOB4
System.out.println3=it is Qero=4P
else i3%WB4
System.out.println3%$= is positive=4P
else
System.out.println3%$= is negative=4P
N
N

do-?hi#e 2oop:- )"is loop is e%ecutes a group o statements repetadely as long as a given
condition is true
Synta%:- do
G
statementsP
N
!"ile 3condition4P
:: Program:- ?enrate numbers
class ,emo
G
public static void main3String argsJK4
G
int num O1P
do
G
System.out.println3num4P
num$$P
N
!"ile 3num V O1B4P
N
N
?hi#e 2oop:- )"is loop repeatedly e%ecutes a group o statements as long as a given
condition is true same as do-!"ile
Sysnta%:- !"ile 3condition4
G
statementsP
N
:: program:- genrate even numbers upto 1B
class ,emo
G
public static void main3String argsJK4
G
int nO&P
!"ile 3nVO1B4
G
System.out.println3n4P
nOn$&P
N
N
IIA:- H"at is t"e dierence bet!een do !"le and !"ileI
8ns:- In do-!"ile !e are going to e%ecute atleast one statement t"en only condition is
going to be c"ec-ed. (ut in case o !"ile loop irst condition is
going to be c"ec-ed.
E !"ile loop is better t"en t"e do-!"ile.
For 2oop:- )"is loop repeteadly e%ecutes a group o statements as long as a condition is
true.
Synta%:- or3e%p1P e%p&P e%p/4
G
statementsP
e%p:- or3int iO1P i V1BP i$$4
G
System.out.println3i4P
N
:: e%p1:- IntialiQation e%pression
e%p&:- conditional e%pression
e%p/:- Modiying e%pression
+ote:- He can !rite or loop by eleminating e%pression 1 or e%pression & or e%pression / or
any t!o e%pression ar all t"e t"ree e%pression.
6".i"ite #oop:- is loop t"at it is e%ecuted orever ininites loop are dra!bac- o a programs.
E+ote:- press ctrl$c 3to comeout t"e loop4
3reak Stateme"ts:- is useul to comeout o a loop.
::program:-
class ,emo1
G
public static void main3String argsJK4
G
int i O1P
or 3P P4
G
System.out.print3i4P
i$$P
i3iW1B4
brea-P
N
N
N
!ested #oop:- !e can !rite one loop bet!een anot"er loop is called +ested loop.
For each #oop:- )"is loop is repetadly e%ecutes t"e group o statements or eac" element
o a collection.
Ecollection:- group o elements.
Synta%:- or3var: collection4
G
statementsP
N
E variables reers to element o collection.
::proram :- .or each #oop

class ,emo&
G
public static void main3String argsJK4
G
int arrJK O G&/.27.;<./1.1BNP
or 3int % : arr4
:: % is represent t"e element o array
System.out.println3%4P
N
N
S:itch Stateme"t:- )"is statement is useul to e%ecute a particular tas- rom among
several tas-. come depending upon t"e value o
variable.
ESynta% means correct ormat !riting statement.
Synta%:- s!itc"3var4
G
case value1: statements1P
case value&: statements&P
-------------------------
-------------------------
-------------------------
case value n: statements nP
J deault : deault statementsK
::program:- or s!itc"
class ,emo/
G
public static void main3String argsJK4
G
c"ar color O 6g6P
s!itc"3color4
G
case 6r6: System.out.println3=red=4P
brea-P
case 6g6: System.out.println3=green=4P
brea-P
case 6!6: System.out.println3=!"ite=4P
brea-P
deault : System.out.println3=+o #olor=4P
N
N
N
E (rea- is useul to comeout o s!itc" bloc-.
:: program:-
class ,emo1
G
public static void main3String argsJK4
G
c"ar var O =?=P
S!itc"3var4
G
case 6?6:
case 6g6: System.out.println3=?reen=4P
brea-P
case 6*6:
case 6r6: System.out.println3=*ed=4P
brea-P
N
N
N
3reak stateme"t:- )"ese are / !ays.
Ist !ay:-W (rea- is used to comeout o loop
It may do...!"ile. !"ile. orloop etc.
IInd !ay:-W (rea- is used to comeout o a S!itc" (loc-.
IIIrd !ay:-W (rea- is used in nested (loc- to jump are go to end o a bloc-.
:: program:-
class ,emo2
G
public static void main3String argsJK4
G
boolean % O trueP
bl1:
G
bl&:
G
bl/:
G
System.out.println3=bloc-/=4P
i3%OOtrue4
brea- bl&P
continueP
N
System.out.println3=(loc-&=4P
N
System.out.println3=(loc-1=4P
N
System.out.println3=out o all=4P
N
N
&o"ti"ue Stateme"ts:- is useul to continue t"e ne%t repeatition o all loop t"en continue
is e%ecuted subseCuent statements in t"e loop are not e%ecuted.
:: program:- :: continue
class ,emo2
G
public static void main3String argsJK4
G
or3int i O1BP iWO1P i--4
G
System.out.println3i4P
N
N
N
Retur" stateme"ts:- "ave t!o !ays:-
314 *eturn statement is used to comeout o a met"od bac- to t"e calling met"od.
main34 -W calling met"od m13 4 --W
called met"od
3&4 *eturn statement can be used to return some value to t"e calling met"od.
return :
return %P
return 1P
return3%$y4P
ERetur" stateme"t i" mai" method termi"ates the e"tire app#icatio"5
:: program:- return in main3 4
class ,emo7
G
public static void main3String argsJK4
G
int n O 1P
System.out.println3=(eore return=4P
i3nOO14
returnP
:: or System.e%it3B4P
System.out.println3=ater return=4P
N
N
E +ote:- return statement is terminate met"od only !"enever System.e%it3B4 is terminate
t"e application.
IIA:- H"at is t"e dierence bet!een System.e%it3B4 and System.e%it314I
8ns:- (ot" !ill terminate t"e application
E System.e%it3B4 represents +ormal termination.
E System.e%it314 represents termination due to an error.
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOO
/STREAM/
Stream:- 8 Stream represents lo! o data it carries to data one place to anot"er place
rom -eyboard to memory.
)"ere are t!o types o stream.
'i) 6"putStream:- )"ey read or access data.
'ii) OutputStream:- )"ey send or !rite data to some ot"er place.
8ll streams are represents as classes in java.io.pac-age 3In t"is pac-age more t"an 2B
class4.

Step-1:- 38ccepting input rom -eyboard4
#onnect -eyboard to InputStream*eader 3obj4
InputStream*eader obj O ne!3InputStream*eader3System.in44P
Ene! operator or use creating t"e object.
Step-&:- #onnect InputStream*eader to (uered*eader.
(uered*eader br O ne! (uered*eader3obj4P
Step-/:- *ead data rom (uered*eader using read3 4 or readLine3 4 met""ods.
4read' ) method read single line or single c"aracter.
4read2i"e' ) method read string or group o c"aracter.
Step-1 and step-& can also be !rite as ollo!s.
(uered*eader br O ne! (uered*eader3ne! InputStream*eader3System.in44P
::Program:- *ead a single c"ar rom -eyboard
import java.io.EP
class Input
G
public static void main3String argsJK4 t"ro!s I'E%ception
G
(uered*eader br O ne! (uered*eader 3ne! InputStream*eader3System.in44P
System.out.println3=Enter a c"ar=4P
c"ar c" O 3c"ar4br.read3 4P
System.out.println3=0 entered:= $c"4P
N
N
T-pe &asti" or casti":- #onverting one data type into anot"er data type is called type
casting or casting as li-e as
3c"ar4 in typecasting !e use cast operator Jcast means
modiyK.
Eread34 and readLine3 4met"od gives I'E%ception.
EreadLine34 met"od need not casting it is use string.
@@proram:- Accepti" stri" .rom a ke-$oard
same program above but little bit c"ange belo!:-
@@proram:- Accepti" i"t va#ue .rom ke-$oard
same program above little bit c"ange given belo!:-
@@proram:- Accepti" a .#oat "o5 .rom ke-$oard
same program as above !it" little bit c"ange
E @loat data type can represents upto ; digits only ater decimal points.
@@Proram:- Accepti" dou$#e va#ue .rom ke-$oard
same program above little bit c"ange
E,ouble data type can represents upto 12 digits only ater decimal points.
@@Proram:- Accepti" emp#o-ees detai#s
import java.io.EP
class Emp,ata
G
public static void main3String argsJK4t"ro!s I'E%ception
G
(uered*eader br O ne! (uered*eader3ne! InputStream*eader3System.in44P
System.out.print3=Enter Empid:=4P
int id O Integer.parseInt3br.readLine344P
System.out.print3=Enter Emp Se% 3M:@4:=4P
c"ar se% O 3c"ar4br.read3 4P
br.s-ip3&4P
System.out.printl3=Enter Empname:=4P
String name O br.readLine3 4P
System.out.println3=IdO=$id4P
System.out.println3=se%O=$se%4P
System.out.println3=+ameO=$name4P
N
N
4"ote:- s-ip3 4 met"od
s-ip t"e 3Rn4 space remove rom t"e read3 4 met"od

H-?:- 8ccept & nos. rom -eyboard and ind t"e result o t"eir addition. substraction.
multiplication and division.
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOO
/Arra-/
Arra-:- 8n 8rray represents a group o elements o same datatype. 9andling group o
elements becames easily t"roug" 8rray.
)"ere are t!o types o 8rray:-
314 Single ,imensional 8rray31,4
3&4 Multi ,imensional 8rray 3&,./,4
Si"#e dime"sio"a# arra-s:- 8 1, array represents a single ro! or single column o
elements.
or e%ample:-W
Mar-s obtained by a student in 2 dierent subject
&reati" a (0 Arra-:- )"ere are t!o !ays. !e can declare a 1, array and also intialiQe it
!it" t"e element.
Mar-sJiK iOB. 1. &. /. 1
i is stands or i"de%:- represe"ts a e#eme"t positio"s "o5 i"
the Arra-A
4 A (0 Arra- ma- o"#- o"e 6"de%5
3a4 int mar-sJK O G2B.21.2&.2/.21NP
loat salJK O G&/BB.BB.27>>.;2. 1&BB.BBNP
double %JK O G1.;2.;.<>>./211.BNP
Strimg names JK O G=*aju=.=*ani=.=vijay=.=\umar=NP
3b4 He can allote a memory rom 1, 8rray and later store t"e element.
int mar-sJK O ne! intJ2K--W 2 represents siQe o array
mar-sJBK O 2BP
mar-sJ1K O 2BP
@loat %JK O ne! loatJ1BBKP
,ouble s O ne! ,oubleJ1BKP
String str O ne! StringJ&BKP
+ote:- 8rray +ame.lengt" gives t"e siQe o t"e 8rray.
arrayname.lengt" ---W siQe. leng"t means property o array.
3i4 int mar-s JK O ne! intJ2KP
mar-sJBK O 2BP
mar-sJ1K O 21P
::program:- total mar-s and percentage --W 1, array
import java.io.EP
class 8rr
G
public static void main3String argsJK4t"ro!s I'E%ception
G
:: to read data rom -eyboard
(uered*eader br O ne! (uered*eader3ne! InputStream*eader3System.in44P
System.out.println3=9o! many subjects=4P
int n O Integer.parseInt3br.readLine344P
:: create an array !it" siQe n
int mar-sJK O ne! intJnKP
::store elements into mar-sJK
or3int iOBPiVnPi$$4
G
System.out.print3=Enter mar-s=4P
mar-sJiK O Integer.parseInt3br.readLine344P
N
::ind total mar-s
int totOBP
or 3int iOBP iVnP i$$4
G
System.out.println3mar-sJiK4P
tot O tot$mar-sJiKP
N
System.out.println3=)otal mar-sO=$tot4P
::percentage
loat percent O tot:nP
System.out.println3=percentageO=$percent4P
N
N
*0 Arra-:- int mar-sJK JK--W & dimensional 8rray 3multi dimensional array4 contains
several ro!s and several column o data.
mar-s obtained a group o student in 2 subjects.
&reati" a *0 Arra-:-B )"ere are t!o !ays:-
3a4 !e can declare a &, array and also intialiQe directly !it" elements.
int mar-sJK JK O G2B.21.2&.2/.21N
He a"ve & Inde% t"ey are in multidimensional array t"ey represents JiK. JjK 3ro! position no.
or column positionno.4
&, array is a combination o / several , 8rray.
/, array is a cobination o & several , array.
E%p:- loat %JKJK O GG1.1.&.1./.1.1.1N.
G&.B.&.&.&./.-&.11N.
G B.1.B.&.B./.-B.BBNNP
'$)-66 :a-:-B !e can allot or a &, array using a ne! operator and later to store t"e
element.
int mar-sJK JK O ne! intJ/KJ2KP
double d JK JK O ne! double J2KJ2KP
String namesJK JK O ne! StringJ2KJ1BKP
byte b JKJK O ne! byteJ1BKJ/KP
::program:-W :: read rom a &, array and display in matri% orm
class 8rr
G
public static void main3String argsJK4
G
::ta-e a &, 8rray
loat %JKJK O GG1.1.&.1./.1.1.1N.
G&.B.&.&.&./.&.1N.
GB.1.B.&.B./.-B.BBNNP
::display in matri% orm
System.out.println3=Rn In matri% orm:=4P
or3int iOBP iV/P i$$4
G
or3int jOBP jV1P j$$4
G
System.out.println3%JiKJjK $=Rt=4P
N
System.out.println34P
N
N
N
Method Parameter:-B is a variable to recieve data rom outside args
Arume"ts:-B arguments are t"e values passed to t"e met"ods.
IIA:- H"at are command line argumentsI
8ns:- )"e command Line arguments are t"e values passed to main met"od rom command
prompt.

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOO
Access Specifires:- Access Specifires are keywords which specify how to access or read
the members of a class or the class itself.
There are four types of access specifires in java.
(1) private:- private members of a class are not accessible anywhere outside the class.
(2) public:- public members of a class are always available outside the class.
(3) protected:- protected members are available outside the class.
(4) default:- when no access specifier is used , java compiler internally uses default specifiers.default
members are available outside the class.
we can not use private specifiers before a class in java.
we can intiali!e the instance variable directly at the time of there declaration in the class.
"ote#$%inal keyword is used to declare constants in java.
"ote#$ we can intiali!e the instance variable usin& a constructor.
Constructor:- ('( A constructor is a similar to a method that it is used to intiali!e the instance
variable.it can perform only one work at a time.
()( A constructor name and the class name should be same.
(*( A constructor may or may not have parameters. A constructor without
parameter is called +efault constructor or !ero parameteri!ed constructor.
(,( A constructor does not return any value not even void.
(-( A constructor is called only once at the time of creatin& the object.
Parameterized constructor:- is a constructor with have one or more parameter.
Person (Strin& s, int i(
.
name /s0
a&e /i0
1
Constructor overloadin:- 2ririn& ) or more constructor with the same name but with a difference in a
parameter is called constructor overloadin&.
('( when no constructor is written in the class then java compiler adds a default constructor with default
values.
!"#$%&S
!et'ods:- A methods represents a &roup of statements to perform a task. a methods has two parts.
('( !et'ods protot(pe and !et'od 'eader:- This part contains method name, method parameter and
method return type.
method parameters are variables to recieve data from
outside in to the data.
Synta3#$ returntype methodname (para',para)..........(
e3p#$ void sum( (
double sum(double 3, double y(
double power(double 3, int n(
double s4rt(double num(
lon& fact(int n(
()( !et'od bod(:- contains a &roup of statements written in . 1 (left and ri&ht curly braces(.
method body contains lo&ic to solve the problem or task.
e3p#$ .
statements0 5$$$$$$$$$$$$ method body
1
"ote#$ if a method returns some result then we should use return statement in the body.
return 30
return '0
return(36y$)(0
return obj
return arr
"ote#$ A method can return only one value or one entity.
77Pro&ram#$ understandin& methods
class Sample
{
//instance vars
private double x,y;
//parameterized constructor
Sample (double a, double b)
//double a and double b is local variable
{
x=a;
y=b;
}
//methods
void sum( )
{
double z = x+y;
Systemoutprintln(!Sum=!+z);
}

public static void main(Strin" ar"s#$)
{
Sample s = new Sample (%&,%'');
ssum( );
//double res = ssum( );
// Systemoutprintln(!(esult=!+res);
}
}
)ocal *ariables:- 8ocal variables declared with in a constructor or method. The scope of local variable is
limited only to that the constructor or methods.
+nstance met'od:- A method that acts upon instance variables of a class is called instance method.
9nstance method is called usin& (objectname(.(methodname( ( (
by default in your pro&ram local variable is present.
Static met'od:- A method that does not acts upon instance variable is called static method.
$$$: A static method should be declared as static.
static methods are called usin& (objectname.methodname( and (class name. methodname(
$$: classname.methodname is much effiecent
$$$: static method can not read 9nstance variables. The reason is J;< first e3ecutes static method and
later it creates object.
"otes#$ J;< e3ecutes in the folowin& order
'$ static blocks
)$ static methods
*$ instance methods
static methods directly access or read static variables
static variable should be declared as static.
99=#$ 2hat is the differnce between instance variable and static variable>
Ans#$ //: 9nstance variable is a variable whose seprate cpoies are available to the objects.9f the instance
variable in an object is modified, it will not effect other object.
9nstance variable are created on heap.
//: Static variable is avariable whose sin&le copy in memory is a shared by all the objects. 9f the
static variable modified. 9t wll effect all the objects. static variable are stored on method area.
77pro&ram to test static block or static method
class Test
.
public static void main(Strin& ar&s?@(
.
System.out.println(Astatic methodA(0
1
static
.
System.out.println(Astatic blockA(0
1
1
!et'ods in ,ava:-
'. Static methods
). 9nstance methods
(a( Accessor method
(b( <utator method
*. %actory method
(2) +nstance met'od:- 9nstance method are the methods which act upon the instance variables.9nstance
methods are called usin& (objectname.methodname( instance methods are two types.
(a) Accessor met'ods:- are te methods which access or read the instance variables.they donot modify
the instance variables.
(b) !utator met'ods:- These methods not only read the instance variable but also modify them.
77Pro&ram$ accessor and mutator methods
class )erson
{
// instance vars
Strin" name;
int a"e;
char sex;
//parameterized constructor
)erson(Strin" name, int a"e, char sex)
{
thisname = name;
thisa"e = a"e;
thissex = sex;
}
//read and display the vars( )
void display( )
{
Systemoutprintln(!*ame=!+name);
Systemoutprintln(!+"e=!+a"e);
Systemoutprintln(!Sex=!+sex);
}
public static void main(Strin" ar"s#$)
{
)erson p% = new )erson(!Shrinu!,,&,-.-);
p%display( );
}
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
77ro&am#$ <utator method
Person modify(Person obj(
.
obj.name /A8a3miA0
obj.a&e$$0
obj.se3/B%B0
1
1

class ,emo
G
public static void main3String argsJK4
G
Person p1 O ne! Person 3=S"rinu=.&B.6M64P
p1.display3 4P
Person p& O p1.modiy3p14P
p&.display3 4P
p1.display3 4P
N
N
this:- t"is is a -ey!ord t"at reers to t"e persent class or current class it means t"is can be
reer to all t"e members o present class. it means can be reers to persent class
instance variable. present class met"ods and present class constructor.
E t"is is only a rerence but not object.
::program:- to reer t"is:-

class Sample
G
int n3 4P
Sample3 4
G
t"is31BB4P
:: t"is is used to parameteriQed constructor
t"is.display3 4P
N
Sample3int n4
G
t"is.n O nP
N
void display3 4
G
System.out.println3=nO=$n4P
N
N
class ,emo
G
public static void main3String argsJK4
G
Sample O ne! Sample3 4P
N
N
Ererence variables are passed to met"ods by value.
Eobjects are passed to met"ods by value.
Factor- methods:- )"ere is a met"od get+umberInstance3 4P it is a static met"od in no.
ormat class. )"is met"od return no.ormat clas object.
8 actory met"od is a met"od t"at returns an object o t"e class to !"ic" it belongs.
+umber@ormat obj O +umber@ormat.get+umberInstance3 4P
IIA:- 9o! many !ays can !e create object in JavaI
8ns:- Hays o creating object in java:
314 0sing ne! operator
Employee obj O ne! Employee3 4P
3&4 0sing @actory met"od
+umber@ormat obj O +umber@ormat.get+umberInstance3 4P
3/4 0sing ne!Instance3 4 met"od
class c O #lass.or+ame3=Employee=4P
Employee obj O c.ne!Instance3 4P
314 0sing cloning :-
#reating e%act copy o an e%isting object is called cloning.
Employee obj1 O ne! Employee3 4P
Employee obj& O obj1.clone3 4P
::program :- area o a circle
import /avatext*umber0ormat;
class 1ircle
{
public static void main(Strin" ar"s#$)
{
final double )2 = ,,/(double)3;
double r = %'';
double area = )24r4r;
Systemoutprintln(!+rea o5 circle=!+area);
// create *umber 5ormat class ob/ect
*umber0ormat ob/ = *umber0ormatgetNumberInstance( );
//store 5ormat in5o into ob/
ob/set.aximum0raction6i"its(,);
ob/set.inimum2nte"er6i"its(3);
//apply 5ormat into area
Strin" str = ob/5ormat(area);
//display 5ormatted area
Systemoutprintln(!0ormatted area=!+str);
}
}

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOO
::program:-
class 7ne
{
89o t;
int x;
7ne(89o t)
{
thist = t;
x=%&;
}
void sho9( )
{
Systemoutprintln(!x=!+x);
tsho9();
}
}
class 89o
{
int y;
89o()
{
y=,,;
}
void sho9()
{
Systemoutprint(!y=!+y);
}
}
class (elate
{
public static void main(Strin" ar"s#$)
{
89o ob/, = new 89o();
7ne ob/% = new 7ne(ob/,);
ob/%sho9();
}
}
O$,ect Craph:- It is a grap" s"o!ing relation bet!een dierent objects in memory.

E !e can store obj&6s rerence in object1 to give relations"ip bet!een t"ose t!o
objects.t"en 8ll t"e members o object& can be rerenced rom object1.
)"is is one !ay o relations"ip bet!een object.
3"o! to give relation o t!o objects in memoryI4
as li-e as (uered*edaer obj relation to InputStream*eader.
6""er c#ass:- Inner class is a class !ritten in anot"er class. is provide a satey mec"anism.
!ote:-
'() Inner class is a class !it" in anot"er class.
'*) Inner is a satey mec"anism.
'+) Inner class is "idden in its outer class rom ot"er classes.
';) 'nly Inner class can be declared as 6private6.
'<) 8n object to Inner class can not be created in any ot"er class.
'D) 8n object to Inner class can be created only its outer class.
'E) Inner class and outer class objects are cretaed in seprate locations in memory.
'F) 8ll outer class members are available to inner class directly.
'G) 'uter class members can be rerenced as 'uter class t"is.member Inner class members
can be rerenced as t"is.member
'(=) Inner class object contains an additional invisible ield 6t"is ] B6 outer class rerence.
H mea"s seprator 2ike as 3a"k acct H i"terest5c#ass
@@Proram:- 6""er c#ass e%amp#e
class (an-8cct
G
private double balP
(an-8cct3double b4
G
bal ObP
N
void start3double r4
G
Interest in O ne! Interest3r4P
in.calculateInterest3 4P
N
private class Interest
G
private double rateP
Interest 3double r4
G
rate O rP
N
void calculateInterest3 4
G
double interest O balErate:1BBP
System.out.println3=InterestO=$interest4P
bal$OinterestP
System.out.println3=(alanceO=$bal4P
N
N
N
class Inner,emo
G
public static void main3String argsJK4
G
(an-8cct account O ne! (an-8cct31BBBB4P
account.start31.24P
N
N
77777777777777777777777777777777777777777777777777777
77777777777777777777777777777777777777777777777777777
7777777
/6!HER6TA!
&E/
Producing a ne! classes rom e%isting classes is called In"eritance.8lready e%isting class is
called Super class and t"e ne!ly produced is called subclass.
e%tends is t"e -ey!ord used in"eritance.
Synta%:-
class subclass e%tends superclass
::program using student class program
class 0se
G
public static void main3String argsJK4
G
::create an object to student class
Student s O ne! Student3 4P
::store data into obejct s
s.setId31B4P
s.set+ame3=S"rinu=4P
s.set8ddress3=*-71. Panc"vati colony ?"aQiabad=4P
se.setMar-s3>7B4P
::retrieve data rom object and display
System.ut.println3=IdO=$s.getId344P
System.out.println3=+ameO=$s.get+ame344P
System.out.println3=8ddressO=s.get8ddress344P
System.out.println3=Mar-sO=$s.getMar-s344P
N
N
+ote:-1 Subclass object contains a copy o superclass object. )"is is t"e reason all t"e
members o superclass are available to t"e subclass.
IIA:- H"at is t"e advantage o In"eritanceI
8ns:-314 reuseability t"e code is t"e main advantage o in"eritance.because o reuseability
developing class or programs !ill ta-e less time t"at means programmer
can developed more code in less time advantage is programms 3productivity4 is
increased.It !ill increase t"e overall productivity o t"e company.so more proit or t"e
company.
+ote:-& He al!ays create an object to subclass only.
::Programs:- i super class "as consturctors
class 'ne
G
'ne34
G
System.out.println33=super=4P
N
N
class )!o e%tends 'ne
G
)!o34
G
System.out.println3=sub=4P
N
N
class #onst
G
public static void main3String argsJK4
G
)!o t O ne! )!o34P
N
N
!ote:-
E Super class deault constructor is available to t"e subclass by deault.
E jvm irst e%ecutes super class constructors and later e%ecutes subclass constructor.
E Super class parameteriQed constructor is not available to subclass object.
Super Ie-:ord:- is useul to reer to superclass it means super can reer to superclass
instance variable. super class met"ods and also super class constructor.
::Program:- Super--W reers to super class members
class 'ne
G
int %P
'ne3int %4P
G
t"is.%O%P
N
void s"o!34
G
System.out.println3=Super %O=$%4P
N
N
class )!o e%tends 'ne
G
int yP
)!o3int a. int b4
G
Super3a4P ::Super class deault constructor
yObP
N
void s"o!34
G
System.out.println3=sub yO=$y4P
super.s"o!34P
N
N
class #onst
G
public static void main3String argsJK4
G
)!o t O ne! )!o31B.&&4P
t.s"o!34P
N
N
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOO
/PO21MORPH6SM/
Po#-morphism:- )"e ability to e%ist in several orms is called polymorp"ism.I a variable or
object or met"od perorms various tas-s t"en it is called Polymorp"ism.
)"ere are t!o types o polymorp"ism.
314 Static Polymorp"ism
3&4 ,ynamic Polymorp"ism
314 Static Po#-morphism:- )"is is polymorp"ism e%ibited as compilation time. "ere t"e
compiler binds a met"od call !it" actual met"od body during compilation time.so
it is aslo called static binding or compile.
3&4 0-"amic Po#-morphism:- )"is is t"e polymorp"ism e%ibited at run time in t"is jvm
binds a met"od call !it" t"e actual met"od body during runtime.
It is called ,ynamic polymorp"ism.so it is also called
,ynamic binding or *un)ime polymorp"ism.
::Program ,ynamic polymorp"ism
class Sample1
G
void add3int a. int b4
G
System.out.println3=sum o t!oO=$3a$b44P
N
void add3int a. int b. int c4
G
int d O a$b$cP
System.out.println3=sum o t"reeO=$d4P
N
N
class Poly
G
public static void main3String argsJK4
G
Sample s O ne! Sample34P
s.add31B.&&4P
s.add31B.&B.224P
N
N
Method Si"ature:- Met"od name along!it" met"od parameters is called met"od
signature.
Hriting t!o or more met"ods !it" te same name but !it" a dierent
met"od signatures is called Met"od 'verloading.
EIn met"od overloading jvm ideniies t"e met"ods dierently as s"o!n belo!:-
314 )"ere is a dierence in t"e number o parameters
void add3int a. int b4
void add3int a. int b. int c4
3&4 )"ere is a dierence in t"e datatypes o parameters
void add3int a. int b4
void add3double a. double b4
3/4 )"ere is a dierence in t"e seCuence o parameters
int s!ap3int a. c"ar b4
int s!ap3c"ar a. c"ar b4
8nyone o t"e above dierences !ill ma-e met"od signatures to be dierent. so J5M can
easily recogniQe t"e met"od J5M.
EMet"od overloading using Instance met"od is an e%ample or dynamic polymorp"ism.
::Program:- ,ymanic polymorp"ism
class 'ne
G
void calculate3double %4
G
System.out.println3=SCuare valueO=$3%E%44P
N
N
class )!o e%tends 'ne
G
void calculate3double %4
G
System.out.println3=sCuare rootO=$mat".sCrt3%44P
N
N
class poly1
G
public static void main3String argsJK4
G
)!o t O ne! )!o34P
t.calculate3174P
N
N
Method Overridi":-
Hriting t!o or more met"ods !it" t"e same name and t"e same signature is super and sub
classes is called Met"od 'verriding.
met"od overriding using intance met"od is an e%ample or ,ynamic polymorp"ism. In
met"od overriding. J5M e%ecutes a met"od depending upon
t"e class or !"ic" t"e object is created.
Static Po#-morphism:- Static plymorp"ism or compile time polymorp"ism perorming
met"od overloading and met"od overriding using static met"ods or
private or inal met"ods are e%ample or static polymorp"ism.
4 Fi"a# methods:- is a met"od !ritten in inal class. 8 class declared as a inal is called a
inal class.

E !e can not create a subclasses to a inal class.
E inal met"ods canot be overridden.
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOO
T-pe&asti:- #onverting one datatype into anot"er datatype is called )ype#asting.
0ataT-pe:- 8 datatype represents t"e type o data stored into a variable or Memory. )"ere
are t!o types o datatypes:-
'() Fu"dame"ta# 0atat-pe or Primitive 0atat-pe:- )"ese datatypes stores a single
value met"ods are not available to "andle t"em
e%ample:- byte. s"ort . c"ar . int
.long.loat. double. boolean is a primitive datatypes.
'*) Adva"ced 0atat-pes or Re.re"ced 0atat-pes:- )"ese datatypes represents several
values. met"ods are also availeble to "andle t"em.
or e%ample:- any array is a advanced
,atatypes. any class3e%:- String. Employee.......etc.4
IIA:- H"at is t"e dierence bet!een undamental datatypes and advanced datatypesI
*eply:- See above. !e "ave used met"od in only advanced not used in undamental
datatypes.
Usi" &asti":-'() !e can convert a primitive datatype into anot"er primitive datatype.
Usi" &asti":-'*) !e can convert 'ne advanced datatype into anot"er advanced
datatype.
Usi" &asti":-'+) He can not convert a primitive datatype into an advanced type or vice-
versa using casting.
or t"is purpose !e s"ould use !rapper class met"od.
&o"verti" or casti" .u"dame"ta# :-
3a4 ?ide"i":- #asting a lo!er datatype into a "ig"er datatype is called !idening
byte.s"ort. c"ar. int. long. loat. double
Lo!er
V--------------------------------W9ig"er.
3E boolean is not convert to anot"er to anot"er datatype4.
c"ar c" O 686
int n O 3int4 c"P

int means caster
OOW EIt is also an e%ample o polymorp"ism.
beacuse same variables e%ist in dierent items.
OOWE Hidening is sae . !idening is also called implict or internal casting.
3b4 !arro:i":- #onverting a "ig"er datatype into a lo!er datatype is called narro!ing.In
t"is !e may lose digit or accuracy.
e%ample:- int % O77P
c"ar c O 3c"ar4 %P
double d O 1&.><;7
int n O 3int4 dP
E +arro!ing is unsae or dangerous.
E +arro!ing It is also called e%plict casting beacuse t"e java compiler does not provide t"e
casting.
&asti" Adva"ced datat-pes:- #asting can be used to convert one class type into anot"er
class type. !"en t"ose classes "ave relations"ip !ay o In"eritance.
::Program:- casting advanced datatype
class 'ne
G
void s"o!134
G
System.out.println3=super=4P
N
N
class )!o e%tends 'ne
G
void s"o!&3 4
G
System.out.println3=sub=4P
N
N
class #ast
G
p.s.v.m3String argsJK4
G
:: super class rerence to reer to super class object
'ne oP
oO ne! 'neP

::sub class rerence to reer to sub class object
)!o tP
tO ne! t!o34P
t.s"o!134P
t.s"o!&34P
N
N
::super class return to subclass object
'ne oP
o One! )!o34P
o.s"o!134P
o.s"o!&34P
N
N
!ote:-( #asting sub class type into super class type is called Hidening.
!ote:-* In Hidening !e can call super class met"od.
In Hidening !e can not call subclass met"ods.
In !idening !e can not call subclass met"ods unless t"ey override superclass met"ods.
ESuper class type to sub class type converting is called !arro:i" or do:"casti".
E+arro!ing using super class object can not access all t"e met"ods o bot" t"e super class
and also sublass.
E%:- )!o tP
t O3)!o4ne! one34P
t.s"o!34P
E +arro!ing using subclass object can access all t"e met"ods o bot" t"e superclass and
also subclass.
'ne oP
o O ne! )!o34P
)!o t O 3)!o4 oP
t.s"o!134P
t.s"o!&34P
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOO
&o"crete a"d A$stract
&o"crete method:- 8 met"od !it" body is called concrete or complete met"od.
A$stract method:- 8 met"od !it"out met"od body is called 8bstract met"od.
A$stract c#ass:- 8 class t"at contains 8bstract met"od is called abstract class.
OOW :e ca" "ot create o$,ect to a$stract c#ass '$eacuse JVM does "ot k"o: ho:
much memor- is re>uired or a$stract does is i"comp#ete method5
//)ro"ram:; an abstract class example
abstract class 1ar
{
//every car 9ill have a re"no
int re"no;
//to store re"no
1ar(int r)
{
re"no = r;
}
//8he mechanism o5 5illin" 5uel is also same every car 9ill have a 5uel tan<
5or all cars
void 5ill8an<()
{
Systemoutprintln(!8a<e car <ey and 5ill 5uel into 8an<!);
}
//every car 9ill have a steerin" but 6i55erent car 9ill have di55erent
streein" mechanism
abstract void steerin"(int direction);
// every car 9ill have brea<s but di55erent cars 9ill have di55erent brea<in"
mechanism
abstract void bra<in"(int 5orce);
}
// ;;;= 8his is a concrete sub class o5 car class
class .aruti extends 1ar
{
.aruti (int re"no)
{
super(re"no);
}
void steerin"(int direction)
{
Systemoutprintln(!.aruti uses mannual steerin"!);
Systemoutprintln(!6rive the car!);
}
void bra<in" (int 5orce)
{
Systemoutprintln(!.aruti uses "as brea<s!);
Systemoutprintln(!Stop the car!);
}
}
// 8his is a concrete subclass o5 car class
class Santro extends 1ar
{
Santro(int re"no)
{
super(re"no);
}
void steerin" (int direction)
{
Systemoutprintln(!Santro uses po9er steerin"!);
Systemoutprintln(!Start it!);
}
void bra<in" (int 5orce)
{
Systemoutprintln(!Santro uses a hydraulic brea<s!);
Systemoutprintln(!Stop it!);
}
}
//usin" a 1ar
class >se1ar
{
public static void main(Strin" ar"s#$)
{
//create ob/ects to maruti and santro

.aruti m = new .aruti(,,,,);
Santro s = new Santro(%,,%);
//create a re5rence o5 abstract class
1ar c;

// c is a re5rence variable
// to drive maruti
c = m;
c5ill8an<( );
csteerin"(,);
cbra<in"(,&&);
}
}
A$stract &#ass:-
314 8n abstract class is a class !it" B or more abstract met"ods.
3&4 8n abstract class contains instance variables and concrete met"ods in addition to
abstract met"od.
3/4 He s"ould declare bot" t"e abstract class and its abstract met"ods as 6abstract6.
314 He can not create an object to abstract class.
324 (ut !e can create a rerence variable o abstract class.
374 'nce as abstract class is !ritten. all t"e abstract met"ods o t"at abstract class s"ould
be implementation 3body4 in its subclasses.
3;4 I any abstract met"od is not implemented . t"en t"at subclass s"ould also be declared
as 6abstract6.
3<4 8bstract class rerence can be used to reer to t"e objects.
3>4 8bstract class rerence can not rere to t"e individual additional met"ods o t"e
subclasses.
31B4 'nce an abstract class is !ritten . It is t"e duty o t"e programmer to create
subclasses or it.
3114 8 class can not be declared bot" as 6abstract6 and 6inal6.
:: inal abstract class Myclass OOW invalid statement.

6!TERFA&E:- 8n Interace is a speciication o met"od prototypes.

void connect( )
oracle
Sybase
MySql
driver
API
Docu
ment
::program an interace to connect to any database
interace MyInter
G
void connect34P
::by deault t"ey are public and abstract
:: !"y public because t"ey are available to /rd party vendor
N
class 'racle,( implements MyInter
G
public void connect34
G
System.out.println3=#onnecting to oracle database=4P
N
N
class Sybase,( implements MyInter
G
public void connect34
G
System.out.println3=#onnecting to sybase database=4P
N
N
class ,atabase
G
public static void main3String argsJK4t"ro!s E%ception
G
:: accept database name rom user t"roug" command line args and store it into an object
c
MyInter mi O 3MyInter4c.ne!Instance34P
:: call connect34 met"od using mi
mi.connect34P
N
N
+ote:- Eobject is super class or all t"e classes in java
6!TERFA&E:-
314 8n interace is a speciication o met"od prototypes.
3&4 8n interace contains B or more abstract met"ods.
3/4 8ll t"e met"ods o t"e interace are public and abstract by deault.
314 8ll t"e variables o t"e interace are public static and inal by deault.
324 He can not create an object to an interace.
374 (ut !e can create a rerence o interace type.
3;4 8ll t"e met"ods o interace s"ould be implemented in its implementation classes.
3<4 I any met"od is not implemented. t"en t"at implementation class s"ould be declared as
6abstract6.
3>4 Interace rerence can be used to reer to objects o its implementation classes.
31B4 once an interace is !ritten. any t"ird party vendor can provide implementation classes
or it.
3114 8n interace can not implement anot"er interace.
31&4 8n interace can e%tend anot"er interace.
31/4 It is possible to !rite a class !it" in an Interace.
3114 8 class can implement multiple Interaces 3^es4.
E%ample:- class Myclass implements Inter1.Inter&...
class 'ne
G
int %P
N
class )!o e%tends 'ne
G
int %P
N

Ans:- 9o! muc" memory is occupied by class )!oI
8ns:- < byte
T-pe o. 6"herita"ce:-
)"ere are t!o types o In"eritance
314 Single In"eritance
3&4 Multiple In"eritance
'() Si"#e 6"herita"ce:- producing subclasses rom a single superclass is called single
In"eritance.
class 8 class T
class ( e%tends 8 class P e%tends T
class A e%tends T
class * e%tends T
'*) Mu#tip#e 6"herita"ce:-
,eriving subclass rom multiple super classes is called Multiple In"eritance.

class 8 class %
class ( class ^
class # e%tends 8.( class _
class 8 e%tends T.^._
or
class ( e%tends T.^._
8bove / statements are invalid.
+ote:-
314 So multiple in"eritance is not available in java.reason is multiple in"eritance leads to
conusion or a
programmer.
3&4 java programmer can ac"ieve multiple In"eritance t"roug" multiple interaces.
class _ implements 8.(
3/4 java programmer can ac"ieve multiple in"eritance by repeated use o single in"eritance.
class _ e%tends 8.( :: invalid
class ( e%tends 8
class _ e%tends (
beacuse o t"e above reason multiple in"eritance is not available directly in java.but
indirectly you can ac"ieve
it t"roug" single in"eritance using repeatedly.
::Multiple in"eritance using interaces
interace @at"er
G
loat 9) O 7.&P
void "eig"t34P
N
interace Mot"er
G
loat 9) 2.BP
void "eig"t34P
N
class #"ield implements @at"er.Mot"er
G
public void "eig"t34
G
System.out.println3=#"ield6s "eig"tO=$3@at"er.9)$Mot"er.9)4:&4P
N
N
class Multi
G
public static void main3String argsJK4
G
#"ield c" O ne! #"ield34P
c"."eig"t34P
N
N
Pac-aes
A packa&e is a subdirectory to store classes and interfaces.
$$: packa&es hide classes and interfaces so that accidental deletion is not possibe.
$$: packa&es isolate, the classes and interfaces.
$$: packa&es provide reusability.
$$: different classes with the same in different packa&es is valid.
$$: The user can also create packa&es and he can e3tend already available packa&es.
77creatin& our own packa&e#pack
packa&e pack0
public class Addition
.
private double a,b0
public Addition (double a, double b(
.
this.a /a0
this.b/b
1
public void sum( (
.
System.out.println(Asum/A6(a6b((0
1
1
%#Cprem:javac $d . Addition.java
"ote#$ we have use our pro&ram to import pack.Addition and import pack.Substraction
77 Dsin& packa&e pack
class Dse
.
public static void main(Strin& ar&s?@(
.
pack.Addition obj / new pack.Addition('E, )).-(0
obj.sum( (0
1
1
77 add another class to the packa&e0 pack
packa&e pack0
public class Substraction
.
public static double sub(double 3, double y(
.
return 3$y0
1
1
7 double res / substraction.sub('E, )).-(
System.out.println(AFesult/A 6rs(0 7
"ote#$('( java compiler searches for .java files in the current directory.
()( java compiler searches for the packa&e in the current directory.
(*( finally java compiler will search for the packa&e directory in the class path.
(,( classpath is a system variable that stores active directory path in a computer system.
%#7prem:echo GH8ASSPATIG

(-( settin& the classpath to the packa&e directory.
77an interface in a packa&e
packa&e pack0

public interface <J+ate
.
void show+ate( (0 77 public abstract
1
77an implementation class for interface

packa&e pack0

import pack.<ydate0
import java.util.+ate0

public class <ydate9mpl implements <ydate
.
public void show+ate( (
.
+ate d / new +ate( (0
System.out.println((d(0
1
1
77 usin& interface
import pack.<ydate9mpl0
class Dse
.
public static void main(Strin& ar&s?@(
.
<y+ate9mpl obj / new <y+ate9mpl( (0
obj.shoe+ate( (0
1
1
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
Access Specifires
An Access specifies in java is a keyword that specify how to access the members of a class or class itself.
There are four access specifiers in java#$
'$ private
)$ public
*$ protected
,$ +efault
('( private members:- of a class are not available in other classes of the same packa&e or another
packa&e.
The scope of private specifier is class scope.
()( public members:- of a class are accessable in other classes of the same packa&e and another
packa&e also.
The scope of of public specifier is &lobal scope.
(*( protected members:- of a class are accessable to the classes of the same packa&e but not to the
classes
of another packa&e.
(,( default members:- are available to the classes of same packa&e and not available to the classes of
another.
this scope of default specifier is packa&e scope.
"ote#$ protected members of class are always available to the subclasses either in the same packa&e or
in another
packa&e.


I

Anda mungkin juga menyukai