Object-Oriented Programming
(Cosc2051)
I n t ro d u c t i o n t o
Object-Oriented CHAPTER -
Pro g r a m m i n g
ONE
B y:
M u l u ke n Yo h a n n i s ( M S c )
1
Contents
Programming Paradigm
Overview of OOP principles
Editing, Compiling and Interpreting java program
2
Programming Paradigms
A paradigm is a method to solve some problem or do some task.
Programming paradigm is a method of thinking about computation and how tasks
are carried out and structured on computers.
A good programming language should support multiple paradigms because
different programming problems require a different way to approach.
If a programming language only supports one or two paradigms, it may be
insufficient or lacking features.
3
Cont..
There are different types of programming paradigm some are the
followings:
Procedural programming
Functional Programming
Object oriented programming
4
Procedural Programming Paradigm
It focuses on the step-by-step instructions that tell the computer “what
to do” to solve a problem.
In POP the problem is viewed as the sequence of things to be done,
such as:
reading, calculating and printing
Primary focus is on procedural call (functions)
Problem is decomposed into tasks
5
Cont..
Key Features of Procedural Programming
Predefined functions
It is typically an instruction identified by a name.
It usually, the predefined functions are built into higher-level programming
languages, but they are derived from the library or the registry, rather than the
program.
One example of a pre-defined function is ‘charAt()’, which searches for a
character position in a string.
6
Cont..
Local Variable
It is a variable that is declared in the main structure of a method and is limited to
the local scope.
It can only be used in the method in which it is defined,
If it is used outside of that method, the code will stop working.
Global Variable
It's a variable that's declared outside of every other function in the program.
Due to this, global variables can be used in all functions, unlike a local variable.
7
Cont..
Modularity
A module is defined as a part of a software program that contains one or more
routines.
When we merge one or more modules, it makes up a program.
Parameter Passing
It is a mechanism used to pass parameters to functions, subroutines or
procedures.
It can be done through ‘pass by value’, ‘pass by reference’, ‘pass by result’,
‘pass by value-result’ and ‘pass by the name’.
8
Structure of POP
In POP problem is represented by instructions/actions.
Instructions are grouped into functions
In a multi-function program, important data items are placed as global
May be accessed by all the functions.
9
Cont..
Each function may have its own local data
Global data can be affected by an unintentional function change.
Procedural approach does not model real world problems very well
Emphasis is on action, not data
10
Cont..
Advantages
It is excellent for general-purpose programming.
The coded simplicity along with ease of implementation of
compilers and interpreters.
Easy to track program flow.
Offers a simple and straightforward way of writing sequential code.
11
Cont..
Disadvantages
The program code is harder to write.
The Procedural code is often not reusable.
Difficult to relate with real-world objects.
Not easy to scale up or extend for larger applications.
Not secure, because of visibility of data across the whole program.
12
Cont..
Some of the common languages
Fortran
Pascal
C
Basic
13
Functional Programming Paradigm
It is about passing data from function to function to get a result.
Functions are treated as data, meaning you can use them as parameters,
return them, build functions from other functions, and build custom
functions.
Functions in FP have to be pure functions, they should avoid shared state,
and side effects and data should be absolute.
A pure function is a function that given the same type of input will always
return the same output, it is not dependent on a local or global state.
14
Cont..
A shared state is a state that is shared between more than one function
or more than one data-structure.
So with shared state, in order to understand the effects of a function,
you need to know all the details of every shared variable.
It adds a lot of complexity and allow less modularity.
15
Cont..
Advantages
Reliable: Pure functions will always return the same output for a given set of inputs, and
will not have any side effects on the rest of the program.
Transparent: All of the parameters that would be involved in the processing of the
function's return value would be clearly passed.
Easier Debugging: Because of immutability, developers don't have to keep track of a
variable's state throughout the program; instead, they can target variables at specific
locations in the code where they may be causing issues.
Lazy Evaluation: Functional programming allows for lazy evaluation, i.e. values of
variables are calculated only when it is required.
16
Cont..
Disadvantages
Using recursion for every iteration operation is unintuitive, as compared to using
while and for loops.
Reduced performance because of immutability : Especially when simple changes
require duplicating big data structures.
Difficult or inefficient to perform recursion without letting variables be updated
(immutability).
17
Cont..
Some of the common languages
Haskell
Clojure
Scala
F#
Mathematica
18
Object-oriented Programming (OOP)
It is an approach to programming which recognizes life as we know.
It treats data as a critical element in the program development.
Does not allow data to flow freely around the system.
Ties data more closely to the function that operate on it, and protects it from
accidental modification from outside function.
Problem decomposed into a number of entities called objects and then builds data
and function around these objects.
19
Features of OOP
Emphasis is on data rather than procedure.
Programs are divided into objects.
Data structures characterize the objects.
Functions that operate on the data of an object are tied together in the
data structure.
20
Cont..
Data is hidden and cannot be accessed by external function.
Objects may communicate with each other through function.
New data and functions can be easily added whenever necessary.
Follows bottom up approach in program design.
21
Benefits of OOP
It is possible to map object in the problem domain to those in the program.
It is easy to partition the work in a project based on objects.
The data-centered design approach enables us to capture more details of a model in
implementable form.
Object-oriented system can be easily upgraded from small to large system.
Message passing techniques for communication between objects makes to interface
descriptions with external systems much simpler.
Software complexity can be easily managed.
22
Cont..
Advantages
Reusability Through classes and objects, and inheritance of common
attributes and functions.
Security: Hiding and protecting information through encapsulation.
Maintenance: Easy to make changes without affecting existing objects
much.
Inheritance: Easy to import required functionality from libraries and
customize them.
23
Cont..
Disadvantages
Steep learning curve: It is complex to create programs based on interaction
of objects. (somewhat)
Larger program size: Object-oriented programs typically involve more lines
of code than procedural programs.
Slower programs: It is typically slower than procedure-based programs, as
they typically require more instructions to be executed.
24
Applications of OOP
Real-time system
Simulation and modeling
Object-oriented data bases
Hypertext, Hypermedia, and ExperText
AI and expert systems
Neural networks and parallel programming
Decision support and office automation systems
CAM/CAD systems
25
Cont..
Some of the common languages
Java
C++
Python
Perl
C#
JavaScript
26
x t
N e
27
Overview of OOP principles
These are the four main principles of the object-oriented programming
paradigm.
Understanding them is essential to becoming a successful programmer.
Abstraction
Encapsulation
Inheritance
Polymorphism
28
Abstraction
Data Abstraction is the property by virtue of which only the essential
details are displayed to the user.
The non-essentials units are not displayed to the user.
Example: A car is viewed as a car rather than its individual
components.
It can also be defined as the process of recognizing only the necessary
properties of an object while discarding the non-essential elements.
29
Cont..
Consider a real-life example of a man driving a car.
The man just understands that hitting the accelerators accelerates the car
and applying the brakes slows it down.
But he has no idea how pressing the accelerator increases the vehicle's
speed.
He has no idea of the car's inner workings or how the accelerator, brakes,
and other controls are implemented.
This is what abstraction is.
30
Encapsulation
It is a process of wrapping code and data together into a single unit.
Encapsulation helps with data security, allowing you to protect the
data stored in a class from system-wide access.
It's the structure that binds code and the data it manipulates together.
Another way to think about encapsulation is, it is a protective shield
that prevents the data from being accessed by the code outside this
shield.
31
Cont..
We can create a encapsulated class in java by making all the data members of the
class private.
To set and get the data in it, we use set and get methods.
The data in a class is hidden from other classes, so it is also known as data-hiding.
32
Inheritance
It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class.
Super Class
The class whose features are inherited is known as superclass(or a base class or a parent class).
Sub Class
The class that inherits the other class is known as subclass(or a derived class, extended class, or child class).
The subclass can add its own fields and methods in addition to the superclass fields and methods.
Reusability
Inheritance supports the concept of “reusability”,
i.e. when we want to create a new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class.
By doing this, we are reusing the fields and methods of the existing class.
33
Polymorphism
In Java polymorphism is mainly divided into two types:
Compile-time Polymorphism
Runtime Polymorphism
Compile-time polymorphism
It is also known as static polymorphism.
This type of polymorphism is achieved by function overloading or
operator overloading.
34
Cont..
Runtime polymorphism
It is also known as Dynamic Method Dispatch.
It is a process in which a function call to the overridden method is
resolved at Runtime
This type of polymorphism is achieved by Method Overriding.
Method overriding occurs when a derived class has a definition for one of
the member functions of the base class.
That base function is said to be overridden.
35
Summary
36
Editing, Compiling and Interpreting Java Program
Java Development Kit (JDK)
It is a software development environment used for developing Java applications
and applets.
It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a
compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other
tools needed in Java development.
37
Cont..
JVM (Java Virtual Machine)
It is an abstract machine.
It is called a virtual machine because it doesn't physically exist.
It is a specification that provides a runtime environment in which
Java bytecode can be executed.
It can also run those programs which are written in other languages
and compiled to Java bytecode.
38
Cont..
JVMs are available for many hardware and software platforms.
The JVM performs the following main tasks:
Loads code
Verifies code
Executes code
Provides runtime environment
39
Cont..
JRE (Java Runtime Environment)
It is a set of software tools which are used for developing Java
applications.
It is used to provide the runtime environment.
It is the implementation of JVM.
It contains a set of libraries + other files that JVM uses at runtime.
40
Cont..
Compiling Java Program
First your code must write the code using text editor and save with .java
extension.
The file name must be the same as the class name.
After this you can compile your program using javac compiler, which translate
java source code into java byte code.
The output from a Java compiler comes in the form of Java class files with .class
extension.
41
Cont..
Interpreting and running Java Program
Interpreting and running a Java program is process of executing the Java Program.
In this process the JVM is invoked and it takes the byte code and interprets it.
In this process the byte code is converted to platform-dependent machine codes so
that your computer can understand and run the program.
Once your program successfully compiles into Java byte codes, you can interpret
and run your applications on any JVM or JVM enabled web browser as applet.
42
Cont..
Whatever Java program you run using JRE or JDK goes into JVM
and JVM is responsible for executing the java program line by
line, hence it is also known as an interpreter.
43
Summary
JDK JRE JVM
JDK is a software development It is a software bundle which provides JVM executes Java byte code and
kit to develop applications in Java class libraries with necessary provides an environment for
Java. components to run Java code. executing it.
It contains tools for developing, It contains class libraries and other
Software development tools are not
debugging, and monitoring java supporting files that JVM requires to
included in JVM.
code. execute the program.
It is the superset of JRE It is the subset of JDK. JVM is a subset of JRE.
The JDK enables developers to
create Java programs that can be The JRE is the part of Java that creates It is the Java platform component
executed and run by the JRE the JVM. that executes source code.
and JVM.
44
JAVA IDE
It is stands for Integrated Development Environment, is a program which helps you
write software.
It helps you organize your software projects, write code, and then test and debug it.
Popular IDEs include, NetBeans IntelliJ and Eclipse.
45
Question & Answer
46
Thank
you
47