0% found this document useful (0 votes)
30 views

OOP Java Chapter One

The document discusses object oriented programming principles including abstraction, encapsulation, inheritance, and polymorphism. It provides examples of each principle in Java and defines key object oriented concepts like classes, objects, and methods.

Uploaded by

mesfin snow
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

OOP Java Chapter One

The document discusses object oriented programming principles including abstraction, encapsulation, inheritance, and polymorphism. It provides examples of each principle in Java and defines key object oriented concepts like classes, objects, and methods.

Uploaded by

mesfin snow
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Object Oriented Programming

Mattu University
College of Engineering & Technology

Object Oriented Programming


Lecture One

Introduction to Object Oriented Programming

Engineering and Technology College Mattu University


Types of programming paradigm
What is programming paradigm?
 Paradigm is a model of programming based on distinct concepts that shapes
the way programmers design, organize and write programs.
 Common programming paradigms are as follows:

 Unstructured programming

 Structured (procedural) programming

 Modular programming and

 Object oriented programming

Engineering and Technology College Mattu


Rift University
valley University
Unstructured programming
Its old programming approach where the entire program is a single algorithm.
Example 1
Bank Account Program
int main ()
{
int balance = 0; int amount;
cout<<” enter amount to deposit” <<endl;
cin>>amount; //3000
balance = balance + amount;
cout<<” balance=” <<balance<<endl;
cout<<” enter amount to withdraw” <<endl;
cin>>amount //1000
balance = balance - amount;
cout<<” balance=” <<balance<<endl; return 0;
2
}
Engineering and Technology College Mattu
Rift University
valley University
Structured (procedural) programming
o It is also known as procedure-oriented programming.
o Units of work is broken down to small tractable parts.
o Function is come to existence.
o Identical lines of codes segments with a specific purpose are written in
separate function.
o The structured programming approach to program design was based on
the following advice:
 To solve a large problem,

 break the problem into several pieces and work on each piece
separately;
 to solve each piece, treat it as a new problem which can itself be
broken down into smaller problems;

Engineering and Technology College Mattu


Rift University
valley University
Bank Account Program
int balance = 0; int amount;
void deposit () {
cout<<” enter amount to deposit” <<endl; cin>>amount;
//3000
balance = balance + amount; }
void withdrawal () {
cout<<” enter amount to withdraw” <<endl; cin>>amount
//1000
balance = balance - amount; }
void display () {
cout<<” balance=” <<balance<<endl; }
int main () {
deposit (); withdraw (); display (); return 0;
} 4

Engineering and Technology College Mattu


Rift University
Valley University
Object oriented programming

It is a new programming approach that provides a way of modularizing


programs by creating partitioned memory area for both data and functions
that can be used as templates for creating copies of such modules on
demand.

Engineering and Technology College Mattu


Rift University
Valley University
Overview of Object-Oriented principles
Java is a class-based object-oriented programming (OOP) language that is built
around the concept of objects.

o OOP concepts (OOP) intend to improve code readability and reusability by

defining how to structure a Java program efficiently.

o The main principles of object-oriented programming are:

 Abstraction

 Encapsulation

 Inheritance

 polymorphism

Engineering and Technology College Mattu


Rift University
Valley University
What are OOP concepts in Java?
 OOP concepts allow us to create specific interactions between Java objects.

 They make it possible to reuse code without creating security risks or making a

Java program less readable.

Abstraction

o Abstraction aims to hide complexity from the users and show them only the

relevant information.

o For example, if you want to drive a car, you don’t need to know about its

internal workings.

o The same is true of Java classes.

Engineering and Technology College Mattu


Rift University
Valley University
Abstraction in Java
 Hides the underlying complexity of data ·
 Helps to avoid repetitive code
 Presents only the signature of internal functionality
 Gives flexibility to programmers to change the implementation of the abstract
behavior.
Encapsulation
 Encapsulation allows us to protect the data stored in a class from system-wide
access.
 As its name suggests, it safeguards the internal contents of a class like a real-life
capsule.
 You can implement encapsulation in Java by keeping the fields (class variables)

