Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 61

Introduction to Object Oriented

Programming (OOP)
Lecture 1
The Institute of Finance Management
Faculty of Computing, Information Systems and
Mathematics

Computer Science Department

Dr. Mandari, H
Computer Programming
• An algorithm is a step-by-step process.
• A computer program is a step-by-step set of
instructions for a computer.
• Every computer program is an algorithm.
• The history of computer programming is a
steady move away from machine-oriented views
of programming towards concepts and
metaphors that more closely reflect the way in
which we ourselves see & understand the world
Programming Languages
• Programming languages allow programmers
to develop software.
• The three major families of languages are:
1. Machine languages
2. Assembly languages
3. High-Level languages
Machine Languages
• Comprised of 1s and 0s
• The “native” language of a computer
• Difficult to program – one misplaced 1 or 0
will cause the program to fail.
• Example of code:
1110100010101 111010101110 10111010110100
10100011110111
Assembly Languages
• Assembly languages are a step towards easier
programming.
• Assembly languages are comprised of a set of
elemental commands which are tied to a specific
processor.
• Assembly language code needs to be translated to
machine language before the computer processes it.
• Example:
ADD 1001010, 1011010
High-Level Languages
• High-level languages represent a giant leap towards
easier programming.
• The syntax of HL languages is similar to English.
• Example:
grossPay = basePay + overTimePay
• Interpreter – Executes high level language programs
without compilation.
• Historically, we divide HL languages into two groups:
1. Procedural languages
2. Object-Oriented languages (OOP)
Procedural Languages
• Early high-level languages are typically called
procedural languages.
• Procedural languages are characterized by
sequential sets of linear commands.
• The focus of such languages is on structure.
• Examples include C, COBOL, Fortran, LISP, Perl,
HTML, VBScript
Object-Oriented Languages
• The focus of OOP languages is not on
structure, but on modeling data.
• Programmers code using “blueprints” of data
models called classes.
• Examples of OOP languages include C++,
Visual Basic.NET and Java.
Object-Oriented Programming Languages

• OOP is mainly a program design philosophy.


• OOP uses a different set of programming
languages than old procedural programming
languages (C, Pascal, etc.).
• Everything in OOP is grouped as self
sustainable "objects". Hence, you gain
reusability by means of four main object-
oriented programming concepts
Object-Oriented Programming Languages

• In OOP programmers define not only the data type


of a data structure, but also the types of
operations/methods (functions) that can be applied
to the data structure.
• In this way, the data structure becomes an object
that includes both data and functions (methods) in
one unit. In addition, programmers can create
relationships between one object and another.
• For example, objects can inherit characteristics from
other objects.
Object-Oriented Programming Languages

• Pure OO Languages
– Eiffel, Actor, Emerald, JADE, Obix, Ruby, Python,
Scala, Smalltalk, Self.
• Hybrid OO Languages
– Delphi/Object Pascal, C++, Java, C#, VB.NET,
Pascal, Visual Basic, MATLAB, Fortran, Perl, COBOL
2002, PHP, ABAP, Ada 95.
OOP Basic Terminology
• Object
– usually a person, place or thing (a noun)
• Method
– an action performed by an object (a verb)
• Property or attribute
– Characteristics of certain object.
• Class
– a category of similar objects (such as automobiles),
does not hold any values of the object’ s
attributes/properties
Classes and Objects
• A class is a prototype, idea, and blueprint for
creating objects.
• An object is an instance of a class.
• For example, in Java we define classes, which
in turn are used to create objects
• A class has a constructor for creating objects
• Class is composed of three things: its name,
attributes/properties, and methods.
Classes & Objects
• A class is a
definition of
objects with the
same properties
and the same
methods.
Classes Example
Object Relationship
HAS-A Relationship
• Association or the “HAS-A”
Teacher Student
Relationship
– One object knows about another object Laptop Mouse
– One object has a reference to another Driver Car
object
Host Parasite
• Inheritance or the “IS-A”
Relationship
– Allows an object to extends to another
object
– Gives ideas to the object TEACHER PERSON STUDENT

– Knows something about the object


IS A IS A
Almost everything in the world can be
represented as an object
• A flower, a tree, an animal
• A student, a professor
• A desk, a chair, a classroom, a building
• A university, a city, a country
• The world, the universe
• A subject such as CS, IS, Math, History, …
• An information system, financial, legal, etc..
What Is an Object, again?
• An object is an instance of a class
• The Instance is the actual object created at
runtime
• Instance is a specific realization of any object
• An object may be varied in a number of ways.
• Each realized variation of that object is an
instance.
Classes & Objects
In short…
• An Object is a Class when it comes alive!
– Homo Sapien is a class, John and Jack are objects
– Animal is a class “Snowball” the cat is an object
– Vehicle is a class My neighbor's BMW is an object
– Mobile Phone is a class, Techno, Sumsung and
Oppo are objects
Technical contrast between Objects &
Classes
Objects Need to Collaborate!
• Objects are useless unless they can
collaborate together to solve a problem.
– Each object is responsible for its own behavior and
status.
– No one object can carry out every responsibility
on its own.
• How do objects interact with each other?
– They interact through messages.
Object Interaction
Example of Object Interaction
OOP Basic Concepts
• Encapsulation
• Inheritance
• Abstraction
• Polymorphism
Encapsulation
Encapsulation
• Is the inclusion of property & method within a
class/object in which it needs to function
properly.
• Also, enables reusability of an instant of an
already implemented class within a new class
while hiding & protecting the method and
properties from the client classes.
Encapsulation
• The class is kind of a container or capsule or a cell,
which encapsulate the set of methods, attributes
and properties to provide its indented
functionalities to other classes.
• In that sense, encapsulation also allows a class to
change its internal implementation without hurting
the overall functioning of the system.
• That idea of encapsulation is to hide how a class
does its operations while allowing requesting its
operations.
Encapsulation – Benefits
• Ensures that structural changes remain local:
– Changing the class internals does not affect any code
outside of the class
– Changing methods' implementation does not reflect the
clients using them
• Encapsulation allows adding some logic when
accessing client's data
– E.g. validation on modifying a property value
• Hiding implementation details reduces complexity
– easier maintenance
Inheritance
• Inheritance—a way of organizing classes
• Term comes from inheritance of traits like eye color, hair
color, and so on.
• Classes with properties in common can be grouped so that
their common properties are only defined once in parent
class.
• Superclass – inherit its attributes & methods to the
subclass(es).
• Subclass – can inherit all its superclass attributes &
methods besides having its own unique attributes &
methods.
Inheritance
• Inheritance allows child classes to inherit the
characteristics of existing parent class
– Attributes (fields and properties)
– Operations (methods)
• Child class can extend the parent class
– Add new fields and methods
– Redefine methods (modify existing behavior)
• A class can implement an interface by
providing implementation for all its methods
Inheritance
• Expresses commonality among classes/objects
• Allows code reusability
• Highlights relationships
• Helps in code organization
Inheritance
Inheritance – Example
An Inheritance Hierarchy
Example: Single Inheritance
Example: Multiple Inheritance
Example: Multiple Inheritance
Lion Eagle

