University of Prince Mugrin
College of Computer and Cyber Sciences
Department of Computer Science
Program of Software Engineering
CS112 - Object Oriented Programming
Lecture 1
INTRO TO OOP
Prof. Mohamed Zayed 1
OUTLINE
Introduction to Object-Oriented Programming (OOP)
Brief history of OOP
Procedural vs. OOP
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
2
3
4
INTRODUCTION TO OOP
Object-Oriented Programming (OOP) is the term used to
describe a programming approach based on objects & classes.
The OO paradigm allows us to organize software as a collection of
objects that consist of both data & behavior.
o This is in contrast to conventional functional programming practice that only
loosely connects data & behavior.
An OO application uses a collection of objects, which communicate by
passing messages to request services.
Objects are capable of passing messages, receiving messages, &
processing data.
5
PASSING MESSAGES BETWEEN OBJECTS
6
INTRODUCTION TO OOP
OOP allows decomposition of a problem into a number of entities called
objects & then builds data & behaviors around these objects.
The software is divided into a number of small units called objects.
o The data & behaviors are built around these objects.
The data of the objects can be accessed only by the behaviors associated with that object.
The behaviors of one object can access the behaviors of another object.
7
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
8
BRIEF HISTORY OF OOP
Simula 67 – the first OOP language; extension of ALGOL60
Smalltalk – conceived by Alan Kay (Smalltalk-72, Smalltalk-80); dynamically typed;
Strongtalk (1993) – Smalltalk + type system
mid 80’s – many languages added support for OO: Objective C, C++, Object Pascal,
Modula 3, Oberon, Objective CAML, CLOS.
Eiffel – Bertrand Meyer (1988) – Pascal-like syntax, design-by-contract
Other “exotic” OO languages: Sather, Trellis/Owl, Emerald, Beta (evolution of Simula), Self
Java – James Gosling (1995); Java 1.5 (2004) – support for generic programming
(Theoretical) extensions to Java: e.g. GJ (1998)
Java 2 (1999) - multiple configurations for different platforms. 9
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
10
PROCEDURAL VS. OO PROGRAMMING
OOP is the successor of procedural (structural) programming.
Procedural programming describes programs as groups of reusable code units
(procedures) which define input & output parameters.
Procedural programs consist of procedures, which invoke each other.
In procedural languages (i.e. C), procedures are a sequence of imperative statements,
such as assignments, tests, loops & invocations of sub procedures.
These procedures are functions, which map arguments to return statements.
11
PROCEDURAL VS. OO PROGRAMMING
The problem with procedural programming is that code reusability is hard &
limited.
The difficulties with this type of programming is that software maintenance can be
difficult & time consuming.
When changes are made to the main procedure (top), those changes can cascade to
the sub procedures of main, & the sub-sub procedures & so on, where the change may
impact all procedures in the pyramid.
The aim of OOP is to try to increase the flexibility & maintainability of programs.
o Because programs created using an OO language are modular, they can be easier to develop,
12
& simpler to understand after development.
PROCEDURAL
VS.
OO
PROGRAMMING
13
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
14
15
OOP FUNDAMENTAL: UNDERSTANDING CLASS & OBJECT
In OO terminology, a class is a template
for defining objects.
Class defines:
o Object data (attributes/properties), &
o Object behaviors (methods/functions).
Multiple objects, or instances of a class
can be created from a single class.
16
OOP FUNDAMENTAL: CLASS
Class is a Template/Blueprint/Plans for defining objects.
Class describes the characteristics of the object.
o They are referred to as data or field – private double price;
Class describes the behaviors of the object.
o Its called methods – public int getUpdate(){return d;};
Data & Methods can be visible only within the scope of the class which declared them &
their descendants (private / protected), or visible to all other classes (public) –
Access Modifier.
17
OOP FUNDAMENTAL: OBJECT
Objects are instances of classes, or the realization of the classes.
Objects are units of abstraction.
Object can communicate with other objects using messages.
o It can pass a message to another object, which results in the invocation of a method.
o Objects then perform the actions that are required to get a response from the system.
Software objects are conceptually similar to real world objects.
An object stores its state in fields/data, & it exposes its behaviors through its
methods.
18
E.g., Car toyota = new Car();
CLASS VS OBJECT
19
CLASS VS OBJECT
20
OOP & JAVA
Class Example Object Example
public class Car { public class CarApp {
private String model; public static void main(String[] args) {
private double cc;
Car toyota = new Car();
public Car() { Car nissan = new Car();
[Link] = model;
[Link] = cc; [Link]();
} [Link]();
}
public void move() { }
[Link](“Vrom-vrom”)
}
}
21
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
22
4 PRINCIPLES OF OOP
For a programming language to be OO, it has to enable working with
classes & objects as well as the implementation & use of the
fundamental OO principles & concepts: Inheritance, Abstraction,
Encapsulation & Polymorphism.
23
24
4 PRINCIPLES OF OOP: INHERITANCE
Inheritance is a way to reuse a written code, & use it
again & again.
The class which is inherited is called the Base class &
the class which inherits is called the Derived class.
o They are also called parent/super & child/sub class.
Derived class inherits a base class.
The derived class can use all the methods which
are defined in base class, hence making code reusable.
25
4 PRINCIPLES OF OOP: ENCAPSULATION
The action of enclosing something in, or as if in, a capsule.
Hide unnecessary details in the classes & provide a clear & simple interface for working with
them.
It describes the idea of bundling data & methods that work on that data within one unit.
o e.g., a class in Java.
This concept is also often used to hide the internal
representation, or state, of an object from the outside.
o This is called Information Hiding.
26
4 PRINCIPLES OF OOP: ABSTRACTION
“An abstraction denotes the essential characteristics of an object that distinguish it from all other
kinds of objects & thus provide crisply defined conceptual boundaries, relative to the perspective
of the viewer” — G. Booch.
Abstraction is the process of hiding all but the relevant information about a thing to
make things less complex and more efficient for the user.
o Shows only essential characteristics.
27
4 PRINCIPLES OF OOP: POLYMORPHISM
Methods with same name but different arguments, performing different actions.
Methods with same name, but functioning in different ways.
Polymorphism means one name, many forms.
There are 2 basic types of polymorphism:
o Overloading
o Overriding
28
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
29
ACCESS MODIFIERS OF CLASS MEMBERS
Java provides a number of access modifiers to set access levels for classes, variables,
methods, & constructors.
The 4 access levels are:-
Visible to the package, the default – No modifiers are needed.
Visible to the class only – private.
Visible to the world – public.
Visible to the package & all subclasses – protected.
30
ACCESS MODIFIERS OF CLASS MEMBERS
31