introduction to object oriented concepts

34
Introduction to Object Oriented Concepts WEEK1 Asst. Prof. Dr. Senem Kumova Metin

Upload: cybele

Post on 23-Feb-2016

76 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Object Oriented Concepts. WEEK1 Asst . Prof. Dr. Senem Kumova Metin. Classification of High level Programming languages. Programming paradigm : Alternative approaches to the programming process Imperative ( Procedural ) ( Fortran , Algol , Pascal , Basic , C) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Object Oriented Concepts

Introduction to Object Oriented Concepts

WEEK1Asst. Prof. Dr. Senem Kumova Metin

Page 2: Introduction to Object Oriented Concepts

Classification of High level Programming languages

• Programming paradigm : Alternative approaches to the programming process

– Imperative (Procedural) (Fortran, Algol, Pascal, Basic, C)

– Functional (Lisp, ML)– Declarative(Logic) (Prolog,GPSS)– Object-Oriented (C++, Java, Scala, Smalltalk)

Page 3: Introduction to Object Oriented Concepts

History of Programming Languages

Page 4: Introduction to Object Oriented Concepts

Programming Languages 1/2

• Imperative: The language provides statements, such as assignment statements , which explicitly change the state of the memory of the computer. X:=X+1

• Functional: In this paradigm we express computations as the evaluation of mathematical functions.

(defun factorial (n) (if (<= n 1) 1 (* n (factorial (- n 1)))))

The program can then be called as(factorial 10)

Page 5: Introduction to Object Oriented Concepts

Programming Languages 2/2• Logic (Declarative): In this paradigm we express computation

in exclusively in terms of mathematical logic

brother(X,Y) /* X is the brother of Y */ :- /* if there are two people F and M for which*/ father(F,X), /* F is the father of X */ father(F,Y), /* and F is the father of Y */ mother(M,X), /* and M is the mother of X */ mother(M,Y), /* and M is the mother of Y */ male(X). /* and X is male */

uncle (X,Y):- father(F,Y), brother(X,F).

• Object-Oriented: In this paradigm we associate behavior with data-structures called " objects " which belong to classes which are usually structured into a hierarchy

Page 6: Introduction to Object Oriented Concepts

What exactly is an object?

• An OBJECT is an identity which includes both Attributes and Behaviors

EXAMPLE -- > PERSON OBJECTAttributes : Eye Color, Age, Height …Behaviors : Walking, Talking, Breathing …

Page 7: Introduction to Object Oriented Concepts

The Object Model

• A OO-software system is a set of cooperating objects

• Objects have state and processing ability • Objects exchange messages

Page 8: Introduction to Object Oriented Concepts

Class and Object• A class is a collection of

objects that have common properties, operations and behaviors.

• A class is a data type, objects are instances of that data type.

Page 9: Introduction to Object Oriented Concepts

Object Oriented Programming• A programming paradigm that uses abstraction (in the form of classes

and objects) to create models based on the real world environment. • An object-oriented application uses a collection of objects, which

communicate by passing messages to request services.

• Objects are capable of passing messages, receiving messages, and processing data.

• The aim of object-oriented programming is to try to increase the flexibility and maintainability of programs. Because programs created using an OO language are modular, they can be easier to develop, and simpler to understand after development.

Page 10: Introduction to Object Oriented Concepts

Procedural Versus OO Programming

• Programs are made up of modules, which are parts of a program that can be coded and tested separately, and then assembled to form a complete program.

– in Procedural Languages, modules are procedures– in OO languages, main modules are classes rather

than procedures !!

Page 11: Introduction to Object Oriented Concepts

Procedural Versus OO Programming• In OO design, attributes and behaviors are within a single

object whereas in procedural they are separated!!

• Procedural programming -> Top Down Design. – Start with a problem (procedure) – Systematically break the problem down

into sub problems (sub procedures). • The difficulties with procedural programming, is that

software maintenance can be difficult and time consuming.

Functional Decomposition

Page 12: Introduction to Object Oriented Concepts

Procedural Versus OO Programming

• In procedural Languages data is separated from procedures The problem of data hiding !!

• Global Data non proper design

Page 13: Introduction to Object Oriented Concepts

Procedural Versus OO Programming: EXAMPLE

• Procedural programming :– Send only data over wire A handshaking aggrement must

be in place between destination & source

• OO programming :– Send the object in which data and operations that

manipulate data are encapsulated– Example : Web object Browser

Page 14: Introduction to Object Oriented Concepts

