You are on page 1of 56

: OOP

Object Oriented Programming


OOP

83

: OOP

) (
Object Oriented Programming
OOP
-:

,

.
Machine
Language .
,
,
Assembly language
.
, .
High Level Language
) (.......- ,

Objects
). (OOP

:
-1 .
-2 .
-3 .

84

: OOP

-4 .
-5 .
-6 .

:
-1 : abstraction
-:
- : Data abstraction
.
- :Methods abstraction
.
-2 ) ): Encapsulation properties
( Methods ) ,
.
) (
-3 :Data Hiding .
.
-4 .:Inheritance
.
-5 ) ( :polymorphisms
.

85

: OOP

2-1 ( ( Object ,Classes

:
. )
(Objectes ) (Classes

2-1-1 Object
) --
. (......-
.
. ) = (
) -(
, .
.. Objects )(1-2
:

)(1-2
:
-1 ) (
) (
-2

86

: OOP

:

) (Car) (2-2
:

)-2
(2

) ( )( ) (3-2 :

)(3-2
) (Mobile :

87

: OOP

) (4-2 .

)(4-2

88

: OOP

Object :
) (Attributes )
(Methodes .
objects

, .

Attribute : methodes
..
)( .
, ) : : (......


.
. )( .
)( .. , )(
.
. = 60
60 , .

2- 2-1 Classes
) Class(
Object Class
. Class
)( Object
Methods Attribute )
(.
Class .Object

89

: OOP

) (Class :

) (..... -
) (.....
) (Class Object
.
) (Class
object ). (Class
Class object ) (5-2 :

)(5-2
,
)( .

90

: OOP


)( .

Classes Types
) (Classes :
(1 :
object )Built-in
. (classes
(2 :
object ) .(User Defined Classes

:Class
Class -:
(1 : Variables .
(2 : Methods
.
:

91

: OOP

2-2 -:Objects
Objects .
)(1
:

92

: OOP

) , (main
. main
-

Rectangle ). (1

) (2 .

) (3 ) Method( area
.


. main

) (8 . main
) (10 Object Rectangle . ((C

Object : Class

.
) (11
Method Rectangle ) (3
) (6-2 :

93

: OOP

)(6-2
.

94

: OOP

). (The Area Is = 30
area :

95

: OOP

)(2
Class Object Method .

96

: OOP

)( . Tree
Objects ) (7-2
:

)(7-2
.
Class :

:
-1 ) (4-1 .

97

: OOP

-2 ) (6 . class Tree
-3 ) (7 height int
, public ,
.
-4 ) (12 ) 1(.
Tree
. main
Tree.java
.
.

main
. PlantTrees
- main :

98

: OOP

-:
-1
main PlantTrees
. Tree

99

: OOP

-2 ) (4-1 .
-3 object Tree . tree1
-4 ) (11
tree1.height Tree object
, tree1 . height=height+1 height
). ((meter(s 1
-5 ) (12 . method grow
-6 ) (13 tree1.height
). ((meter(s 2
-7 ) (15) (14 ) Objects (2 tree2
tree3 . tree1
-8 ) (16 ) (20 . grow
-9 ) 21 (24 .
:

2-3 : Constructor

100

: OOP

object Class
.
:
-1 .
-2 .
-3 constructor return . void
-4 new .
-5 . Constructor
-6 . constructor
Constructor ,
.


. .

100 70 140 .. :
.
= 0 = 10 = 1-

170
.

. = 0
170
.

...

101

: OOP

.

...
:
= 0
= 1
...
: Constructor
...

Constructor -:


return object
.

Constructor :

102

: OOP

Constructor myclass
return .
return
Method .
object
main object:

103

: OOP

main object class myclass


) (8-2 object :

)(8-2
object object name class
, myclass . myclass
, object object
. class

.. . destructor

104

: OOP


,
. . Destructor

2-4 Classes . Java Libraries


Classes . Packages
,
.

.
Sun
:
http://java.sun.com/j2se/1.5.0/docs/api/index.html


) . (import .
, import
java.lang
.
import .
: import
import
. :

105

: OOP

import include ) (++C


.

.
:

)
(1 ) (util
( . (Scanner
Math
.
.
)*( :
;*.import java.util
) (9-2 SUN
.

106

: OOP

)(9-2

107

: OOP

)(3
.
:

.

) (util.Scanner . Scanner
) (lang.Math Math
) (pi pi=3.14
. Math

:

108

: OOP

) ( result double

2-5 .
:

, .
Reference Variables
:
) (int a , float b , char c
Stack .
Reference Variables Heap
Objects :

109

: OOP

Stack . int a
Stack Card Car
, Heap ) (d
Reference Object , heap
) (d Reference
Variables
Object . Heap
) (10-2 object
Car :
;()d.speed
speed ) (d Car
)( .
primative

110

: OOP

.
)(10-2
Reference type :

:
-1 ) (c Car

-2 object . new

-3 object ). (c

Reference

:Variables
-1 ) (11-2 ) (b ) (c Book
:

111

: OOP

)(11-2
-2 ) (d Book ) , (c
) (c )
(d Object 2 ): (12-2

)(12-2
:

112

: OOP

;Book c=d ) (c )(d


. :

.
-3 )(13-2

113

: OOP

)(13-2
, c=b
) (b ) . (c ) (c object 2
.

-1

) (c,b

) (14-2 :

)(14-2
) (b=c
-2
) (b
) (15-2 :
object 2

114

: OOP

)(15-2
: ) (b,c

, object2 Object 1
object 1 .
. Garbage-Collector
) . ( c=null

-3

) (c
). (16-2

)(16-2
) (c ) (b object2
object2 ) (b .object2
: object2 heap


Garbage
Collector .

115

: OOP

) (c,b,d
. stack object heap .

: Variable Life Time

: )(RAM
.

.
-1 : Static Variables
class . class
class .
-2 : Local Variables
) (Block .
) (Block
.
-3 : Parameter Variables
Method
Method .

-4 : Instance Variables
class .
object .

Static Methods

116

: OOP

) object ( ,
,
.

static , static
. methods
: static
{public class A
{)public static void methodA(...
...
}
{)public void methodB(...
...
}
{
static methodA OBJECT
:
(...)A.methodA
Static :
;(System.exit(0
)(4
Static Methods
:

117

: OOP

,
) (11 area static
.
) (19 volume static
.
) (6 ) (PI
. ((PI=3.14

.RoundStuff.java .

118

: OOP


. .RoundStuffDemo.java

.
) (8,7,6 .
) (10,9 .
) (11 area
(RoundStuf.area(radiuas radiuas .
) (16,15 volume
(RoundStuff.volume(radius radius
.

119

: OOP

:
:Enter radius
2
A circle of radius 2.0 inches
.has an area of 12.56636 square cm
A sphere of radius 2.0 inches
has a volume of 33.51029333333333

2-6 passing object to method


object .
)(5
.
:

:
- 1,2 .

120

: OOP

3 ) (q method1 (object (p ) (i ). (q
:
)(6

:

Names name string ). (1


) (2 ) . ( f ) (3 object . Names ) (4 ahmed . name
121

: OOP

) (5 x.name .Ahmed ) (6 ) (x ) (f name Ahmed . magdy


) (7 . magdy :

2-7 Variables Scope


.
) (instance Variables Method
.
Block .

.
scope .
.
.



) (Public

) ( Private
) . (Protected )
. (final

122

: OOP

:
-1 public .
-2 private }{
.
-3 protected .
-4 final Object .
-5 abstract .Object
)(17-2

)(17-2
Modifiers )(
Method constructor
Modifiers .
) (public . constructor
)(18-2 ) Modifiers(

123

: OOP

METHOD

CLASS

modifier

Variable


)public (A




) )

protected
)(A

)blank (A
)"("friey

private
)(A

there is only
one per
class but
still there
could be
more than
one per
JVM

cannot be
instantiated, are
called by
classname.method,
can only access
static variables

used to declare a
toplevel class as
opposed to an
inner class

not
applicable

Method is defined
but contains no
implementation
code
(implementation
code is included in
the subclass). If a
method is abstract
then the entire
class must be
abstract.

cannot be
instantiated,
must be a
superclass, used
whenever one or
more methods
are abstract

abstract

final

Static

124

: OOP

-:
Member variables-1 Instance variables
Class variables-2 Static variables
Automatic variables-3 variables Local
-4 Constant Final variables
:
-1 Member variables : ) ( Instance variables

class :
} class Garage extends Building
;int floorArea
;int numberOfWindows

;String usage 125


{

: OOP

instance variables Instance .


Global . Instance

instance

:
} ([]public static main ( String args
;()Garage myGarage = new Garage
;"myGarage.usage = "junk storage
{
-2 : Static variables

, static Instance
:

} class Mm
;"static String model = "Mondeo
;in engineSize
{
...
;Mm slowCar, fastCar
;slowCar.Size = 1600
;fastCar.Size = 2800
;(System.out.printf("The model is " + slowCar.model
...

126

: OOP

-3 : Local variables
Methods .
Initialization
.
:
} ( public double forthRoot( double d
;dobule result = 0.0
;((result = Math.sqrt(Math.sqrt(Math.sqrt(d
;return result
{

: ) (y
). (if

127

: OOP

-4 : Constant
) ( final
:
;final limit = 100

.

2-8 This
(keyword (this Construction
Methods .
) (this )( .
this ; this.name=name : ) (name .
this )( .
)-: (this
-1 .
) (this :
public class MyClass
{
;int number
)public MyClass(int number
{
;this.number = number
}
{
.Constructor Method
MyCLASS . int number

128

: OOP

. int number
this this.number number .
number )=(
. :
{public class student
;private String name
;private int id
;private int mark
// this
{)public student (String name,int id,int mark
;this.name=name
;this.id=id
;this.mark=mark
}
// this
{)public void setName(String newName
;this.name=newName
}

Constructor
( class ( name,id,mark this
.
Student ) ( name
) . ( name
-2
this constructor

129

: OOP

:
) (2 constructor ) (2 ) (4
. myprog
) (1 myprog ) (2 myprog ).(start
) (4 ) (myprog . int b
) (5 ))( (this ) (myprog ) (2 ). (start
) (6 ) (b ) (b=100 ). )10000

130

: OOP

) (7 object ) (c myprog ) (100 ) (b myprog


). (4
:

start
10000

) (this

). (this
)(this .

2-9 )( Array Of Objects


, ,
) (.,int,float,char
:

131

: OOP

) ( :
;[array Name[ ]= new int[10
:

.

addresses object
) (heap
Refrance Variables
.

Array Of Objects :

132

: OOP

Book
Science :
;Book[ ] science
; [Science=new Book[7
Book ) (7
) (7 objects ) . (heap -:

object :

class object heap


.
.

133

: OOP

.
:
-1 Book ). (1
-2 name string
Instance variables . main
-3 ) (4 object . Book
-4 ) (5 ) (20
,
). (null
-5 ) (6 name )
(2 ". "JAVA

134

: OOP

-6 ) (7 Book mybook )
(3
-7 ) (89 object [mybook[0 [mybook[1
.
-8 ) (10 book1 . [mybook[2
-9 ) (1112 .
-10) (1314 .
-11 ) 15 (19 while loop .
) (16 length

.
:


null JAVA
.

135

: OOP


-1 ) (
-2
-3 Object
-4 Class
-5 Constructor
-6
-7 (keyword (this
-7 :
} public class Foo
;int i
;static int s
} (public static void main(String[] args
;()Foo f1 = new Foo
;(System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s
;()Foo f2 = new Foo
;(System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s
;()Foo f3 = new Foo
;(System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s
{
} ()public Foo
;++i
;++s
{
{

136

: OOP

-8 )( :

-9 myCount.count :

137

: OOP

-10 times )(

-11 [Circle[] x = new Circle[10 :


:

-12 :
-1 ) (Reference variables . Objects
-2 .
-3 .
-4 .

138

You might also like