#1 - OOPS Concepts in Java
#1 - OOPS Concepts in Java
-GPS
=> OOPS Overview
OOPs oriented
* means
object
real world
p rogramming
like Bike ele
* Here
object means
entity
Car
,
ATM
,
.
Procedural
I
OOPS
Programming
Program is divided into
parts Program is divided into
called
functions.
objects
freely.
and data moves
Overloading is not
possible Overloading is possible
present
-Pascal
Eg :
,
C
,
FORTRAN er .
Gg :- Java
,
C#
,
Python ,
C ++ c .
=>
Objects & Classes
*
Object has
2
things :-
Properties or State
-
Behavior or Function
For Example : -
*
Dog is an
object because : -
Properties like :
Age colour breed .
-
etc
, ,
,
.
Behavior like
Apply brake Drive , Increase speed .
etc
-
Class is
blueprint I skeleton
of
↓ a an
object.
To class is .
required
create
object
-
an a
,
,
class
template or
from
which an
object
can be created
we can
,
To create class
keyword class
-
a use :
,
Eg : -
Class Student
S
J Data
int
string
age ;
ariables
mama ;
address
string ;
update Address ()
J
Data
E
Method
get Age
3
return
age : I Data
Method
Student Student (C
engStu
= new
;
Data Abstraction
*
functionality
for the user
.
It achieved
through interface and abstract classes
* can be .
Example :-
we
:
,
we
press ,
That is ABSTRACTED
will reduce . How
! from us .
*
Advantages of
Abstraction : -
&
confidentiality
St increases
security
-
DEMO : -
1)
public apply Brake ;
"
3
Class
cargmp implements
Car
&
Brake
public
apply
E
Il Step -
1
11
Step -
2
11
Step- 3
!
3
3
from ultimately
all that is hidden the but
Step-e---but user
car
stops.
So this
improves
security
is the
of
as user not awa re
internal and
functionality only
knows about the result
.
2 Millar OOMS DATA ENCAPSOLATION
=>
of
-
a
single unit
.
a as
the variables
*
Advantages of encapsulation :-
-Better control $
security
.
access
DEMO :
class
Dog
S
private
string Dog ;
((
string get Colour
E
return this colour ;
,
colour)
void Set Colour
/string
E
this colour-colour ;
3
Now let's
object
of Dog type
create an
get ;
which
of
in turn have the access variable .
=> 3 unde
Lillar OOPS INTERITENCE
of
-
*
Capability of
a class to inherit
properties from
their parent class .
Can be achieved
*
using
extends
keyword through interface or .
*
Types of inheritance : -
Single inheritence
-
-
Multilevel inheritence
Hierarchical innecitence
diamond
through interface
to but solve
problem ,
we can
Inheritence
*
Advantages Of
Code
reusability
-
We inheritence
can achieve
polymorphism using
-
DEMO : -
Vehicle (Parent)
to
Class Vehicle
E Car 1 child)
boolean
engine ;
boolean ((
get Engine
E
return this
engine ;
3
3
&
string type ;
K
string getCarType
E
return this
type ;
3
let's
Car
Now
object
of
create an .
Car CarK
swift = new
;
;
:
getCarType)) ;
So since is extends
Swift object car which
g
,
an vehicle
it possible
get Engine
hence can call whereas vice versa isn't
*
Single
Class it
↓
Class B
* Multilevel
Class it
↓
Class B
↓
Class 2
* Hierarchical
Class it
~
I
Class B Class (
*
Multiple
21 ass A Class B
*
-
&S
&
Class (
-
I his is in Java due to diamond
not s
upported
workaround
problem but there is a
for it
using interfaces .
*
Poly means
"Many" &
morphism means "Form"
If in situation
different
*
differently
same method ,
behaves
*
Example :
it person husband
fathersolid employee
-
can be .
esc
, ,
*
Types of polymorphism : -
/
-
Run Time
Dynamic Polymorphism /Mechod
Overriding
DEMO :
class Sum
return a + b
;
3
E
return a + b + c ;
3
So this
of creating
methods with same but
practice name
Pass A
E Class B extends it
int ( E
get Engine
E int
getaugine((
S
return 1j
3 retur 2
;
3
let's
of
Now class i
create an
object
Bobj = new B 1) ;
11 This'll
obj getEngine) ;.
return 2
method
So in
oversiding.
,
overeiding everything ,
i.e.
arguments ,
return
type ,
* Is -
a
relationship
Achieved inheritence
through
-
Example is - a animal
-
: Doa .
Inheritence
from is a relation between its
parent child classes
-
an -
Has
* -
a
relationship
is used Other class it's calld HAS-A
Whenever
object in
-
an
, ,
relationship
.
Relation Ship could
one-to-many many-to-many
-
be one-to-one ,
,
Example
·
School has students
Bike
engine
·
has
Aggregation Both
objects
survive
individually means
·
-
can
,