What exactly is an Object?

• Attributes = Data• The attributes contain the

information that differentiates between the various objects !!

Page 15: Introduction to Object Oriented Concepts

What exactly is an Object?• Behavior = Method • The behavior is what the object can do !!• You invoke a method by sending a message to it.

• Each attribute must have set and get methods Because other objects should not

manipulate data within another object due to data hiding

• Setter = Mutator : Changes the value of attribute• Getter = Accessor : Returns the current value of the

attribute

Page 16: Introduction to Object Oriented Concepts

Object Behaviors: Example

Page 17: Introduction to Object Oriented Concepts

Employee and payroll UML class diagrams

Page 18: Introduction to Object Oriented Concepts

What exactly is a Class?

• A class is a blueprint for an object.

• Objects can not be instantiated without a class –> When you instantiate an object you use the class as the basis for how the object is built

Page 19: Introduction to Object Oriented Concepts

Example: A Definition of a Person class

Page 20: Introduction to Object Oriented Concepts

UML class diagram of Person class

Page 21: Introduction to Object Oriented Concepts

Encapsulation

• The ability of an object to not reveal all the attributes and behaviors !!

– All of the object's data is contained and hidden in the object and access to it restricted to members of that class.

• One of the fundamental principles of OOP !!!

Page 22: Introduction to Object Oriented Concepts

Encapsulation• Data abstraction allow programmers to hide data

representation details behind a (comparatively) simple set of operations (an interface)

• What are the benefits??– Reduces conceptual load

• Programmers need to knows less about the rest of the program– Provides fault containment

• Bugs are located in independent components– Provides a significant degree of independence of program

components• Separate the roles of different programmer

Page 23: Introduction to Object Oriented Concepts

Data Hiding

• Data Hiding is a major part of encapsulation.• In good OO design, an object shold only reveal

the interfaces that other objects must have to interact with it !!!

• Interface : The collection of public behaviors that provide the communication with the object

• Private data supports data hiding !!

Page 24: Introduction to Object Oriented Concepts

A real world example of Interface and Implementation

• Requesting Object : Toaster (requires electricity)• Interface : Electrical outlet • Implementation : Coal powered plant or nuclear power plant or a local

generator

Page 25: Introduction to Object Oriented Concepts

A model of the Interface/Implementation Paradigm

Page 26: Introduction to Object Oriented Concepts

Inheritance• Object oriented programming languages allow classes to

inherit commonly used state and behavior from other classes CODE REUSE

• How to factor out the commonalities of various classes?? Find out IS-A RELATIONSHIPS

– A mammal is a vertebrate– A dog is a mammal

• Superclass (parent) contains all the attributes and behaviors that are common to classes that inherit from it (subclasses)

Page 27: Introduction to Object Oriented Concepts

Inheritance : Classification of Vertebrates

• Arrows represent “is-a” relationship

Page 28: Introduction to Object Oriented Concepts

Inheritance: A Simple Example

Page 29: Introduction to Object Oriented Concepts

Abstract Classes

Page 30: Introduction to Object Oriented Concepts

Polymorphism

• Dictionary Definition of Polymorphism:The quality of being able to assume different forms

• In the context of programming: A program part is polymorphic if it can be used for objects of several types

Page 31: Introduction to Object Oriented Concepts

Polymorphism

• Polymorphism is tightly coupled to inheritance

• In inheritance hierarchy, all subclasses inherit the interfaces from their superclass.

• However each subclass is a separate entitiy , each might require a different response to same message !! Polymorphism supports different responses

Page 32: Introduction to Object Oriented Concepts

Polymorphism : Examplepublic abstract class Shape{

private double area;public abstract double getArea();}

public class Circle extends Shape{double radius;

public Circle(double r){ radius=r;}

public double getArea(){ area=3.14*radius*radius;

return (area) ;}

}

Page 33: Introduction to Object Oriented Concepts

Composition

• Objects are often built or composed from other objects COMPOSITION

• Find out HAS-A relationships !!

• A computer contains video cards, drives, keyboards A computer has (a) video card(s) A computer has (a) drive(s)

Page 34: Introduction to Object Oriented Concepts

Conclusion• Encapsulation

– A single object contains both its data and behaviors and can hide what it wants from other objects.

• Inheritance – A class can inherit from another class and take advantage of

the attributes and methods defined by the super class• Polymorphism:

– Similar objects may respond to the same emssage in different ways

• Composition: – An object may be built from other objects