0% found this document useful (0 votes)
61 views20 pages

A Programmer's Guide To Java SCJP Certification: CH 1: Basics of JAVA Programming

This chapter discusses the basics of Java programming including object-oriented concepts like classes, objects, methods, and fields. It covers the essential elements of a Java program including compiling and running a Java application. Different types of Java applications like applications, applets, and servlets are described. Key object-oriented programming concepts like encapsulation, inheritance, polymorphism, and abstraction are explained. Instance variables, methods, constructors, and static members are defined. The chapter also provides an example of a simple Java program and class.

Uploaded by

henish1304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views20 pages

A Programmer's Guide To Java SCJP Certification: CH 1: Basics of JAVA Programming

This chapter discusses the basics of Java programming including object-oriented concepts like classes, objects, methods, and fields. It covers the essential elements of a Java program including compiling and running a Java application. Different types of Java applications like applications, applets, and servlets are described. Key object-oriented programming concepts like encapsulation, inheritance, polymorphism, and abstraction are explained. Instance variables, methods, constructors, and static members are defined. The chapter also provides an example of a simple Java program and class.

Uploaded by

henish1304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

A Programmers guide

to
Java SCJP Certifcation
Ch 1 : Basics Of JAVA
Programming
Chapter Outline

Basic concepts of object oriented programming:


class, object, method, felds, reference, members,
inheritance and aggregation.

ssential elements of JAVA Programming.

!o" to compile and r#n a JAVA Program.

$hree t%pes of e&ec#table in JAVA: Application,


Applets and 'er(lets.
Classes

)embers of class: *ield and )ethod


