BAHIR DAR UNIVERSITY
BIT
Faculty of Electrical and Computer engineering
Department of computer engineering
Object Oriented Programming
OOP Interpreted By: Asaminew G. 12/03/2024 1
v a
Ja
to
e
.
o m
l c
e
W
OOP Interpreted By: Asaminew G. 12/03/20 2
24
Chapter one : Get started
Outline
o What is object oriented
programming?
o Development environment
o Object oriented
fundamentals
OOP Interpreted By: Asaminew G. 12/03/2024 3
What is object oriented
programming?
Is a programming deals with a
collection of interacting objects
An object can be: -
a physical thing, such as a Car
a mental thing, such as an Idea.
a natural thing, such as an Animal
an artificial thing, such as an ATM.
OOP Interpreted By: Asaminew G. 12/03/2024 4
cont. …
Java was created in 1991 by
James Gosling of Sun
Microsystems.
Initially called Oak, latter it was
changed to Java because there
was already a language called
Oak.
OOP Interpreted By: Asaminew G. 12/03/2024 5
API, JDK and IDE
API (Application programming interface) is a
class library
Contains predefined classes and interfaces in a
package
The Java language specification is stable, but the
API is still expanding
API->package->class, interface, exception, etc.
JDK (Java Development toolkit) consists of a set
of separate programs for developing and testing
Java programs, each of which is invoked from a
command line
OOP Interpreted By: Asaminew G. 12/03/2024 6
Cont.
JDK (Java Development Kit) contains the API
as well as the compilers, runtimes, and other
miscellaneous tools. The Java API is simply all
the libraries that make up the core language
that you can work with out of the box.
IDE (Integrated Development Environment) is a
software Editing, compiling, building, debugging, and
online help are integrated in one graphical user
interface
OOP Interpreted By: Asaminew G. 12/03/2024 7
OOP fundamentals
1. Feature of OOP
Portable :- running on different platform
(window, Linux, Mac, Solaris). Java is
different from other programming language
because, it both compiled and interpreted. A
file with .class extension is contain a byte
code. Byte code is machine language of the
java virtual machine ( java VM). Java include
a set of class library that provide basic data
type, system input and output capability.
OOP Interpreted By: Asaminew G. 12/03/2024 8
Feature of OOP cont…
Platform :- is hardware or software
environment in which program run. Most
platform are a combination of OS and
hardware, but java platform is software-
only platform, this is why it run on d/t
platform. It contain two components, JVM
and API
OOP Interpreted By: Asaminew G. 12/03/2024 9
Feature of OOP cont…
…
OOP Interpreted By: Asaminew G. 12/03/2024 10
Feature of OOP cont…
Simple and small :- easier to write, compile,
debug, and to learn
Secure :- java securities API provide
cryptography, public key infrastructure, secure
communication, authentication and access control.
Multithreading :- high degree of parallelism
(concurrent activities through API ). But, other
language provide sequential control (one action at
a time)
Object oriented
OOP Interpreted By: Asaminew G. 12/03/2024 11
package and import
Package :-
is a directory that contain library. library is group a
number of related classes and interfaces
Packaging is decrease class colliding, promotes code
reuse, and maintainability
categorizing and managing the external API
Packages named java. or javax. are reserved
Example package bit.fece.ce;
Import :-
is a java keyword used to include other file from another
package into current package.
OOP
.
It use a period ( ) to separate each package structure
Interpreted By: Asaminew G. 12/03/2024 12
Indentation and comment
Indentation :- is process of aligning a line of
code according to a class or a method that
contain line of code
OOP Interpreted By: Asaminew G. 12/03/2024 13
Comment
Comment :- Java supports three types of
comments:
/* multiline comment */ : The compiler
ignores everything from /* to */ . The
comment can span over multiple lines.
// single line : The compiler ignores
everything from // to the end of the line.
Javadoc or Java documentation
comments(/**...*/)
Example :- /** the following method is perform
<B> maths </B> operation */
OOP Interpreted By: Asaminew G. 12/03/2024 14
Java keywords
Keyword :- is a predefined reserved words
for java program and they are not used as an
identifier
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
OOP Interpreted By: Asaminew G. 12/03/2024 15
Identifiers
Identifier :-
is a unique name given to a particular
variable, package, class, interface, function
or label of class in the program
a series of characters consisting of letters,
digits, underscores ( _ ) and dollar signs ($)
that does not begin with a digit and does not
contain spaces.
Example :- variable identifier
OOP Interpreted By: Asaminew G. 12/03/2024 16
Classes
Class :-
is a building block of java application
is a blueprint from which objects are created
define a set of objects which are similar in terms of
attribute, association and functionality
only one public class per source code file
a file can have more than one non public class
class name must be CamelCase contain more than
one word
Note: if there is a public class in a file, file name
must be the same as the name of public class
OOP Interpreted By: Asaminew G. 12/03/2024 17
Class cont…
Syntax of simple java file :-
package [package name]
import [other-Package]
[modifier (optional)] Class [class-Name] {
modifier fields-Name ;
Constructor method (optional)
normal method
}
OOP Interpreted By: Asaminew G. 12/03/2024 18
Fields and Data type
Field :-
is
a variable or data associated with programs such as
string, integer, array and object
also called instance variable (fields)
names should be in mixed case
Represent state of an object
are
a place where information can be stored while a
program is running
their value can be changed at any point in the program
Data type :-
is express a type of fields
E.g. string, integer, float, Boolean,…
OOP Interpreted By: Asaminew G. 12/03/2024 19
Constructor
Constructor :-
is a function called during the creation (instantiation) of
objects
its name must be similar to class name that contain it
has no return type
If you don't define a constructor for a class, a default
parameter less constructor is automatically created by the
compiler
The default constructor calls the default parent constructor
(super()) and initializes all instance variables to default value
possible to create more than one constructor with d/t
parameter
OOP Interpreted By: Asaminew G. 12/03/2024 20
Methods
Method :-
is a function called during setting a value to fields or
getting a value of fields and performing d/t operation
it may called setter or getter
may contain an argument or not
represent the behavior of the object
Name is must be mixedCase if contain more than one word
Syntax :-
Modifier return-Type method-Name (){
// body method
}
OOP Interpreted By: Asaminew G. 12/03/2024 21
Objects
Object :-
is an instance of a class/type
has a (current) state and behavior
Student, blackboard, chock, chair, and other are objects
Example :-
a dog has state (name, color, breed, hungry) and behavior
(barking, fetching, and wagging tail).
Students have state (name, age, sex) and behavior (eating,
studying, learning)
new keyword is used to create class instance (object)
thiskeyword is used in constructor and method to assign
value to fields of class i.e. referring fields in the current class
OOP Interpreted By: Asaminew G. 12/03/2024 22
Reference to object
from the new operation :-
instantiatesan object of a particular
class, and returns a reference to it
This reference is a handle to the
location where the object resides in
memory.
Student stud=new Student();
Student stud2=new Student();
OOP Interpreted By: Asaminew G. 12/03/2024 23
Reference to object
by assignment :-
duplicate reference to the object (and
not to a copy of the object).
Student stud1=stud;
N.B. After the assignment, the value
of stud1 is the same as stud. A
reference to same Student object.
OOP Interpreted By: Asaminew G. 12/03/2024 24
Reference to object
by cloning an object.
new reference to a copy of the
object
two copies of the object exist
with independent references
Changing one object does not
automatically change the other.
Student stud3 = stud.clone();
OOP Interpreted By: Asaminew G. 12/03/2024 25
Passing object as parameter
When object is passed to the
method, actual object is not
passed, instead object reference
is passed i.e. memory location of
an object is passed
In the body of called method new
object my created or parameter
object can be used as it is.
OOP Interpreted By: Asaminew G. 12/03/2024 26
Object as UML
UML representation of an object
Class name
e.g. Student
List of fields
e.g. name, age, sex,…
List of methods
getName()
setName(“Abel”)
OOP Interpreted By: Asaminew G. 12/03/2024 27
Example
…
OOP Interpreted By: Asaminew G. 12/03/2024 28
…
…
OOP Interpreted By: Asaminew G. 12/03/2024 29
Out put
…
OOP Interpreted By: Asaminew G. 12/03/2024 30