introduction to object oriented concepts
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 PresentationTRANSCRIPT
Introduction to Object Oriented Concepts
WEEK1Asst. 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)
– Functional (Lisp, ML)– Declarative(Logic) (Prolog,GPSS)– Object-Oriented (C++, Java, Scala, Smalltalk)
History of Programming Languages
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)
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
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 …
The Object Model
• A OO-software system is a set of cooperating objects
• Objects have state and processing ability • Objects exchange messages
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.
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.
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 !!
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
Procedural Versus OO Programming
• In procedural Languages data is separated from procedures The problem of data hiding !!
• Global Data non proper design
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
What exactly is an Object?
• Attributes = Data• The attributes contain the
information that differentiates between the various objects !!
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
Object Behaviors: Example
Employee and payroll UML class diagrams
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
Example: A Definition of a Person class
UML class diagram of Person class
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 !!!
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
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 !!
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
A model of the Interface/Implementation Paradigm
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)
Inheritance : Classification of Vertebrates
• Arrows represent “is-a” relationship
Inheritance: A Simple Example
Abstract Classes
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
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
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) ;}
}
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)
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