introduction to object-oriented programming and software development lecture 1

32
Introduction to Object-oriented programming and software development Lecture 1

Upload: todd-lee

Post on 27-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Object-oriented programming and software development Lecture 1

Introduction to Object-oriented programming and software

development

Lecture 1

Page 2: 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.

Page 3: Introduction to Object-oriented programming and software development Lecture 1

3

Procedural Programming

In procedural programming, data and operations on the data are separated.

This methodology requires sending data to procedure/functions.

Page 4: Introduction to Object-oriented programming and software development Lecture 1

4

Procedural Programming

Function A

Data Element

Function B

Page 5: Introduction to Object-oriented programming and software development Lecture 1

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

Page 6: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 7: Introduction to Object-oriented programming and software development Lecture 1

7

Object-Oriented Programming

Object

Attributes (data)

Methods(behaviors / procedures)

Page 8: Introduction to Object-oriented programming and software development Lecture 1

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

}}

Page 9: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 10: Introduction to Object-oriented programming and software development Lecture 1

10

Object-Oriented Programming

ObjectAttributes (data)

typically private to this object

Methods(behaviors / procedures)

Otherobjects

ProgrammingInterface

Page 11: Introduction to Object-oriented programming and software development Lecture 1

11

Object-Oriented Programming Languages

        Pure OO Languages

Smalltalk, Eiffel, Actor, Java

 

        Hybrid OO Languages

C++, Objective-C, Object-Pascal

Page 12: Introduction to Object-oriented programming and software development Lecture 1

12

VOCABULARY OF OOP

CLASS OBJECT ENCAPSULATION implement DATA HIDING INHERITANCE POLYMORPHISM

Page 13: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 14: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 15: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 16: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 17: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 18: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 19: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 20: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 21: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 22: Introduction to Object-oriented programming and software development Lecture 1

22

Object Orientation Principle

Divide-and-conquer Encapsulation and Modularity Public Interface Information Hiding Generality Extensibility Abstraction

Page 23: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 24: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 25: Introduction to Object-oriented programming and software development Lecture 1

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

Page 26: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 27: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 28: Introduction to Object-oriented programming and software development Lecture 1

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

Page 29: Introduction to Object-oriented programming and software development Lecture 1

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.

Page 30: Introduction to Object-oriented programming and software development Lecture 1

30

Example ofabstraction

Page 31: Introduction to Object-oriented programming and software development Lecture 1

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

Page 32: Introduction to Object-oriented programming and software development Lecture 1

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.