25
Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE

Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

Embed Size (px)

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 Objects

Citation preview

Page 1: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 2: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

و من يتق الله ...

قال الله تعالى:

)وإبراهيم إذ قال لقومه اعبدوا الله واتقوه ذلكم خير

لكم إن كنتم تعلمون( (16)العنكبوت:

Mahmoud Rafeek Alfarra

2

Downloaded from http://staff.cst.ps/mfarraDownloaded from http://staff.cst.ps/mfarra

شريحـة ثابتـة لعلنا نحسن من خاللها أخالقنـا و أفعالنا لنفوز يوم االمتحان الحقيقي

Page 3: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 4: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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.

Page 5: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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.

Page 6: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 7: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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.

Page 8: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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.

Page 9: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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.

Page 10: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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.

Page 11: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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.

؟؟

Page 12: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 13: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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.

Page 14: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

Class & ObjectClass & Object

Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra

14

Page 15: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

Class & ObjectClass & Object

Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra

15

Page 16: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 17: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 18: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 19: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 20: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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

Page 21: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

Example: Student Class

Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra

21

Instance variable

Constructor

Constructor

Page 22: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

Example: Student Class

Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra

22

Method

OverloadedMethod

Page 23: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

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);

Page 24: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

Example: Student Class (cont’)

Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra

24

Page 25: Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION

Next LectureNext Lecture… …

Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra

25

How to store Objects

in Array & why?