computer programming 2 lecture 8: object oriented programming creating classes & objects...
DESCRIPTION
Out Lines Mahmoud Rafeek Alfarra Downloaded from Downloaded from 3 What is OOP? why? Main Concepts of OOP Thinking in Objects. Class & Object Access modifiers How To build Classes How To create ObjectsTRANSCRIPT
Computer Programming 2
Lecture 8:Object Oriented Programming
Creating Classes & Objects
Prepared & Presented by: Mahmoud Rafeek
Alfarra
MINISTRY OF EDUCATION & HIGHER EDUCATIONCOLLEGE OF SCIENCE AND TECHNOLOGY (CST)KHANYOUNIS- PALESTINE
و من يتق الله ...
قال الله تعالى:
)وإبراهيم إذ قال لقومه اعبدوا الله واتقوه ذلكم خير
لكم إن كنتم تعلمون( (16)العنكبوت:
Mahmoud Rafeek Alfarra
2
Downloaded from http://staff.cst.ps/mfarraDownloaded from http://staff.cst.ps/mfarra
شريحـة ثابتـة لعلنا نحسن من خاللها أخالقنـا و أفعالنا لنفوز يوم االمتحان الحقيقي
Out Lines Out Lines
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
3
What is OOP? why?Main Concepts of OOPThinking in Objects.Class & ObjectAccess modifiers How To build ClassesHow To create Objects
What is OOP?What is OOP?
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
4
Object oriented programming, or OOP, is one model of programming language.
Unlike other examples of computer programming language, object oriented
programming focuses on the use of objects instead of actions in order to carry out tasks.
whywhy??
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
5
• Re-use: he power of object-oriented systems lies in their promise of code reuse which will increase productivity, reduce costs and improve software quality.
• Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
Main Concepts of OOPMain Concepts of OOP
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
6
•A computer language can be aid to be Object Oriented if it provides support for the following, which are presented by classes & objects:
Abstracti
onPolymorphism
Polymorphism
Encapsulation
Inheritance
Inheritance
Main Concepts of OOPMain Concepts of OOP
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
7
•Encapsulation: The act of placing data and the operations that perform on that data in the same class. The class then becomes the 'capsule' or container for the data and operations.
Main Concepts of OOPMain Concepts of OOP
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
8
•Inheritance: The reuse of base classes (superclasses) to form derived classes (subclasses). Methods and properties defined in the superclass are automatically shared by any subclass.
Main Concepts of OOPMain Concepts of OOP
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
9
•Polymorphism: Same interface, different implementation. The ability to substitute one class for another.
Main Concepts of OOPMain Concepts of OOP
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
10
•Different classes may contain the same method names, but the result which is returned by each method will be different as the code behind each method (the implementation) is different in each class.
Main Concepts of OOPMain Concepts of OOP
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
11
•Abstraction: using the methods of classes without care how they are implemented.
؟؟
Thinking in ObjectsThinking in Objects
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
12
• Every thing in the world is an object of class which contains
attributes
behavior
Class & ObjectClass & Object
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
13
•A class is a blueprint, or prototype, that defines the variables (data) and the methods (operations) common to all objects of a certain kind.
• An instance of a class. A class must be instantiated into an object before it can be used in the software. More than one instance of the same class can be in existence at any one time.
Class & ObjectClass & Object
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
14
Class & ObjectClass & Object
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
15
How To build Classes?
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
16
Any Thing (Class)
Attributes
Behavior
Each one is a represented as a variable
in class
Each one is a represented
as a Method in class
How To build Classes?
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
17
Bank Account
ID, owner,
amount, …
Withdraw, deposit, transfer
Each one is a represented as a variable
in class
Each one is a represented
as a Method in class
How To build Classes?
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
18
Access_modifiers class class_name {
Access_modifiers class_name() {
}} Constructor method
Which gives the initial values to the variables
Reserved word Name of class, must matchThe identifier rules As public
Access Modifiers
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
19
private: private members are accessible only in the class itself
package: package members are accessible in classes in the same package and the class
itself protected: protected members are
accessible in classes in the same package, in subclasses of the class, and in the class itself
public: public members are accessible anywhere the class is accessible
Example: Student Class
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
20
Lname string
Student_IDstring
Levelint
Telstring
Gender string
Programstring
Print_infoInput: -return: void
Update_infoInput: name or IDReturn : void
Fname string
Example: Student Class
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
21
Instance variable
Constructor
Constructor
Example: Student Class
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
22
Method
OverloadedMethod
How To create Objects?
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
23
Class_name object_name = new Class_name ();
Class_name object_name = new Class_name (value1, value2);
Example: Student Class (cont’)
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
24
Next LectureNext Lecture… …
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
25
How to store Objects
in Array & why?