Introduction to Object-
Oriented Programming (OOP)
Abdella Nurahmed
DEBRE BERHAN UNIVERSITY
DEBRE BERHAN
What is programming?
• A set of procedures.
• A set of step-by-step instructions.
04/03/2022 Abdella Nurahmed 2
What is a programming
languages?
• The way in which we tell/give the set of
procedures or programs to a computer.
• There are hundreds of programming
languages?
– Examples are: C, C++, Objective-c, java, C#...
• But computer only understands machine
code or machine language.
04/03/2022 Abdella Nurahmed 3
Programming paradigms
• Two widely known:
– Structural Programming Paradigm
• based on operations
– Object-Oriented Programming Paradigm
• based on data.
04/03/2022 Abdella Nurahmed 4
Programing techniques that
follow the structural approach
• Unstructured Programming:
– consisting only one main program.
– Here “ main program” stands for a sequence of
commands or statements
– What if the same statement sequence is needed
at different locations?
• Procedural Programming:
– Extract the same statement sequences and
combine into one called procedure.
04/03/2022 Abdella Nurahmed 5
Cont…
– Procedure call is used to invoke the
procedure.
– Introduced parameters as well as
procedures of procedures (sub
procedures).
– program can be viewed as a
sequence of procedure calls.
– What if we need to use procedure
in other program?
04/03/2022 Abdella Nurahmed 6
Cont…
• Modular Programming:
– Structure based on the
operations rather than the data.
– Procedures of a common
functionality are grouped
together into separate modules.
– Each module can have its own
data.
– Allows each module to manage
an internal state
04/03/2022 Abdella Nurahmed 7
Cont…
– There is only one state per module
– Each module exists at most once in the whole
program.
• Problems with Modular Programming:
– Explicit Creation and Destruction - explicitly call a
procedure to create and destroy the module instance
– Decoupled Data and Operations
– Missing Type Safety - the compiler cannot guarantee
for type safety.
04/03/2022 Abdella Nurahmed 8
Object – Oriented Programming
• Programs get structured by the
data rather than operations.
• Groups operations and data
into modular units called
objects.
• Indented to make thinking
about programming closed to
think about the real world.
• Solves some of the problems
04/03/2022 Abdella Nurahmed 9
In OO approach
• Emphasis is on data, not procedure
• Programs are made up of objects
• Data is hidden from external functions,
functions not part of the object.
• Objects can communicate with each other
through their methods
• New data and methods can be easily added
when necessary.
04/03/2022 Abdella Nurahmed 10
The basic concepts of OO
• Objects and classes
• abstraction and encapsulation
• Inheritance
• Polymorphism
04/03/2022 Abdella Nurahmed 11
Objects
• An identifiable thing or Self contained
• Have identity separate from other object
• Have their own attribute that describe their
current state.
• Have behavior which meaning things they can do.
• Objects interact with each other by sending
messages between them.
• Each object is responsible to initialize and destroy
itself correctly.
04/03/2022 Abdella Nurahmed 12
Class
• Describes what an object will be
• A blueprint, a detail description and definition
• Has:
– Name: what is it?
• Ex: Customer, Employee, Player, BankAccount etc.
– Attribute: what describes is it?
• Ex: Name, Address, Width, Height
– Behavior: what can it do?
• Ex: amendAdress, open, save, play
04/03/2022 Abdella Nurahmed 13
Cont…
• Example, a class named ‘Customer’ has 3-
attributes and 2-methods (functions).
04/03/2022 Abdella Nurahmed 14
Cont…
• Examples of Objects created using this class
could be:
04/03/2022 Abdella Nurahmed 15
Data Abstraction and
Encapsulation
• Abstraction
– way in which a class represents only the
essential features of the set of objects it models
– Focus on the essentials rather than specific
– Ignores irrelevant or unimportant
– So a class is like a list of abstract
• attributes e.g. size, weight, cost, and
• the methods that operate on those attributes e.g.
change the cost, increase the weight.
04/03/2022 Abdella Nurahmed 16
Cont…
• Encapsulation:
– wrapping up of data and methods into a single
unit (a class).
– the data inside a class is hidden from everything
outside the class that mean information hiding.
– Restricts access internal workings of a class.
– Also known as “Black box”
04/03/2022 Abdella Nurahmed 17
Inheritance and Polymorphism
• Inheritance:
– the way in which objects of one class get the
properties of objects of another class.
• includes the data/ attributes and the methods.
– Form of code reuse.
• Polymorphism:
– the ability to take more than one form/structure.
– A particular operation/method may behave
differently for different number of inputs.
04/03/2022 Abdella Nurahmed 18
The Benefits of OO
• Reuse of existing classes and eliminate redundant
code using inheritance.
• Hiding of data(data/information hidding)
• Structure of the system is more likely to be
meaningful to users.
• Easier to split work up between a team of developers.
• OO systems can be easily upgraded from small to
larger systems.
• Software complexity can be easily managed
04/03/2022 Abdella Nurahmed 19
Compiled and Interpreted
Languages
• Two main ways of converting source code
into machine code.
• In compiled languages the compiler:
– go through source code and create a separate
machine code file.
• In Interpreted languages the interpreter:
– going through source code line by line and
processing it on-the-fly.
04/03/2022 Abdella Nurahmed 20
Cont…
04/03/2022 Abdella Nurahmed 21
Intermediate/hybrid approach
• Source code compiled partially into intermediate
language.
• Intermediate language:
– sometimes called bytecode.
– It is cross-platform
• Ex: Windows, Linux, Mac...
• Intermediate language compiled into a machine
code on the host computer.
• Sometimes referred to as Just-In-Time or JIT
compilation.
04/03/2022 Abdella Nurahmed 22
Language Example
• Compiled:
– C, C++, Objective-c
• Interpreted:
– Php, javascripte
• Hybrid:
– Java, C#, VB.NET,Python
04/03/2022 Abdella Nurahmed 23
Features of Java programming
language
• Object Oriented
• Compiled and interpreted
(Intermediate/hybrid approach)
• Platform independent & Portable
04/03/2022 Abdella Nurahmed 24
Java Compiling and interpreting
process
• Java compiler translates the program into an
intermediate language called Java byte-codes.
– The Java byte-codes are like a set of machine code
instructions for the Java VM.
• Java interpreters (different for different
platforms) parse and run each of the Java
byte-code instructions to each machine code.
• The interpreters are usually called Virtual
Machine.
04/03/2022 Abdella Nurahmed 25
Cont…
04/03/2022 Abdella Nurahmed 26
Cont…
04/03/2022 Abdella Nurahmed 27
JAVA development Environment
• includes
– many number of development tools and
– hundreds of classes & methods.
• The development tools are part of the system
known as java development kit (JDK).
• The set of classes and methods are part of the
Java Class Library(API), also known as
Application Programming Interface(API).
04/03/2022 Abdella Nurahmed 28
Java Development Kit (JDK)
• Include tools:
– javac --- java compiler
– java --- java interpreter
– jdb --- java debugger etc...
04/03/2022 Abdella Nurahmed 29
Main Java API packages
• The Java API consists of many built-in classes
and methods that we can use in our code.
• Related classes are grouped into a form
packages.
• Some of the most commonly used packages:
– Language support package (java.lang) – classes
required for implementing basic features of Java.
– Utilities package (java.util) – classes that provide
various utility functions such as date and time
functions.
04/03/2022 Abdella Nurahmed 30
Cont…
– Input/Output package (java.io) – classes
required for manipulation of input/output
to/from programs
– Networking Package (java.net) – classes used
for communicating with other programs/PCs
over networks and internet
– Applet Package (java.applet) – classes that are
used to create Java applets.
04/03/2022 Abdella Nurahmed 31