Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

BAHIR DAR UNIVERSITY

BIT
Faculty of Electrical and Computer engineering
Department of computer engineering

Object Oriented Programming

OOP Interpreted By: Asaminew G. 10/25/2017 1


.

OOP Interpreted By: Asaminew G. 10/25/201 2


7
Chapter one : Get started

Outline
o What is computer?
o What is object oriented programming?
o Why java?
o The Java Language Specification, API, JDK,
and IDE
o Designing Good Programs (SE life cycle)
o Object oriented fundamentals
OOP Interpreted By: Asaminew G. 10/25/2017 3
What is computer ?
 An electronic device that stores and processes
data.
 Works under the control of a computer program,
a set of instructions that tell a computer what to
do
 Includes both hardware and software
 Hardware is the physical (electronic and mechanical
components) aspect of the computer that can be seen
Software is the invisible instructions that control
the hardware and make it perform specific tasks.

OOP Interpreted By: Asaminew G. 10/25/2017 4


Type of software
 Application software refers to programs
designed to provide a particular task or service,
such as word processors, computer games,
spreadsheet programs, and Web browsers
 System software includes programs that
perform the basic operations that make a
computer usable. For example, operating
system, which contains programs that manage
the data stored on the computer's disks.

OOP Interpreted By: Asaminew G. 10/25/2017 5


Hardware component

OOP Interpreted By: Asaminew G. 10/25/2017 6


Flow of information

OOP Interpreted By: Asaminew G. 10/25/2017 7


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. 10/25/2017 8


Why java ?
 BCPL -> B -> C -> C++ -> Java
 BCPL was developed in 1967 by Martin Richards as
a language for writing operating systems software
and compilers. Ken Thompson modeled many
features in his language B after their counterparts in
BCPL, using B to create early versions of the UNIX
operating system at Bell Laboratories in 1970.
 The C language was evolved from B by Dennis
Ritchie at Bell Laboratories and was originally
implemented in 1972. It initially became widely
known as the development language of the UNIX
operating system.
OOP Interpreted By: Asaminew G. 10/25/2017 9
Java
was created in 1991
by James Gosling of Sun
Microsystems.
Initially called Oak, latter was
changed to Java because there was
already a language called Oak.
OOP Interpreted By: Asaminew G. 10/25/2017 10
What is Java Technology?
 A programming language

As a programming language, Java can create


all kinds of applications that you could create
using any conventional programming
language.
OOP Interpreted By: Asaminew G. 10/25/2017 11
What is Java Technology?...
 A Development Environment

As a development environment, Java technology


provides you with a large suite of tools:
– A compiler (javac)
– An interpreter (java)
– A documentation generator (javadoc)
– A class file packaging tool and so on...
OOP Interpreted By: Asaminew G. 10/25/2017 12
What is Java Technology?...
 An Application and Runtime Environment

Java technology applications are typically


general-purpose programs that run on any
machine where the Java runtime environment
(JRE) is installed.
OOP Interpreted By: Asaminew G. 10/25/2017 13
What is Java Technology?...
 Deployment Environment

There are two main deployment environments:


1. The JRE supplied by the Java 2 Software
Development Kit (SDK) contains the
complete set of class files for all the Java
technology packages, which includes basic
language classes, GUI component classes,
and so on.
OOP Interpreted By: Asaminew G. 10/25/2017 14
What is Java Technology?...
 Deployment Environment…

2. The other main deployment environment is


on your web browser.
Most commercial browsers supply a Java
technology interpreter and runtime
environment.

OOP Interpreted By: Asaminew G. 10/25/2017 15


The Java Language Specification

 The Java language specification is a technical


definition of the language that includes the syntax
and semantics of the Java programming language.

Reading assignment : Java language specification @


java.sun.com/docs/books/jls

OOP Interpreted By: Asaminew G. 10/25/2017 16


API, JDK and IDE

 API (Application programming interface) is a class library


 Containspredefined 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
 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. 10/25/2017 17


Designing Good Programs (SE life cycle)

 SoftwareEngineering is the process of designing


and writing software.
 The software life cycle refers to the different
phases involved in the design and development
of a computer program

OOP Interpreted By: Asaminew G. 10/25/2017 18


OOP Interpreted By: Asaminew G. 10/25/2017 19


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. 10/25/2017 20


Feature of OOP cont…
 Platform :-
 is hardware or software environment in which
program run.
 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. 10/25/2017 21


Feature of OOP cont…

…

OOP Interpreted By: Asaminew G. 10/25/2017 22


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. 10/25/2017 23
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.

 It .
use a period ( ) to separate each package structure

OOP Interpreted By: Asaminew G. 10/25/2017 24


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. 10/25/2017 25


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. 10/25/2017 26


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. 10/25/2017 27
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. 10/25/2017 28


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. 10/25/2017 29
Class cont…

 Syntax :-
package [package name]
import [other-Package]
[modifier (optional)] Class [class-Name] {
modifier fields-Name ;
Constructor method (optional)
normal method
}
OOP Interpreted By: Asaminew G. 10/25/2017 30
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 :-
 isexpress a type of fields
 E.g. string, integer, float, Boolean,…

OOP Interpreted By: Asaminew G. 10/25/2017 31


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
 Ifyou 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. 10/25/2017 32


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. 10/25/2017 33


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)
 this keyword 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. 10/25/2017 34
Creating reference to object
from the new operation :-
instantiates an 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. 10/25/2017 35
Cont…
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. 10/25/2017 36


Cont…
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. 10/25/2017 37


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. 10/25/2017 38
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. 10/25/2017 39


Example

…

OOP Interpreted By: Asaminew G. 10/25/2017 40


…

OOP Interpreted By: Asaminew G. 10/25/2017 41


Out put

…

OOP Interpreted By: Asaminew G. 10/25/2017 42

You might also like