: Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

17
Lecturer : Maha Sabri : Maha Sabri Altememe Altememe Lecture :1 Lecture :1 1

Transcript of : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

Page 1: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

Lecturer : Maha Sabri Altememe : Maha Sabri Altememe

Lecture :1 Lecture :1

1

Page 2: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

The 1960s gave birth to structured programming.

This is the method encouraged by languages such as C and Pascal. The use of structured languages made it possible to write middling complex programs fairly easily. Structured languages are characterized by their support for stand-alone subroutines, local variables, rich control constructs, and their lack of reliance upon the GOTO. Although structured languages are a powerful tool, even they reach their limit when a project becomes too large.

2

Introduction to Object-Oriented Programming

Page 3: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

Object oriented programming involves

programming using objects. An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner. For example, a student, circle.

3

What Is Object-Oriented

Programming?

Page 4: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

4

Concept: Object have state and behaviors

The state of an object is represented by data fields with their current values .

The behavior of an object is defined by a set of methods.

A class can be defined as a template that describes the behaviors that object of its type support.

Every object belongs to (is an instance of) a class An object may have fields, or variables(The class

describes those fields) An object may have methods(The class describes those

methods)

Page 5: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

5

Objects

•Is a bank account an object? •Is an account no. an object?•Is a checking account an object?

Page 6: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

6

Example of a class

public class Student { String name; int age;

void getname(){ } void setage(){ } }

State of object

behaviors of object

object

Page 7: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

To support the principles of object-oriented

programming, all OOP languages have three features in common:

Encapsulation Polymorphism Inheritance.

7

Fundamental Principles of OOP

Page 8: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

Encapsulation is used to hide the values or state of a

structured data object inside a class. That is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. In an object-oriented language, code and data may be combined in such a way that a self-contained. When code and data are linked together in this fashion, an object is created. In other words, an object is the device that supports encapsulation. Within an object, code, data, or both may be private to that object or public.

8

Encapsulation

Page 9: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

9

Encapsulation – Example

PersonPerson

-name : string-name : string-age : int-age : int

+Person(string name, int +Person(string name, int

age)age)

+Name : string { get; +Name : string { get;

set; }set; }

+Age : TimeSpan { get; +Age : TimeSpan { get;

set; }set; }

Page 10: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

Object-oriented programming languages support polymorphism, which is characterized by the phrase "one interface, multiple methods." In simple terms, polymorphism is the attribute that allows one interface to control access to a general class of actions.

10

Polymorphism

Page 11: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

For example: UndergraduateStudent and

GraduateStudent are subclasses of Student

11

Count..

Page 12: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

Inheritance is the process by which one object

can get the properties of another object. This is important because it supports the concept of classification. If you think about it, most knowledge is made manageable by hierarchical classifications.

For example, a Red apple is part of the classification apple, which in turn is part of the fruit class, which is under the larger class food.

12

Inheritance

Page 13: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

Inheritance let us to create a new class from the existing classes.

The new class is called subclass and the existing class is called superclass.

The subclass inherits the properties of the superclass.

The properties refer to the method or the attribute (data)

Inheritance is ‘is-a’ relationExample: if a Circle inherits the Shape class, hence

the Circle is a Shape. (See the next figure)13

Count..

Page 14: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

14

Count..

Page 15: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

Single inheritance

Is a subclass that derived from a single/one superclass (existing class)

Multiple inheritance Is a subclass that derived from more than

one superclass Not supported by Java.

15

Count..

Page 16: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

16

Single

Multiple

Page 17: : Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.

17

Q?Q?