Transcript Document

Lecturer : Maha Sabri Altememe
Lecture :1
1
Introduction to Object-Oriented
Programming

 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
What Is Object-Oriented
Programming?

 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
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
4
methods)
Objects

•Is a bank account an object?
•Is an account no. an object?
•Is a checking account an object?
5
object
Example of a class
public class Student {
String name;
int age;
void getname(){ }
void setage(){ }
}

State of object
behaviors of object
6
Fundamental Principles of
OOP

 To support the principles of object-oriented programming,
all OOP languages have three features in common:
 Encapsulation
 Polymorphism
 Inheritance.
7
Encapsulation

 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 – Example
Person

-name : string
-age : int
+Person(string name, int age)
+Name : string { get; set; }
+Age : TimeSpan { get; set; }
9
Polymorphism

 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
Count..

 For example: UndergraduateStudent and GraduateStudent
are subclasses of Student
11
Inheritance

 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
Count..
 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’ relation
Example: if a Circle inherits the Shape class, hence the
Circle is a Shape. (See the next figure)
13
Count..

14
Count..

 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
Single
Multiple
16
Q?
17