[go: up one dir, main page]

0% found this document useful (0 votes)
87 views40 pages

Chapter 1

This document provides an overview of a course on Object Oriented Programming (OOP) using Java. It discusses various programming paradigms including unstructured, procedural, modular, and object-oriented programming. It describes key OOP concepts like encapsulation, abstraction, inheritance, and polymorphism. The course will cover 5 chapters: an introduction to OOP and Java, basic Java programming, objects and classes, OOP concepts, and exception handling.

Uploaded by

Beky Kitawm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views40 pages

Chapter 1

This document provides an overview of a course on Object Oriented Programming (OOP) using Java. It discusses various programming paradigms including unstructured, procedural, modular, and object-oriented programming. It describes key OOP concepts like encapsulation, abstraction, inheritance, and polymorphism. The course will cover 5 chapters: an introduction to OOP and Java, basic Java programming, objects and classes, OOP concepts, and exception handling.

Uploaded by

Beky Kitawm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Jimma University, Institute of Technology, Faculty of Computing and Informatics

Object Oriented Programming – OOP


Information Technology
Year-III
Academic calendar Aug 02, 2022

Lecture slides compiled by Bekan Kitaw (M. Sc)


Object Oriented Programming – OOP
Contents of the course in chapters;
1. Chapter One: Introduction to OOP - Programming
2. Chapter Two: Basics in Java Programming
3. Chapter Three: Objects and Classes
4. Chapter Four: OOP Concepts
5. Chapter Five: Exception Handling

1-2
Chapter 1: Introduction
• Programming paradigm
• Object-Oriented Programming
• The Java Programming Language
• Program Development

1-3
Programming paradigm
• A programming paradigm is a style of programming or way
of programming.
• Choosing a good paradigm that helps to manage the
complexity of programs, and hence it is very important.
• There are many programming techniques. This includes:
- Unstructured programming
- Procedural programming
- Modular programming
:- Object - Oriented programming
- Logic programming

1-4
Unstructured programming
• It is the historically earliest programming paradigm.
• In this type, all the instructions of a program are written
one after the other in a single function and hence suitable
for writing only small and simple applications.
• The program must be written as a single continuous, i.e.
nonstop or unbroken block.
• Thus, it becomes harder to modify and to debug.
• Unstructured programming languages are often touted for
providing freedom to programmers to program as they
want.

1-5
Unstructured programming(Cont...)
• Producing hardly-readable (“spaghetti”) code.
• Some languages commonly cited as being non-structured
include assembly languages, MS-DOS batch files, and early
versions of BASIC, Fortran, COBOL, and MUMPS.
• Here main program stands for a sequence of commands or
statements which modify data which is global throughout the
whole program.

1-6
Procedural programming
• In this type of programming paradigm, the program is built
from one or more procedures (also called subroutines or
functions).
• Procedural programming concentrates on creating functions.
• Programs can now be written more structured and error free.
• Procedural programming offers many benefits over simple
sequential programming.
 Easier to read and more maintainable
 Allows reuse of procedures
 More flexible

1-7
Procedural programming(Cont...)
• Some languages commonly cited as being procedural
include Pascal, and C languages.
• Generally, we have a single program which is divided into
small pieces called procedures.

1-8
Modular programming

Procedures of a common functionality are grouped together
into separate modules. And this type of programming paradigm
is called Modular programming.

A module is a separate software component.

Also defined as the act of designing and writing programs as
interactions among functions that each perform a single well-
defined function.

Each module can have its own data.

This allows each module to manage an internal state which is
modified by calls to procedures of this module.

Modular programming becomes a base for object oriented
programming.

1-9
Modular programming(Cont...)
• Examples of modular programming languages - all the object
oriented programming languages like
- C++,
- Java, are modular programming languages.

1-10
Logic programming
• Logic programming is a programming paradigm based on logic.
• The logic paradigm fits extremely well when applied in problem
domains that deal with the extraction of knowledge from basic
facts and relations.
• It is a computer programming paradigm in which program
statements express facts and rules about problems within a
system of formal logic.
• Typical example for Logic programming paradigm is:
- Prolog