private and providing public getter and setter methods to each of them.

Engineering and Technology College Mattu


Rift University
Valley University
Encapsulation in Java

o Restricts direct access to data members (fields) of a class.

o Fields are set to private

o Each field has a getter and setter method Getter methods return
the field
o Setter methods let us change the value of the field.
Polymorphism
 Polymorphism to the ability to perform a certain action in
different ways.
 In Java, polymorphism can take two forms: method overloading
9
and method overriding.
Engineering and Technology College Mattu
Rift University
Valley University
Polymorphism in Java

 The same method name is used several times.

 Different methods of the same name can be called from the

object.

 All Java objects can be considered polymorphic (at the minimum,

they are of their own type and instances of the Object class).

 Example of static polymorphism in Java is method overloading.

 Example of dynamic polymorphism in Java is method overriding.

10

Engineering and Technology College Mattu


Rift University
valley University
Inheritance
It makes it possible to create a child class that inherits the fields and
methods of the parent class.
Inheritance in Java
o A class (child class) can extend another class (parent class) by inheriting
its features.
o Implements the DRY (Don’t Repeat Yourself) programming principle.

o Improves code reusability.

o Multilevel inheritance is allowed in Java (a child class can have its own
child class as well).
o Multiple inheritances are not allowed in Java (a class can’t extend more
than one class).. 11

Engineering and Technology College Mattu


Rift
Mettu University
valley University
University
Overview of Java Programming and types of Java Program
 Java is a robust, general-purpose, high-level programming language and

a powerful software platform.

 It is also object-oriented, distributed, portable and multi-threaded.

 Java follows the 'Write - once - run - anywhere' approach.


 All Java programs must run on the Java platform that has two
components, the Java Virtual Machine (JVM) and the Java
Application Programming Interface (API).
There are two core types of java program namely:
1. Java applications and
2. Java applets
12

Engineering and Technology College Mattu


Rift University
valley
Mettu University
University
Definition of Java Application, Java Applets
Java applications: The Java applications are the standalone programs
written in Java to carry out certain tasks.
 These applications run directly by the Java interpreter.

 A standalone application should be delivered and installed on each


computer before it is run.
Java Applets: Java applets are the Java programs that are embedded
inside HTML files and can be downloaded into a Java-capable
browser (E.g. Netscape and Hot Java).
An applet can perform tasks and interact with users on their browser's
Webpages without using resources from the web server after being
downloaded. 13

Engineering and Technology College Mattu


Rift
Mettu University
valley University
University
Implementing Java Programming (Editing, Compiling and Interpreting)
Implementation of a java program involves a series of steps.
They include :
 Creating the program

 Compiling the program

 Running the program

Remember that, before we begin creating the program, the Java


Development Kit(JDK) must be properly installed on our
system.

14

Engineering and Technology College Mattu


Rift
Mettu University
valley University
University
Contd..
JDK- Java development kit is all collection of programs needed to develop a Java
program it comprises JRE and compiler, interpreter and another useful programs.
Editing (Creating Java Program)
We can create a Java program using any text-editor software such as:
Notepad, Notepad++, Sublime text and etc.
and also we can use IDE (integrated development environment) such as
NetBeans, eclipse and etc.
Assume that we have create the following program using text-editor
class Test {
public static void main(String [] args) {
System.out.println(“Hello world”);
}
15

Engineering and Technology College Mattu


Rift
Mettu University
valley University
University
We must save this program in a file called Test.java

Compiling Java program


To compile the program, we must run the Java compiler Javac,
with the name of the source file on the command line as shown
below;
javac Test.java

If everything is ok, the javac compiler creates a file called Test.class


containing the bytecodes of the program. Note that the compiler
automatically names the byte code as file as

<classname>.class

16

Engineering and Technology College Mattu


Rift
Mettu University
valley University
University
Java virtual machine

17

Engineering and Technology College Mattu


Rift
Mettu University
valley University
University
Contd..

18

Engineering and Technology College Mattu


Rift
Mettu University
valley University
University
End of chapter one

Engineering and Technology College Mattu


Rift
Mettu University
valley University
University

You might also like