eatMeat() squawk()
Roar() fly()

Griffin
Abstraction
• Abstraction allows programmers to represent complex real
world in the simplest manner.
• In abstraction we represents the major attributes only
• It is a process of identifying the relevant qualities and
behaviors an object should possess, in other word
represent the necessary features without representing the
back ground details
• You should always use abstraction to ease reusability, and
understanding for the design and enable extension.
• When we design the abstract classes, we define the
framework for later extensions.
Abstraction
• An abstract class, which declared with the
“abstract” keyword, cannot be instantiated.
• It can only be used as a super-class for other
classes that extend the abstract class.
• Abstract class is a design concept and
implementation gets completed when it is
being realized by a subclass.
Abstraction - type of classes
Abstraction
• An abstract class is a class that may not have any
direct instances.
• An abstract operation is an operation that it is
incomplete and requires a child to supply an
implementation of the operation.
Polymorphism
• Polymorphism refers to a programming
language's ability to process objects differently
depending on their data type or class.
• Generally, the ability to appear in many forms.
• More specifically, it is the ability to redefine
methods for derived classes.
Polymorphism
• For example, given a base class shape,
polymorphism enables the programmer to
define different area methods for any number
of derived classes, such as circles, rectangles
and triangles.
• No matter what shape an object is, applying
the area method to it will return the correct
results.
Polymorphism
• Polymorphisms is a generic term that means 'many
shapes'. More precisely Polymorphisms means the
ability to request that the same methods be
performed by a wide range of different types of
things.
• In OOP, polymorphisms is a technical issue and
principle.
• It is achieved by using many different techniques
named method overloading, operator overloading,
and method overriding.
Polymorphism
• In Java, two or more classes could each have a
method called output
• Each output method would do the right thing
for the class that it was in.
• One output might display a number
(output.number) in one class, whereas it
might display a name (output.text) in another
class.
Polymorphism
• It is the ability to look at a class in its parent
image.
• Lets see the robot example throughout the
following few slides
Unified Modelling Language
• UML is a diagramming tool for describing and documenting
object oriented applications
• Programming language independent
• Used for modelling an application before its engineered
• Twelve different diagrams in all, with many complex details
• Generally though only two of these are used regularly
– Class diagrams
– Sequence diagrams
Unified Modelling Language
• Class Diagrams
– Describe classes and interfaces
– …their properties
– …their public interface
– …and their relationships (e.g. inheritance, aggregation,
composition)
• Sequence Diagrams
– Describe how objects send messages to one another
– Useful for describing how a particular part of an application works
• We’ll be covering just class diagrams
– Very useful for describing APIs and discussing OO applications
UML Classes
• Box with 3 sections
• The top contains the class
name
• The middle lists the classes
attributes
• The bottom lists the classes
methods
• Can indicate parameters and
return types to methods, as
well as their visibility
UML Association
• A line between two classes
indicates a relationship
• Extra information can be added to
describe the relationship
• Including
– Its name
– The roles that the classes play
– The cardinality of the relationship
(how many objects are involved)
• E.g. a Person works For a
Company, which has many
employees
UML comments
• Useful for adding
text for the readers
of your diagram
• The symbol looks
like a little note,
with a dotted line
joining it to the
class or relationship
that its describing
UML Aggregation
• Aggregation (a whole-part
relationship) is shown by
a line with clear diamond.
• As aggregation is a form
of relationship you can
also add the usual extra
information
• i.e.
– Name
– Roles
– Cardinality
UML Inheritance
• Inheritance is shown by a
solid arrow from the sub-
class to the super-class
• The sub-class doesn’t list
its super-class attributes
or methods,
• Unless its providing its
own alternate version
(i.e. is extending the
behaviour of the base
class)
Concluding Remarks
• Advantages of OOP
– Code reuse & recycling
– Improved software-development productivity
– Improved software maintainability
– Faster development
– Lower cost of development
– Higher-quality software
– Encapsulation
Disadvantages of OOP
• Steep learning curve
• Could lead to larger program sizes
• Could produce slower programs
OOP Suitability
• Object oriented programming is good in
complex projects or modular type of systems.
It allows simultaneous system development
teams and also could aid in agile system
development environments like Extreme
Programming.
END

You might also like