1-11
Object oriented programming(OOP)
• OOP is a programming paradigm that uses objects – data
structures encapsulating data fields and procedures together
with their interactions – to design applications and computer
programs.
• Object is a member (also called an instance) of a Java class
that has an identity, a behavior and a state.
• The state of an object is stored in fields (variables), while
methods (functions) display the object's behavior.
• For example: in real life, a car is an object.
- The car has idenitity which is car name.
- The car has state(attributes), such as weight and color,
- The car has behaviour(methods), such as drive and brake.

1-12
Object oriented programming(Cont...)
• Example 2:

• Associated programming techniques may include features such


as data abstraction, encapsulation, modularity, polymorphism,
and inheritance.
• Many modern programming languages now support OOP.

1-13
Outline
• Programming paradigms
• Object-Oriented Programming
• The Java Programming Language
• Program Development

1-14
Problem Solving
 The purpose of writing a program is to solve a problem.
 Solving a problem consists of multiple activities:
 Understand the problem
 Design a solution
 Consider alternatives and refine the solution
 Implement the solution
 Test the solution
 These activities are not purely linear – they overlap and
interact.

1-15
Problem Solving(Cont...)
• The key to designing a solution is breaking it down into
manageable pieces.
• When writing software, we design separate pieces that are
responsible for certain parts of the solution.
• An object-oriented approach lends itself to this kind of
solution decomposition.
• We will dissect our solutions into pieces called objects and
classes.

1-16
Object oriented concepts
 An object-oriented programming language provides
support for the following object-oriented concepts:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism

1-17
Encapsulation
• Encapsulation refers to the bundling of data members and
member functions inside of a common “box”.
• keeps both data and code safe from outside interference and
misuse.
• Encapsulation is achieved by specifying which classes may use
the members of an object.
• We can use the encapsulation concept to implement an
information-hiding mechanism.
• Information hiding refers to the notion of hiding some or all
members of the class.
• You can implement an information-hiding mechanism by
making your class attributes inaccessible from the outside.
- Access Modifiers
1-18
Encapsulation(Cont...)


If we made an instance variable public, then anyone can
change its state.

But if we make our instance variable private/protected then
actually we are restricting outside entities from making
changes to it.

1-19
Abstraction

Abstraction is simplifying complex reality by modeling
classes appropriate to the problem, and working at the most
appropriate level of inheritance for a given aspect of the
problem.

Abstraction hides implementation details from the outside
world.

It is a way to segregate implementation from interface.
- reduce complexity and
- increase efficiency
Inheritance
• Inheritance is the process by which one object acquires the
properties of another object.
• This is important because it supports the concept of
hierarchical classification.
• For example, a Golden Retriever is part of the classification
dog, which in turn is part of the mammal class, which is
under the larger class animal. Without the use of hierarchies,
each object would need to define all of its characteristics
explicitly.
• The new classes, known as subclasses (or derived classes),
inherit attributes and behavior of the pre-existing classes,
which are referred to as super classes (or ancestor classes).
• The inheritance relationships of classes gives rise to a
hierarchy. 1-21
Polymorphism
• Derived from the Greek,
- poly “many” and
- morph “form”, thus, polymorphism means ―”many forms”
• Polymorphism is the ability of objects belonging to different
types to respond to method, field, or property calls of the same
name, each one according to an appropriate type-specific
behavior.
• It is a feature that allows one interface to be used for a general
class of actions.
• More generally, the concept of polymorphism is often
expressed by the phrase “one interface, multiple methods.”

1-22
Outline
• Programming paradigms
• Object-Oriented Programming
• The Java Programming Language
• Program Development

1-23
Java
• A programming language specifies the words and symbols that
we can use to write a program.
• A programming language employs a set of rules that dictate
how the words and symbols can be put together to form valid
program statements.
• The Java programming language was created by Sun
Microsystems, Inc.
• It was introduced in 1995 and it's popularity has grown quickly
since then.

