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

#1 - OOPS Concepts in Java

Uploaded by

achieve2025ssc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

#1 - OOPS Concepts in Java

Uploaded by

achieve2025ssc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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

Doesn't provide to hide


wayto
a
Objects provides data hiding
,
data data
, gives importance function gives importance to .

freely.
and data moves

Overloading is not
possible Overloading is possible

Inheritence is Inheritence possible


not
possible is

Code does Code


reusability not
reusability
is
present .

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 Bark eat


:
sleep exc
-

,
.

* Car is because it has


an
object : -

Properties like Colour


, Type ,
Brand
,
weight
esc
.

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
,

So provides the blueprint


-

,
class
template or
from
which an
object
can be created

From class creat


multiple objects
.
-

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

Now let's typeStudent.


of
create an
object

Student Student (C
engStu
= new
;

Data Abstraction
*

=> B Pillar of OOPs -

* It hides the internal implementation


only
and shows essential

functionality
for the user
.

It achieved
through interface and abstract classes
* can be .

Example :-

Car BRAKE and it speed


pedal if
car
onlyBut
show the
-

we
:
,
we
press ,

That is ABSTRACTED
will reduce . How
! from us .

Cellphone : how call is made that its ABSTRACTED to .


us

*
Advantages of
Abstraction : -

&
confidentiality
St increases
security
-
DEMO : -

Interface Car &

1)
public apply Brake ;

public inc Speed 1)


;
public handbrake));

"
3

Class
cargmp implements
Car

&
Brake
public
apply
E
Il Step -
1

11
Step -
2

11
Step- 3

!
3
3

So when user calls


apply
Brake))
, internally it's
invoking Step-1,

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
-

Encapsulation bundles the data code


working on that data in

a
single unit
.

* Also known as DATA-HIDING


*
Steps to achieve encapsulation
Declare variable
of private
class
-

a as

Provide public I view the


getters setters to
modify values
y
-

the variables

*
Advantages of encapsulation :-

Loosely coupled code


-

-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

Dog lab = new


Dog();
lab .
Set Colour ("black") ;
lab Color 11 will return black
-

get ;

So here haven't the


we
given the access
of
variable colour

did it with the


of class
dog
.
Instead we
help of the getter setter

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 .

It inherit both don't


* can
functions and variables so that we

have for write them in the child class


again .

Can be achieved
*
using
extends
keyword through interface or .

*
Types of inheritance : -

Single inheritence
-

-
Multilevel inheritence

Hierarchical innecitence

inheritence (Not supported due


actually
Java
Multiple
by
-

diamond
through interface
to but solve
problem ,
we can

the diamand problem .

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

Class Car extends Vehicle

&

string type ;
K
string getCarType
E
return this
type ;
3

let's
Car
Now
object
of
create an .

Car CarK
swift = new
;

Swift get Engine


1)
-

;
:

Vehicle vehicle new Vehicle 1)


=
;
11
-

Should not work


Vehicle .

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 .

=> 42 Millar OOPS POLMORPUISM


of
-

*
Poly means
"Many" &
morphism means "Form"
If in situation
different
*
differently
same method ,
behaves

*
Example :

it person husband
fathersolid employee
-

can be .
esc
, ,

Water can be liquid ,


or
gas
.

*
Types of polymorphism : -

Compile Time /Static Polymorphism /Method


Overloading
-

/
-
Run Time
Dynamic Polymorphism /Mechod
Overriding
DEMO :

class Sum

int doSum (inta in + b)


,

return a + b
;
3

int doSum (inta intb , inte)


,

E
return a + b + c ;
3

So this
of creating
methods with same but
practice name

different perometers is ka overloading .

Bo the method will be called based on the


parameters
.

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

So which method to call is decided at runtime this is Kla

method

So in
oversiding.
,
overeiding everything ,
i.e.
arguments ,
return
type ,

method name is same


.

* 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

School has classes


·

Association relationship different objects


between I
-

Aggregation Both
objects
survive
individually means
·
-
can
,

will not end another


ending of one
object object .

Composition Ending of object will end another .


object
·
-
one

You might also like