مقدمة في علم الحاسوب مفاهيم أساسية في تكنولوجيا المعلومات IT
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
-
Upload
mahmoud-alfarra -
Category
Education
-
view
283 -
download
19
Transcript of البرمجة الهدفية بلغة جافا - مفاهيم أساسية
OO
PMain Concepts of OOP
Object Oriented Programming
Prepared & Presented by: Mahmoud Rafeek Alfarra
2012
Chapter 1
OOOO
PP
http://mfarra.cst.ps
Contents
What is OOP?1
Procedural VS OO2
Main Concepts3
Focus on Class & Object4
Access modifiers 5
Access methods 6
OOOO
PPWhat is OOP?
To have a fine definition of OOP, Please note what you are showing in your class room now?
Nice, what is the properties and behavior of each of them ?
this is the OOP style.
http://mfarra.cst.ps
ChairsTables Teacher
PCs..
ChairsTables Teacher
PCs..
# of legs
Type of material
Color
.
.
.
# of legs
Type of material
Color
.
.
.
OOOO
PPWhat is OOP?
Object-oriented programming (OOP): A type of
programming in which programmers define not only the
attributes, but also the types of operations that can be
applied to the data structure.
In this way, the application becomes a set of objects that
includes both data and functions.
http://mfarra.cst.ps
OOOO
PPProcedural VS OO
http://mfarra.cst.ps
In a way, procedural programming could also be
called linear programming. One thing happens and then
the next. Code is executed from the top of the file to the
bottom.
OOOO
PPProcedural VS OO
http://mfarra.cst.ps
In OOP Code is often broken up and distributed across
multiple files, each one with a single purpose.
OOP is also more abstract than procedural
programming because it looks for patterns and
reusability.
The same code can be loaded and executed many
times to accomplish a task without having to retype it.
OOOO
PPMain Concepts
http://mfarra.cst.ps
OOP
OOOO
PPAbstraction
Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
i.e: The client cares about what functionality a car offers, not about how that functionality is implemented.
http://mfarra.cst.ps
OOOO
PPEncapsulation
http://mfarra.cst.ps
Units (classes) normally hide the details of their implementation from their clients.
OOOO
PPInheritance
Inheritance: is a form of software reuse in which a new class is created by absorbing
an existing class's members and embellishing them with new or modified
capabilities.
http://mfarra.cst.ps
OOOO
PPPolymorphism
Polymorphism enables us to "program in
the general" rather than "program in the
specific.
http://mfarra.cst.ps
OOOO
PPDiagram of program structure
A program consists of one or more classes.
Typically, each class is in a separate .java file.
Program
File File
File
File
ClassVariables
Constructors
Methods
Variables
Variables
Statements
Statements
http://mfarra.cst.ps
OOOO
PPClass & Object
In your class room there is … tables,
chairs, students.
Each one of them is object from class.
http://mfarra.cst.ps
OOOO
PPClass & Object
http://mfarra.cst.ps
OOOO
PPClass & Object
A class is a description of a kind of object
In most cases, it is the objects that do the
actual work.
A class describes data, constructors, and
methods.
http://mfarra.cst.ps
OOOO
PPThinking to build class …
http://mfarra.cst.ps
Any Thing
Attributes
Behavior
Each one is presented as a variable in the Class
Each one is presented as a variable in the Class
Each one is presented as a method in the Class
Each one is presented as a method in the Class
A new class will be considered as a new data type, so you can declare a variables (Objects) of them and then
you can set and get data to its properties.
OOOO
PPHow to build my class?
http://mfarra.cst.ps
Access_modifiers class class_name {// variables = attributes Access_modifiers class_name() {
}// behavior = methods
}
Access_modifiers class class_name {// variables = attributes Access_modifiers class_name() {
}// behavior = methods
}
Always, the class has a method called constructorwhich gives initial values to the attributes of class
Is a reserved word
The identifier of class Must be as any variable
OOOO
PPMy first class …
http://mfarra.cst.ps
class Rectangle {public float length;public float width;public float area;
public Rectangle(){length = 1.0f;width = 1.0f;area = recArea();
}
public Rectangle(float l, float w){length = l;width = w;area = recArea();
}
public float recArea(){return length*width;
}}
Attributes(properties, instance variables)of rectangle class
Default Constructor of classIt just gives any values to properties
Overloaded constructor of classIt gives values to properties by user
Method to calculate the area of rectangle Any rectangle has an area behavior
OOOO
PPAccess modifiers
The access to classes, constructors, methods
and fields are regulated using access modifiers.
i.e. a class can control what information or data
can be accessible by other classes.
To take advantage of encapsulation, you should
minimize access whenever possible.
http://mfarra.cst.ps
OOOO
PPAccess modifiers
Java provides a number of access modifiers to
help you set the level of access you want for
classes as well as the fields, methods and
constructors in your classes.
http://mfarra.cst.ps
A member has package or default accessibility when no accessibility modifier is specified.
OOOO
PPAccess modifiers
4 Access Modifiers:
1. public: are visible to any class in the Java program,
whether these classes are in the same package or in another
package.
2. private: they cannot be accesses by anywhere outside
the enclosing class.
http://mfarra.cst.ps
A standard design strategy is to make all fields private and provide public getter methods for them.
OOOO
PPAccess modifiers
4 Access Modifiers:
3. protected: Fields, methods and constructors declared
protected in a superclass can be accessed only by
subclasses in other packages.
Classes in the same package can also access protected
fields, methods and constructors as well, even if they are not a
subclass of the protected member’s class.
http://mfarra.cst.ps
OOOO
PPAccess modifiers
4 Access Modifiers:
4. default: when no access modifier is present, any class,
field, method or constructor that has no declared access
modifier is accessible only by classes in the same package.
http://mfarra.cst.ps
OOOO
PPAccess modifiers
http://mfarra.cst.ps
OOOO
PPAccess methods
While properties should be private, so we need to
available using them through methods, which called
access methods.
http://mfarra.cst.ps
getDatapublic data getData(){ return data;}
setDatapublic void setData(data){This.data.data;}
Access methods
OOOO
PPclass Rectangle {
private float length;private float width;private float area;public Rectangle(){
length = 1.0f;width = 1.0f;area = recArea(); }
public Rectangle(float l, float w){length = l;width = w;area = recArea(); }
public float recArea(){return length*width; }
public void setLenghth(float length){ this.length = length;}public float getLength(){
return length;}
}
Access methods
http://mfarra.cst.ps
OOOO
PPاستغفروا ربكم
: ذكره تعالى ا قال
ه { ل ل ا ن كا ومام ه في ت ن وأ م ه ب ذ ع ي ل
ه ل ل ا ن كا وماهم و م ه ب ذ ع م
ن رو ف غ ت س ني رو ف غ ت س }ي: ] 33النفال [
http://mfarra.cst.ps
OOOO
PP
QUESTIONS?QUESTIONS?http://mfarra.cst.ps
Thank You …Thank You …