You are on page 1of 28

Object Oriented Programming

O Chapter 1

O Main Concepts of OOP

P
2012

Prepared & Presented by:


Mahmoud Rafeek Alfarra
O
O
O
O
PP
Contents

1 What is OOP?

2 Procedural VS OO

3 Main Concepts

4 Focus on Class & Object

5 Access modifiers

6 Access methods

http://mfarra.cst.ps
O
O
O
O
PP
What 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.
## of
oflegs
legs
Chairs
Chairs
Type
Typeof
ofmaterial
material
Tables
Tables Color
Color
Teacher
Teacher
PCs
PCs ..
.. ..
..
..
http://mfarra.cst.ps
O
O
O
O
PP
What 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
O
O
O
O
PP
Procedural VS OO

 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.

http://mfarra.cst.ps
O
O
O
O
PP
Procedural VS OO

 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.

http://mfarra.cst.ps
O
O
O
O
PP
Main Concepts

OOP

http://mfarra.cst.ps
O
O
O
O
PP
Abstraction

 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
O
O
O
O
PP
Encapsulation

 Units (classes) normally hide the details of


their implementation from their clients.

http://mfarra.cst.ps
O
O
O
O
PP
Inheritance
 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
O
O
O
O
PP
Polymorphism

 Polymorphism enables us to "program in


the general" rather than "program in the
specific.

http://mfarra.cst.ps
O
O
O
O
PP
Diagram of program structure
Program

File File File


Class
Variables

Constructors Variables File


Statements

Methods Variables
 A program
Statements
consists of one or
more classes.
 Typically, each
class is in a
separate .java file.

http://mfarra.cst.ps
O
O
O
O
PP
Class & Object

 In your class room there is … tables,


chairs, students.
 Each one of them is object from class.

http://mfarra.cst.ps
O
O
O
O
PP
Class & Object

http://mfarra.cst.ps
O
O
O
O
PP
Class & 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
O
O
O
O
PP
Thinking to build class …

Each
Eachone
oneisispresented
presentedas as
aavariable
variableininthe
theClass
Class Attributes

Any Thing
Each
Eachone
oneisispresented
presentedasas
aamethod
methodininthe
theClass
Class Behavior

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.

http://mfarra.cst.ps
O
O
O
O
PP
How to build my class?

Is a reserved word

The identifier of class


Must be as any variable

Access_modifiers
Access_modifiersclass
classclass_name
class_name{{
//
//variables
variables==attributes
attributes
Access_modifiers
Access_modifiersclass_name()
class_name(){{

}}
//
//behavior
behavior==methods
methods Always, the class has
a method called constructor
}} which gives initial values
to the attributes of class

http://mfarra.cst.ps
O
O
O
O
PP
My first class …
class Rectangle {
public float length;
public float width; Attributes(properties, instance variables)
of rectangle class
public float area;

public Rectangle(){
length = 1.0f;
Default Constructor of class
width = 1.0f; It just gives any values to properties
area = recArea();
}

public Rectangle(float l, float w){


length = l; Overloaded constructor of class
width = w; It gives values to properties by user
area = recArea();
}
public float recArea(){ Method to calculate the area of rectangle
return length*width; Any rectangle has an area behavior
}
} http://mfarra.cst.ps
O
O
O
O
PP
Access 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
O
O
O
O
PP
Access 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.

A member has package or default accessibility when no


accessibility modifier is specified.

http://mfarra.cst.ps
O
O
O
O
PP
Access 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.

A standard design strategy is to make all fields private


and provide public getter methods for them.

http://mfarra.cst.ps
O
O
O
O
PP
Access 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
O
O
O
O
PP
Access 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
O
O
O
O
PP
Access modifiers

http://mfarra.cst.ps
O
O
O
O
PP
Access methods

 While properties should be private, so we need to


available using them through methods, which called
access methods.
Access
methods

setData getData
public void setData(data){ public data getData(){
This.data.data; return data;
} }

http://mfarra.cst.ps
O
O
O
O
PP
Access methods
class 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;
}
} http://mfarra.cst.ps
‫‪O‬‬
‫‪O‬‬
‫‪O‬‬
‫‪O‬‬
‫‪PP‬‬
‫استغفروا ربكم‬

‫قال ا تعالى ذكره‪:‬‬

‫ن ال ّل هُ‬‫وما كا َ‬ ‫َ}‬
‫ت ِفي ِه مْ‬
‫م َوأ ْن َ‬ ‫ِل ُي عَ ّذ َب ُه ْ‬
‫ن ال ّل ُه‬ ‫َوما كا َ‬
‫ه مْ‬ ‫م َو ُ‬ ‫ُم عَ ّذ َب ُه ْ‬
‫ن{‬
‫غ ِف ُرو َ‬ ‫س َت ْ‬‫َي ْ‬
‫‪http://mfarra.cst.ps‬‬
O
O
O
O
PP
Thank You …

QUESTIONS?
http://mfarra.cst.ps

You might also like