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

ASSAM_POLICE-GS_EXAM

The document discusses Object-Oriented Programming (OOP) concepts in Java, highlighting key principles such as Encapsulation, Inheritance, Polymorphism, and Abstraction. It provides examples of each principle, demonstrating how they are implemented in Java code. Additionally, it explains the difference between instance and static members in classes.

Uploaded by

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

ASSAM_POLICE-GS_EXAM

The document discusses Object-Oriented Programming (OOP) concepts in Java, highlighting key principles such as Encapsulation, Inheritance, Polymorphism, and Abstraction. It provides examples of each principle, demonstrating how they are implemented in Java code. Additionally, it explains the difference between instance and static members in classes.

Uploaded by

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

cIASSMAte

Date :
Page:

OBJECT-ORIENTED PROGRAMMING
(oOPS) CONCEPTS IN JAVA
Obiect- (riented Programming (00P) is a design
programming
around
paradigm that organizes software
data, or abjects rather than tuncions and
logic
Jaya is1San object -oriented language that ollows
key 00OP principles such as Encapsulatiom ,
Inherilence Polymorphism and Abstracion.

1 Principles O0P n Java:


a) Encapsulation
|Encapsulatin themehe
and
1S
concept wrapping
(Tuncions)
The
that
data (variables) änd methods
operate am the data into single unit called
aclass.
The class's data is hidden trom direct
access by other claSses and is amly
only zccess
ible through public geer /seter methods.

@urious- programmer
clASSMAte
Date :
Page:

Example:
class Persom
private String nameWprivate Variabie
Cencapsulation)
/ Public Qetter method for accesSing private variable
public String aetName ()L
retun name

/ Publie setter metthod for moditying privak vartable


void
public setName (sting mame)
this. name 3 Mame j

b) mheritexnce
Inheritence allouws ome class Cchild class) to inherit
the properies and behaviors o another class
(parentclass). This promotes
DL ode reuse and
the creation CH 'hierarchial class structure.

uriQUs -programmer
Class Animal
classExample
sound)void
Dog Systcm-out-
Systerm.out
@
mmer +Sournd)i extends
|sOumd
Void):
Dog Animal prinin
printn
Animal
Void ((Animal
("Dog
Overridden
method.
+Child+ <-Parerit
Method class
<- barks makes
Page: Date :
cIASSMAte
") a
dass saund");
clASSMAte
Date :
Page:

9Polymorphism
Palymorphisrn allaws methods to behave differently
deperndng
his
on the object that invokes them.
can be achieved through Method
Overloading Ccompile time) and Method
Overriding (runtime palymorphism).
Example Method Oerloading
class Calculator
int add Cint a ,int b) {
retun atbi

Method overloading Same Ymethod name uit


diterent paramelers.
couble add (double a double b) E
retun a+bj
}

Caleulator
add (int int) + Method Overloading
add (double ,double)
ki( Calulator
@urious -programme
Soumd") +chld
class
(overriding
cIASSMAte Sund))
a class
Parert
4
;") method.
Overridden
Date:
Page makes Method
¢ @
urious-pro
barks
Animal
("DoQ
Example
Method
Overriding
(" Arimal
System:outprintln println Sound void
Void
(): Sound():
soundC)s
Void
Extends
sound
()YOid System.out Animal
@Override Dog
class
Arimal
Dog
Class
features
method
clASSMAte
be
achieved
using shape")
essential icircle")
Page: LAbstract
Date
the programmer
the interfaces. Caloring Drauing
()i &
only cass: (" Shape ("
concept voiddrauw -
out-printn uriUs
can -printn
exposing Abetractabstract
cdass
Shape extonds()S
daw
Void
the lhisabstract
classes @
))Abstraction: voidsystcm.out
color
()
Circle Systm.
is abstract Oyerride
andobject wth
Abstraciom
Example Class
details
an
H
" Class: " 2.
3 public
dass classCar Example : Object
public start
void )
yearint
Classes
Carl. car
startO; Car SysBm. madel
String
j i
canand:
model
Carl .
static (methods).
properties A
An
year l.
car1
Main out & instance blueprint and
programmer -
voi=d @urias-printm
:2022i new
main perfom
actians. (atributes) Objects
"Tesla"j fa o for
Car ("Car
lcalling
methodthe (String creaking
() class
j is
lcreaing Ll starting")
snd
args) that objects
behaviourS Page Date :
IASSMAte

an holds
object defirning
data
used
initialize
to
Variable
+Instance
crended is retun
type.
cIASSMAte Car
Class object
performs
value.
:DatePage:
Method Object method
used
+ Consiructorsi
acan
retun an
a @urious-p
that have
start
()method when
Carl model
"Tesla"
=
(Object)
2022
=
year code mot
-m odel: Void
String A called
Constructor:
specal
ot does
Car
clasS int : and
() blockand
task
is and
:
year start 4
Methods
A .
- + created
objects
Method:
3.
"
cIASSMAte
args)lcomstrucor
called
25);
:DatoPage:
¬ [1 ,John"
age) String
@rious
-programmer
int ( Person
main
,
this
Mame
=Mame;
Mame agei
Canstrudor
fExanmple. voidnew
name;
Main
class
public
=
Person
(String = static
Person
pl
int
age
i Comshructorage
class
Persn
String this. public
M
instancethis
Example: instance Keyword
|The t|h4i.s
CString
model)
Car Constructor class
Car ¬ hey
using Il
Hhis instance
String have
model'
Whis.
reicns Variables
keyword
refers
is
It
nodel variable nmodeli
"model' the
-programmer O
= diffexentiate.
Same used to
sa
ismodel and this' methodand
i name.
the parameter the to
instance
variable to differentiate
between to
paranmetot
cuTrent
object
parameters
when Page: Date :
clASSMAte

between
cIASSMAte
Date :
Page:

5 Static and mstance Members.:


" nstance Members: Belomg to an object and are
created when the cbject isis instantiated
Slatic Members Belong to the class tself nd
3re shared acros all objecs.
Example
class Counter &
int instance Count =0; / nstange variable.
static int static Court ==0; Il statte variable.
JMethod to increment counts.
Voicd increment 0E
instantce Count +tj
stati cCount ++i

@ uriaLS- programmer
public class Main &
public static void main (Shing Il args)¬
Counter c1= mew Counter (;
S;
Counter c2 = new Counter ()i

cl.incremernt C);
c2.increment (;
cIASSMAe
Ciate

System out printlm ("Instane ount for cd :"+


c1. instance Count : /1
System. out printn ("Instance Cant for c2:"+
c2. instance Caumt ); Il1
System. out printn ("Static Cant (shared across
instances): " + Cournter. Staticlount) ; l/2
}

Q urioUs -programmer

You might also like