1-24
Characteristics of java
• The Java programming language is a high-level language that
can be characterized by all of the following
- Simple
- Portable
- Object oriented
- Platform Independent
- Multi Threading
- Interpreted

1-25
Simple
• Java was designed to be easy for the professional
programmer to learn and use effectively.
• If you already understand the basic concepts of object-
oriented programming, learning Java will be even easier.

Portable
• Java programs can execute in any environment (Linux,Window,
Mac etc.) for which there is a Java run-time system (JVM).
• That really means there is no implementation dependent features.

1-26
Platform Independent
• Being platform-independent means a program compiled on one
machine can be executed on any machine without any change.
• Known by WORA(Write Once, Run anywhere) slogan.
• It means if we compile a program on Windows, then we can run it
on Linux and vice versa without doing any modification of the
code.

Multi Threading
• Java supports Multithreading.
• Multithreading means handling more than one job at a time, so get
more process get done in less time than it could with just one
thread.

1-27
Object oriented
• Object-oriented programming is at the core of Java.
• Almost everything in Java is an object.
• All programs and data live within objects and classes. ‘Objects’
model Java rather than the ‘processes’.
• Java comes with an extensible set of classes organized in
packages.

Interpreted and high performance


• Java enables the creation of cross-platform programs by
compiling into an intermediate representation called Java
byte code.

1-28
Java Program Structure
 In the Java programming language:
 A program is made up of one or more classes.
 A class contains one or more methods.
 A method contains program statements.
 These terms will be explored in detail throughout the
course.
 A Java application always contains a method called main.

1-29
hello.java
public class hello
{
//-----------------------------------------------------------------
// Prints a hello world.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.println (“hello world:");

System.out.println ("Whatever you are, be a good one.");


}
}

1-30
Java Program Structure
// comments about the class
public class MyProgram
{
class header

class body

} Comments can be placed almost anywhere

1-31
Java Program Structure
// comments about the class
public class MyProgram
{

// comments about the method


public static void main (String[] args)
{
method header
method body
}
}

1-32
Outline
• Programming paradigms
• Object-Oriented Programming
• The Java Programming Language
• Program Development

1-33
Program Development
 The mechanics of developing a program include several
activities
 writing the program in a specific programming language
(such as Java).
 translating the program into a form that the computer can
execute.
 investigating and fixing various types of errors that can
occur.
 Software tools can be used to help with all parts of this
process.

1-34
Programming Languages
• Each type of CPU executes only a particular machine
language.
• A program must be translated into machine language before
it can be executed.
• A compiler is a software tool which translates source code
into a specific target language.
• Often, that target language is the machine language for a
particular CPU type.
• The Java approach is somewhat different.

1-35
Java Translation
Java source
code Java
bytecode

Java
compiler
Bytecode Bytecode
interpreter compiler

Machine
code

1-36
Development Environments
• There are many programs that support the development of
Java software, including:
 Sun Java Development Kit (JDK)
 Sun NetBeans
 IBM Eclipse
 Borland JBuilder
 MetroWerks CodeWarrior
 BlueJ
 jGRASP
• Though the details of these environments differ, the basic
compilation and execution process is essentially the same.

1-37
Syntax and Semantics
• The syntax rules of a language define how we can put together
symbols, reserved words, and identifiers to make a valid
program.
• The semantics of a program statement define what that
statement means (its purpose or role in a program).
• A program that is syntactically correct is not necessarily
logically (semantically) correct.
• A program will always do what we tell it to do, not what we
meant to tell it to do.

1-38
Errors
• A program can have three types of errors.
• The compiler will find syntax errors and other basic problems
(compile-time errors).
 If compile-time errors exist, an executable version of the
program is not created.
 A problem can occur during program execution, such as
trying to divide by zero, which causes a program to
terminate abnormally (run-time errors).
 A program may run, but produce incorrect results, perhaps
using an incorrect formula (logical errors).

1-39
Basic Program Development

Edit and
save program
errors

errors
Compile program

Execute program and


evaluate results

1-40

You might also like