Abstractions :
+t denotes the essential propert% and beha(ior of an object.
,sed to handle comple&ities in programming.
Class :
,ser -efned -ata t%pe. .e.g. 'tring in Ja(a/
0ro#p of 'imilar 1ind of objects.
Propert% .attrib#te/ and Beha(ior .operations/ of objects are
defned.
Field: A (ariable in
class representing
propert% of an object.
Method: ,sed to
defne operation or
beha(ior of an object.
Example
p#blic class Char'tac12 33C4A'' 5A)
33 C4A'' -C4A6A$+O5':
pri(ate char78 stac1Arra%9 F!E"#S
pri(ate int topOf'tac19
p#blic Char'tac1 .int capacit%/ 2 CO$S%&'C%O&
stac1Arra% : ne" char7capacit%89
topOf'tac1 : ;19
<
ME%(O#S
p#blic (oid p#sh.char element/ 2 stac1Arra%7==topOf'tac18 :
element9 <
p#blic char pop./ 2 ret#rn stac1Arra%7topOf'tac1;;89 <
p#blic boolean ismpt%./ 2 ret#rn topOf'tac1 > ?9 <
p#blic boolean is*#ll./ 2 ret#rn topOf'tac1 :: stac1Arra%. length ; 19 <
<
'M" $otation )or Class
Char'tac1
A**reviated Form
@ $%pes Of Class -eclarations
Char'tac1 Class name
stac1Arra%
topOf'tac1
p#sh./
pop./
ismpt%./
is*#ll./
Expanded Form
Fields
Methods
O*+ects

$he Process of creating objects is called instantiation.

Object m#st be created before is can be #sed.

6eference (al#e is ret#rned "hen an object is created.

+n JAVA, Object can onl% be manip#lated (ia its


reference.

A process of creating object in(ol(es follo"ing steps.


,aria*le #eclaration
CharStac- stac-./ stac-01
Creating an O*+ect 2'sing ne3 -e43ord5
stac-. 6 ne3 CharStac-2.751
Stac-06ne3 CharStac-2851

ach Object has its o"n cop% of felds.


Arg#ments to Constr#ctor
Constructor

A Constr#ctor m#st ha(e the same name as the


class its in.

Ahen a ne" instance of an% class is created, a


constr#cted is called.

+t is #sed to initialiBe objects.

#e)ault Constructor: +f no constr#ctor is created,


defa#lt parameter less constr#ctor is created b%
compiler.

$he frst line of a constr#ctor m#st either be a call


on another constr#ctor in the same class .#sing
this/, or a call on the s#perclass constr#ctor .#sing
s#per/.

-iCerence Bet"een Constructor and Method 9


O*+ect Aliases

Object can be referred b% se(eral references.

'#ch references are called as Aliases.

'#ch an OBJC$ can be manip#lated (ia an% its


Aliases.
Char'tac1 stac1A : ne" Char'tac1.1@/9
Char'tac1 stac1B : ne" Char'tac1.D/9
stac-: 6 stac-A1 2.5 aliases a)ter assignment

Ahat abo#t )emor% allocated to OBJ Estac1BF G

A#tomatic 0arbage Collection.


O*+ect Aliases
:e)ore Aliases
A)ter Aliases
!nstance Mem*ers

*ields of an objects are called !nstance


,aria*les.

)ethods of an object is called as !nstance


Method;

)ethod pertain to each object of a class, not to


get conf#sed "ith the implementation of method,
"hich is shared b% all instance of a class.

+nstance Var. and )ethods collecti(el% called as


+nstance )embers, diCerent from 'tatic )embers.
!nvo-ing Methods

Objects comm#nicate b% )essage passing.

$his is done b% calling a method on the object #sing


the binar% inf& dot .H.H/ operator.

A )ethod Call m#st ha(e *ollo"ing parts.

5ame of an object, that is recei(er of message,

5ame of the method to be in(o1ed,

Arg#ment to be passesd .+* A5I/.

$he method in(o1ed on the recei(er can also send


information bac1 to the sender, (ia a single ret#rn
(al#e.
CharStac- stac- 6 ne3 CharStac- 2851 Create a stac-
stac-;push 2<J<51 2.5 Character <J< pushed
stac-;printStac-Elements 251 205 Compile=time error: $o such
method in CharStac-
Static Mem*ers

-oes not belong to an% object, belongs to class


onl4.

*or instance, A Class "ants to 1eep trac1, no. of


objects created.

'#ch a co#nter can not belonged to an% object.

,se Static 1e% "ord.

'tatic (ariables are initialiBed at r#n time, "hen


class is called and defa#lt (al#e is J6O.

'imilarl%, methods can also be defned as static.


Summar4 OF
%erminologies
!nheritance

!nheritance and Aggregation : @ mechanism to


b#ild class from f#ndamental classes.

*or +nstance, class Car2derived5 can inherit class


,ehicle2:ase5;

+n Ja(a, deri(ing a ne" class from an e&isting class


reK#ires the #se of the e&tends cla#se in the
s#bclass declaration.

A s#bclass can e&tend onl% one s#perclass.5o


)#ltiple +nheritance/. $he s#bclass can inherit
members of the s#perclass.

!o"e(er, )#ltile(el inheritance is possible.


Example
// Source Filename: CharStack.java
p#blic class Char'tac1 2
// Instance variables
protected char78 stac1Arra%9 // The array that implements the stack.
protected int topOf'tac19 // The top of the stack.
< // The rest of the definition is the same as in prevoius Example
//Filename: PrintableCharStack.java
p#blic class PrintableChar'tac1 e&tends Char'tac1 2 33 .1/
// Instance method
p#blic (oid print'tac1lements./ 2 33 .@/
for .int i : ?9 i >: topOf'tac19 i==/
'%stem.o#t.print.stac1Arra%7i8/9 33 print each char
'%stem.o#t.println./9
<
33 Constr#ctor calls the constr#ctor of the s#perclass e&plicitl%.
PrintableChar'tac1.int capacit%/ 2 s#per.capacit%/9 < 33 .L/
<
Aggregation

B#ilding ne" classes from e&isting classes #sing


aggregation/ a composite object is b#ilt from the
elemental objects that are its parts.

*or +nstance, Class ,ehicle has se(eral parts,


therefore object of (ehicle is composite object.

b#ilding ne" classes from e&isting classes #sing


aggregation, a composite object is b#ilt from the
constit#ent objects that are its parts.

+n contrast to the constit#ent objects "hose


reference (al#es are stored in felds, the (al#es of
primiti(e data t%pes are themsel(es stored in the
felds of the composite object.
%enets O) JA,A

Code in JAVA m#st be encaps#lated in class.

$here are t"o 1inds of (al#es in JAVA: O*+ects /


that are instance of class or arra% A5- atomic
values of primiti(e data t%pes.

&e)erence denotes objects and are #sed to


manip#late objects.

Objects in ja(a can not contain other objects, the%


can onl% contain reference to other objects.

-#ring &ec#tion, reclamation o) o*+ects that


are no longer in #se are managed b% the r#ntime
s%stem.
Java Programs

ach so#rce fle name has a ;+ava e&tension.

A 'o#rce fle can contain more than one class, B#t


in each so#rce fle A%MOS% one p#blic class can
be there.

ach class declaration in a so#rce fle is compiled


into a separate class fle, containing Ja(a b%te
code.

$he name of this fle comprises the name of the


class "ith .class as its e&tension.

$he classes in the Ja(a standard librar% are alread%


compiled, and the J-M tools 1no" "here to fnd
them.
Sample Program
public class CharStack // Source Filename: CharStack.java
// Same as in previous !"ample #.$.
%
public class Client //Filename: Client.java
public static void main&Strin'() ar's*
CharStack stack + ne, CharStack&-.*/
Strin' str + 01no tis ot nuf era skcatS0/
int len'th + str.len'th&*/
System.out.println&02ri'inal strin': 0 3 str*/
for &int i + ./ i 4 len'th/ i33*
stack.push&str.char5t&i**/
%
System.out.print&06eversed strin': 0*/
,hile &1stack.is!mpty&**
System.out.print&stack.pop&**/
%
System.out.println&*/
%
%
Summar4 o) chapter
$he follo"ing information "as incl#ded in this
chapter:
N Basic concepts in OOP, and ho" the% are s#pported
in Ja(a
N ssential elements of a Ja(a application
N Compiling and r#nning Ja(a applications

You might also like