Introduction to Object-oriented programming and software
development
Lecture 1
2
Procedural Programming
Traditional programming languages were procedural. C, Pascal, BASIC, Ada and COBOL
Programming in procedural languages involves choosing data structures (appropriate ways to store data), designing algorithms, and translating algorithm into code.
3
Procedural Programming
In procedural programming, data and operations on the data are separated.
This methodology requires sending data to procedure/functions.
4
Procedural Programming
Function A
Data Element
Function B
5
#include <iostream>
void read_data();double addition (double,double);
void main () { double num1, num2, double; read_data(); cout << "Sum:"<<addition(num1,num2);}
//function definitionvoid read_data ( ) { cout << "enter 1st number" << endl; cin >> num1; cout << "enter 2nd numbber" << endl; cin >> num2;}double addition(double n1, double n2){ return(n1+n2);}
6
Object-Oriented Programming
Object-oriented programming is centered on creating objects rather than procedures/ functions.
Objects are a melding of data and procedures that manipulate that data.
Data in an object are known as attributes. Procedures/functions in an object are known
as methods.
7
Object-Oriented Programming
Object
Attributes (data)
Methods(behaviors / procedures)
8
public class Add {private double num1,num2;public Add() {
num1 = 1.3;num2 = 3.4;sum = 0.0;
}public double getNum1() {
return num1; }
public double addnumber(double n1,double n2) {return (n1+n2);
}}public class TestAdd {
public static void main(String[] args){
Add a = new Add();System.out.println("Sum:"+a.addnumber(getNum1(),num2));
}}
9
Object-Oriented Programming
Object-oriented programming combines data and behavior via encapsulation.
Data hiding is the ability of an object to hide data from other objects in the program.
Only an objects methods should be able to directly manipulate its attributes.
Other objects are allowed to manipulate an object’s attributes via the object’s methods.
This indirect access is known as a programming interface.
10
Object-Oriented Programming
ObjectAttributes (data)
typically private to this object
Methods(behaviors / procedures)
Otherobjects
ProgrammingInterface
11
Object-Oriented Programming Languages
Pure OO Languages
Smalltalk, Eiffel, Actor, Java
Hybrid OO Languages
C++, Objective-C, Object-Pascal
12
VOCABULARY OF OOP
CLASS OBJECT ENCAPSULATION implement DATA HIDING INHERITANCE POLYMORPHISM
13
CLASS
The most important term. A class is the template or blueprint from which objects
are actually made. All code that you write in Java is inside a class. The standard Java library supplies several thousand
classes. You can create your own classes in Java to describe
the objects of the problem domains of your applications and to adapt the classes that are supplied by the standard library to your own purposes.
A class encapsulates the attributes and actions that characterizes a certain type of object.
14
OBJECTS
Classes can be used to instantiate as many objects as are needed.
Each object that is created from a class is called an instance of the class.
A program is simply a collection of objects that interact with each other to accomplish a goal.
15
Classes and Objects
Car class
The Car class defines the attributesand methods that will exist in all objects
that are an instances of the Car class.
Kancil object
The Kancil object is aninstance of the Car class.
Nazaria object
The Nazaria object is aninstance of the Car class.
16
ENCAPSULATION key concept in working with objects
. Combining data and behavior
in one package and hiding the implementation of the data from the user of the object.
The data in an object are called its instance fields, and the procedures that operate on the data are called its methods.
The key to making encapsulation work is to have methods never directly access instance fields in a class other than their own.
Programs should interact with object data only through the object's methods.
By encapsulating object data, you maximize reusability, reduce data dependency, and minimize debugging time.
17
DATA HIDING implemented through ENCAPSULATION
Data Hiding is important for several reasons. It protects of attributes from accidental
corruption by outside objects. It hides the details of how an object works, so
the programmer can concentrate on using it. It allows the maintainer of the object to have
the ability to modify the internal functioning of the object without “breaking” someone else's code.
18
Allow CODE REUSABILITY Object-Oriented Programming (OOP) has encouraged
component reusability. A component is a software object contains data and
methods that represents a specific concept or service. Components typically are not stand-alone programs. Components can be used by programs that need the
component’s service. Reuse of code promotes the rapid development of larger
software projects.
19
INHERITANCE
Inheritance is the ability of one class to extend the capabilities of another. it allows code defined in one class to be
reused in other classes Consider the class Car. A Car is a specialized form
of the Vehicle class. So, is said that the Vehicle class is the base or parent
class of the Car class. The Car class is the derived or child class of the
Vehicle class.
20
INHERITANCE
VehicleVehicle is theparent class.
TruckCar
is-a relationship
Car and Truck arechild classes of
Vehicle.
Car and Truck areSpecialized versions of
a Vehicle.
Vehicle represents allof the generic attributes
and methods of a vehicle.
21
POLYMORPHISM
Polymorphism means "many forms." A reference variable is always of a single,
unchangeable type, but it can refer to a subtype object.
A single object can be referred to by reference variables of many different types—as long as they are the same type or a supertype of the object.q The reference variable's type (not the object's type), determines which methods can be called!
Polymorphism allows extensibility.
22
Object Orientation Principle
Divide-and-conquer Encapsulation and Modularity Public Interface Information Hiding Generality Extensibility Abstraction
23
Divide-and-Conquer Principle
The first step in designing a program is to divide the overall program into a number of objects that will interact with each other to solve the problem.
Problem solving: Break problems (programs) into small, manageable tasks.
24
Encapsulation Principles
The next step in designing a program is to decide for each object, what attribute it has and what actions it will take.
The goal is that each object is a self-contained module with a clear responsibility and the attributes and actions necessary to carry out its role
Problem solving: Each object knows how to solve its task and has the information it needs.
25
Interface Principles
For object to work cooperatively and efficiently, we have to clarify exactly how they are to interact, or interface, with one another.
Each object should present a clear public interface that determines how other objects will be used.
ObjectAttributes (data)
typically private to this object
Methods(behaviors / procedures)
Otherobjects
ProgrammingInterface
26
Information Hiding Principles
To enable objects to work together cooperatively, certain details of their individual design and performance should be hidden from other objects.
Each object should shield its users from unnecessary details of how it performs its role.
27
Generality Principles
To make an object as generally useful as possible, we design them not for a particular task but rather for a particular kind of task. This principle underlies the use of software libraries.
Objects should be designed to be as general as possible.
Objects are designed to solve a kind of task rather than a singular task.
28
Extensibility Principles
One of the strength of the object-oriented approach is the ability to extend an object’s behavior to handle new tasks.
An object should be designed so that their functionality can be extended to carry out more specialized tasks.
Shape
Circle Rectangle Triangle
Cone Cylinder
29
Abstraction Principles
is the ability to focus on the important features of an object when trying to work with large amounts of information.
ignore many of the attributes that characterize the real objects and focus only on those attributes that are essential for solving a particular problem.
30
Example ofabstraction
31
Benefits of Object-oriented programming Save development time (and cost) by reusing code
once an object class is created it can be used in other applications
Easier debugging classes can be tested independently reused objects have already been tested
32
Self-test: Introduction to Object Oriented Programming
1. What are the seven basic principles of object orientation? Provide a brief description of each.
2. State the benefits of object oriented programming.