INTRODUCTION TO OOP
CMT210: OOP1
Lecture1: Introduction to OOP
Programming Languages
Programming languages allow programmers to
code/develop/create software.
Generations of programming languages are:
Machine language (1st Generation Languages – 1GL)
Assembly language (2nd Generation Languages – 2GL)
High-Level languages (3rd Generation Languages - 3GL)
4GL (Very High-level Languages)
5GL (Artificial Intelligence Languages)
2
1). Machine Language
Comprised of 1s and 0s
It is 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
3
Advantages of 1GL
They are translation free and can be directly executed by the
computers.
The programs written in these languages are executed very
speedily and efficiently by the CPU of the computer system.
The programs written in these languages utilize the memory in
an efficient manner because it is possible to keep track of each bit
of data.
4
2). Assembly Language
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
5
Assembly Language contd.
A low-level processor-specific
programming language design
to match the processor’s
machine instruction set
each assembly language
instruction matches exactly one
machine language instruction
6
Why learn Assembly Language?
To learn how high-level language code gets translated into machine
language
i.e.: to learn the details hidden in HLL code
To learn the computer’s hardware
by direct access to memory, video controller, sound card, keyboard…
To speed up applications
direct access to hardware (ex: writing directly to I/O ports instead of
doing a system call)
good ASM code is faster and smaller: rewrite in ASM the critical areas
of code
7
Assembly Language Applications
Application programs are rarely written completely in assembly
language
only time-critical parts are written in ASM
Ex1: an interface subroutine (called from HLL programs) is written in ASM for
direct hardware access
Ex2: device drivers (called from the OS)
ASM often used for embedded systems (programs stored in PROM chips)
computer cartridge games, microcontrollers (automobiles, industrial plants...),
telecommunication equipment…
Very fast and compact but processor-specific
8
Assembler
An assembler is a program that converts ASM code into
machine language code:
mov al,5 (Assembly Language)
1011000000000101 (Machine Language)
most significant byte is the opcode for “move into register AL”
the least significant byte is for the operand “5”
Directly programming in machine language offers no
advantage (over Assembly)...
9
Advantages of 2GL
It is easy to develop, understand and modify the program
developed in these languages as compared to those developed
in the first generation programming language.
The programs written in these languages are less prone to
errors and therefore can be maintained with a great case.
10
3). High-Level Languages
High-level languages represent a giant leap towards easier
programming.
The syntax of HL languages is similar to English (English-like).
They enable a programmer to write programs that are independent
of a particular type of computer.
11
High-Level Languages contd.
They are called high-level because they are closer to human languages
and further from machine languages.
In contrast, assembly languages are considered low-level because they
are very close to machine languages.
3GL and later generations are considered as high-level languages
because they enable the programmer to concentrate only on the logic
of the programs without considering the internal architecture of the
computer system.
12
Advantages of 3GL
It is easy to develop, learn and understand the program.
As the program written in these languages are less prone to
errors they are easy to maintain.
The program written in these languages can be developed in
very less time as compared to the first and second generation
language.
13
Examples of 3GL
FORTRAN, ALGOL, COBOL, C++, C, Java, Visual Basic,
JavaScript etc
14
4). 4GL (Very High-level Languages)
The languages of this generation were considered as very
high-level programming languages requiring a lot of time
and effort that affected the productivity of a programmer.
The fourth generation programming languages were
designed and developed to reduce the time, cost and effort
needed to develop different types of software applications.
15
Advantages of 4GL
These programming languages allow the efficient use of data by
implementing various databases.
They require less time, cost and effort to develop different types
of software applications.
The program developed in these languages are highly portable as
compared to the programs developed in the languages of other
generation.
16
Examples of 4GL
Perl, PHP, Python, Ruby, SQL etc
17
5GL (Artificial Intelligence Language)
The programming languages of this generation mainly focus on
constraint programming.
The major fields in which the fifth generation programming
language are employed are Artificial Intelligence and Artificial
Neural Networks
Examples of 5GL: mercury, prolog, OPS5 etc
18
Advantages of 5GL
These languages can be used to query the database in a fast
and efficient manner.
In this generation of languages, the user can communicate
with the computer system in a simple and easy manner.
19
Programming Languages chart
20
Programming Paradigms
A programming paradigm is a style, or “way,” of programming.
Some languages make it easy to write in some paradigms but not
others.
A paradigm is a method to solve some problem or do some task
A programming paradigm is an approach to solve a problem using
some programming language
21
Programming Paradigms
Paradigms differ in the concepts and
abstractions used to represent the elements of a
program (such as objects, functions, variables,
constraints, etc.) and the steps that compose a
computation (assignment, evaluation,
continuations, data flows, etc.).
22
Programming Paradigms contd.
There are lots of programming languages that are known but all
of them need to follow some strategy when they are
implemented. This methodology/strategy is what is called a
Programming paradigm.
Apart from varieties of programming language there are lots of
paradigms to fulfil each and every demand.
23
Programming Paradigm examples
Imperative programming paradigm
Procedural programming paradigm
Declarative programming paradigm
Logical programming paradigm
Functional programming paradigm
Structured programming paradigm
Object-oriented programming paradigm
Etc, etc
24
Some paradigms explained
Imperative: Programming with an explicit sequence of
commands that update state.
Declarative: Programming by specifying the result you want, not
how to get it.
Structured: Programming with clean, goto-free, nested control
structures.
Procedural: Imperative programming with procedure calls.
Object-oriented: a problem is divided into a number of objects
which can interact by passing messages to each other
25
STRUCTURED vs. OO PROGRAMMING
MAIN PROGRAM GLOBAL DATA
FUNCTION FUNCTION 2 FUNCTION 3
1
FUNCTION 4 FUNCTION 5
26
Structured Programming
Using function
Function & program is divided into
modules
Every module has its own data and
function which can be called by other
modules.
27
OBJECT ORIENTED PROGRAMMING
Object 2
Object 1
Data Data
Function Function
Object 3
Data
Function
28
OBJECT ORIENTED PROGRAMMING
Objects have both data and methods
Objects of the same class have the same data elements
and methods
Objects send and receive messages to invoke actions
Key idea in object-oriented:
The real world can be accurately described as a
collection of objects that interact.
29
OOP TERMINOLOGIES
object
- usually a person, place or thing (a noun)
method
- an action performed by an object (a verb)
attribute
- description of objects in a class
class
- a category of similar objects (such as automobiles)
- does not hold any values of the object’s attributes
30
Examples of attributes and methods
Attributes: Methods:
manufacturer’s name Define data items
model name (specify manufacturer’s
year made name, model, year,
color etc.)
number of doors Change a data item
size of engine
(color, engine, etc.)
Display data items
etc.
Calculate cost
etc.
31
Why OOP?
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
Design Principles of OOP
The 4 basic design principles of Object-Oriented
Programming (OOP):
Encapsulation
Abstraction
Polymorphism
Inheritance
33
1). Encapsulation
The bundling of data and methods
operating on this data into one unit (called
a class).
34
2). Abstraction
Focus only on the important facts about the
problem at hand
to design, produce and describe so that it can be
easily used without knowing the details of how it
works.
Analogy:
When you drive a car, you don’t have to know how
the gasoline and air are mixed and ignited.
Instead you only have to know how to use the
35
controls.
3). Polymorphism
Greek for “many forms”
The same word or phrase can mean different things in different
contexts.
The ability of a variable, function, or object to take on multiple
forms.
Class objects belonging to the same hierarchical tree may have
methods with the same name, but with different behaviours.
Analogy:
In English, bank can mean side of a river or a place to put
money
36
3). Polymorphism Contd.
a). Method Overloading
The operation of one function depends on the argument
passed to it.
Example: Fly(), Fly(low), Fly(150)
b). Method Overriding
Occurs when a subclass (child class) has the same method as
the parent class but the subclass provides a
particular/different implementation of the method declared
by its parent classes
37
4). 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.
Superclass – inherit its attributes & methods to the
subclass(es).
Subclass – can inherit all its superclass attributes &
methods besides having its own unique attributes &
methods.
38
An Inheritance Hierarchy
Superclass
Vehicle
Subclasses
Automobile Motorcycle Bus
Sedan Sports Car Luxury Bus School Bus
What properties does each vehicle inherit from the types
of vehicles above it in the diagram?
39
Object-Oriented Programming Languages
Pure OO Languages: Smalltalk, Eiffel, Actor, Java
Hybrid OO Languages: C++, Objective-C, Object-
Pascal, Python
40
Classes
A construct that is used as a blueprint to
create instances of the class (class instances,
class objects, instance objects or just
objects).
It defines constituent members which enable
class instances to have state and behavior.
Data field members (member variables or
instance variables) enable a class object to
maintain state.
Methods, enable a class object's behavior.
Class instances are of the type of the
41 associated class.
Classes Contd.
For example, an instance of the class "Fruit" (a
"Fruit" object) would be of the type "Fruit".
A class usually represents a noun, such as a person,
place or (possibly quite abstract) thing.
Programming languages that include classes as a
programming construct subtly differ in their
support for various class-related features. Most
support various forms of class inheritance. Many
languages also support advanced encapsulation
control features, such as access specifiers.
42
Object
Object is a runtime entity.
It is an instance of class
Represents a Place, Person, anything that has
some attributes.
43