Prepared by Melak E.
Object-Oriented Programming in java
10/6/2025 1
chapter 1:Introduction of OOP
objectives
• To review computer programs and programming language
• To understand what programming paradigm is?
• To understand what OOP is?
• To distinguish the terms JRE,JVM, IDE, and JDK.
• To write a simple Java program .
• To display output on the console .
• To explain the basic syntax of a Java program
•10/6/2025
To create, compile, and run Java programs . 2
What is Computer Program?
• Computer programs, known as software, are instructions to the
computer.
• You tell a computer what to do through programs.
• Without programs, a computer is an empty machine.
• Computers do not understand human languages, so you need to
use computer languages to communicate with them.
• Programs are written using programming languages.
• What is software , program and code ?
10/6/2025 3
What is Programming Languages?
• Programming languages are languages that programmers use to
write code/program to instruct a computer.
• Computers do not understand human languages, so programs
must be written in a language a computer can use.
• Programming languages are classified into three categories.
Machine, Assembly and High- Level Language
• Machine Language:1101101010011010
• Assembly Language:ADD 2, 3, result
• High-Level Language:Some popular ones are C, C++, C#, VIB
and [Link] for this course Java is our focus.
10/6/2025 4
What is Programming Paradigm ?
• Solving a programming problem requires choosing the right
concepts.
• All but the smallest toy problems require different sets of concepts
for different parts.
• This is why programming languages should support many
paradigms.
• A paradigm is a way of doing something .
• A programming paradigm a fundamental style or approach to
programming that defines how developers structure and organize
code, as well as how they think about and solve problems.
10/6/2025 5
Some Major Types of Programming Paradigm
1. Imperative or procedural
• It is based on commands that update variables in storage.
• Control flow is an explicit sequence of commands.
• Commands show how the computation takes place, step
by step.
• Each step affects the global state of the computation.
• Some programming languages used in this paradigm are
• Fortran,Pascal, Basic, C etc
10/6/2025 6
Cont...
• 2. Declarative
• Control flow in declarative programming is implicit:
• programmer states only what the result should look like, not how
to obtain it. e.g SQL
3. Logical or Rule based
• It is a set of sentences in logical form, expressing facts and rules
about some problem domain.
• Based on formal logic, where programs consist of a set of facts
and rules.
• Major logic programming language families include PROLOG.
10/6/2025 7
Cont...
4. Functional
• In this paradigm we express computations as theevaluation of
mathematical functions.
• The reason is that the paradigm originates from a purely
mathematical discipline: the theory of functions.
• In functional programming control flow is expressed by
combining function calls, rather than by assigning values to
variables.
• e.g Haskell, Lisp, Scala.
10/6/2025 8
Cont...
[Link]-Oriented
• The object-oriented paradigm has got great popularity in the recent years.
• Object-Oriented Programming is a programming paradigm based on the
concept of “Objects”, which may contain data, in the form of fields, often
known as Attributes; and code, in the form of Methods.
• These properties are very important when programs become larger and
larger.
• Java, C++, C#, python are some examples of object-oriented
10/6/2025 9
overview of oop
• Object-Oriented Programming (OOP) is a programming paradigm that
organizes code into objects, which are instances of classes.
Here are some key concepts and principles of Object-Oriented Programming:
• Class: A blueprint or template for creating objects. It defines the properties and
behaviors common to all objects of a certain kind.
• Object: An instance of a class. It is a concrete entity created based on the class
definition.
• Encapsulation:refers to the bundling of data (attributes) and the methods
(functions) that operate on that data into a single unit, i.e., a class. It helps in
hiding the internal details of an object and exposing only what is necessary.
10/6/2025 10
Continued…
• Inheritance: is a mechanism that allows a new class (subclass or derived class) to
inherit properties and behaviors of an existing class (base class or parent class).
This promotes code reuse and establishes a relationship between classes.
• Polymorphism: allows objects of different types to be treated as objects of a
common base type. It enables a single interface to represent different types and is
often expressed through method overriding and interfaces.
10/6/2025 11
Continued…
• java is a high-level, object-oriented programming language that was developed by
Sun Microsystems and released in 1995.
• It is designed to be platform-independent, meaning that Java programs can run on
any device that has a Java Virtual Machine (JVM) installed, regardless of the
underlying hardware and operating system.
• Java technology applications are typically general-purpose programs that run on
any machine where the Java runtime environment (JRE) is installed.
• Java Technology mainly consists two parts
§ JDK
§ JRE.
10/6/2025 12
Java Development Kit (JDK)
• JDK is a software development environment that provides the tools
needed to
is essential for developing Java applications, containing the necessary
tools and libraries.
• The tools such as the JDK is a superset compilers of the JRE and and
contains everything that is in the debuggers necessary for developing
applets JRE, plus and applications.
Java Platform = JDK (because JRE is subset of JDK
JRE = JVM + Java Packages librariesof Classes(like util, math, lang, awt,
swingetc) +
10/6/2025 13
Continued…
Components:
Compiler (javac): Converts Java source code into bytecode.
• Java Runtime Environment (JRE): Allows running Java applications.
10/6/2025 14
JDK Editions
1. Java Standard Edition (J2SE / Java SE)
• Provides libraries for general-purpose programming (e.g., data
structures, networking, GUI).
• Mainly for desktop/standalone applications.
2. Java Enterprise Edition (J2EE / Java EE)
• Adds libraries and frameworks for enterprise and web-based
applications.
• Includes Servlets, JSP, EJB, and Web Services.
3. Java Micro Edition (J2ME / Java ME)
• Designed for mobile devices and embedded systems with limited
resources.
10/6/2025 15
Java Runtime Environment (JRE)
• JRE is the program that emulates the JVM, so that users can run Java
programs.
• To run Java programs, you need download and install the JRE.
• Runs code compiled for a JVM and performs class loading (through
the class loader), code verification (through the bytecode verifier) and
finally code execution.
• The major component of JRE are
– Java platform core classes libraries
– Java virtual machine (JVM)
10/6/2025 16
Integrated Development Environments (IDEs)
• IDEs are software applications(tools) that provide
comprehensive facilities to programmers for software
development.
• Editing, compiling, debugging, and online help are integrated in
one graphical user interface.
• Popular Java IDEs:
• Eclipse:
• IntelliJ
• NetBeans:
10/6/2025 17
Why Java?
Java is a popular and widely used programming language for various
reasons. Here are some key factors that contribute to Java's popularity
Platform Independence: Java programs are compiled into an
intermediate bytecode that can be executed on any device with the Java
Virtual Machine (JVM).
Object-Oriented Paradigm: It follows OOP principles such as
encapsulation, inheritance, and polymorphism, making it easier to
design, develop, and maintain complex software systems.
frameworks, and tools.
10/6/2025 18
Some Uses of Java
• Applications
• Java applets (running from a Web browser).
• Server-side applications
• Multimedia application
• Game Application
Etc.
10/6/2025 19
Features of Java
Simple
Here are some aspects highlighting why Java is often considered a
simple language:
• Syntax Similar to C++
• No Pointers
• Standard Libraries:Java comes with a rich set of standard libraries
that provides pre-built functionalities for common tasks, reducing the
need for developers to write code from scratch.
• Platform Independence:Java's "Write Once, Run Anywhere"
(WORA) principle allows Java programs to run on any device with a
Java Virtual Machine (JVM), making it easier to deploy applications
across various platforms.
10/6/2025 20
Object oriented
• focus on the data (objects) and methods manipulating the data
• all functions are associated with objects
• almost all datatypes are objects (files, strings, etc.)
• potentially better code organization and reuse
Interpreted
•java compiler generate byte-codes
•the compiled byte-codes are platform-independent
•java bytecodes are translated on the fly to machine readable
instructions in runtime (Java Virtual Machine)
10/6/2025 21
Multithreaded
Java provides support for concurrent execution of multiple threads within a single
program.
•Thread Class:Java represents threads using the Thread class. Developers can
create threads by extending the Thread class or implementing the Runnable
interface.
• Concurrency and Parallelism:Java supports both concurrency and parallelism.
Concurrency refers to the ability of different parts of a program to execute
independently, while parallelism involves executing multiple threads
simultaneously.
• Thread Lifecycle:Java threads go through various states in their lifecycle,
including NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING,
and TERMINATED. Developers can control the execution of threads using
methods provided by the Thread class.
10/6/2025 22
Phases of a Java Program
§ Java programs normally go through five phases – edit, compile, load,
verify and execute
10/6/2025 23
10/6/2025 24
Phase 1: Creating a program
• Involves editing a file with an editor
• The following are some Java Integrated Development Environments
(IDEs).
– Jbuilder, NetBeans , Sun ONE Studio, Eclipse, jEdit, Jcreator, BlueJ,
jGRASP, etc.
– Most of this editors can be downloaded for free.
– Any text editor can also be used for creating Java programs.
– Programs written on IDEs or any other editors are known as Source
Code.
– Java source code file names end with .java extension
10/6/2025 25
Phase 2: Compiling Java Programs into Bytecodes
• Since a computer cannot understand a source program, program called a
compiler is used to translate the source program into a machine
language program called an object program.
• With Java, you write the program once, and compile the source program
into a special type of object code, known as bytecode.
• The bytecode can then run on any computer (platform) with a Java
Virtual Machine
10/6/2025 26
Continued…
• A virtual machine is a software application that
simulates a computer-JVM is one of the most widely used Java Bytecode
virtual machine Java Virtual
Machine
• Java Virtual Machine, which is part of JRE, is a software
Any
that interprets Java bytecode. Computer
• To compile Java Program
§ Javac [Link]
• To execute java programs
§ Java welcome
10/6/2025 27
Continued…
Runs on
Windows PC
Running JVM
Code Compile Result
Java Program Java Compiler Java Byte code UNIX
(*.java) javac (*.class) Running JVM
Macintosh
Running JVM
10/6/2025 28
Phase 3: Loading a program
• A program must be placed in memory before it can execute : a process
known as loading.
• The class loader takes the .class file (produced during compilation
process) containing the program’s bytecodes and transform them to
primary memory.
• The class loader also loads any of the .class files provided by java that
your program uses.
• The .class files can be loaded from a disk on your system or over a
network
10/6/2025 29
Phase 4 : Bytecode Verification
• Involves examining bytecodes to ensure that they are valid and do not
violate Java’s security restriction.
• Java enforces strong security, to make sure that Java programs arriving
over the network do not damage your files or your system (as computer
viruses and worms might)
10/6/2025 30
Phase 5: Execution
• The JVM executes the program’s bytecodes, thus performing the actions
specified by the program.
• .class file is not final machine code yet. It is virtual machine code. JVM
need a second compilation to convert virtual machine code to real
machine code. We call it just-in-time compiler (JIT).
• This greatly affects the speed of the execution.
• Traditionally, our C/C++ program can finish all the compilation before
execution. The generated machine code could run directly on OS or
hardware
10/6/2025 31
A Simple Java Program
• The following code is a java program that prints the string “ Welcome to Java”. Now
lets make a line by line investigation of the program
1. /* Your first Java program; [Link]
2. The program prints a string Welcome to Java Programming
3. */
4. public class Welcome {
5. //main method begins execution of Java application
6. public static void main (String args[])
7. {
8. [Link](“Welcome to Java”);
9. }//end of main method
10. } //end
10/6/2025 of class Welcome 32
Continued…
• Lines 1-3 & 5 comment lines
• There are two types of comments in Java
§ End-of-line (Single line) comments
oA line that begins with //
§ Traditional (Multiple line ) Comments
oA text that begins in /* ends in */
oAll text between these delimiters is comment so it is ignored by
the compiler.
oThese comment can be applied for a line, more than a lines or
part(s) of a line
10/6/2025 33
Continued…
§ Javadoc comments
oA text that begins in /** and ends in*/
oAs with traditional comments, all text between the Javadoc comment
delimiters is ignored by the compiler.
oJavadoc comments enable programmers to embed program
documentation directly in their programs.
10/6/2025 34
Continued…
• Line 4: public class Welcome {
§ This line begins class declaration of the class Welcome.
§ Every program in Java consists of at least one class declaration .
§ Generally, Java class declaration has the following format:
[Access level Specifier] class class_name { … }
10/6/2025 35
Continued….
• The class is the essential Java construct. A class is a template or blueprint for
objects.
• To program in Java, you must understand classes and be able to write and use
them.
• The mystery of the class will continue to be unveiled throughout this course.
For now, though, understand that a program is defined by using one or more
classes.
10/6/2025 36
Continued…
• Access level Specifier
§ Could be omitted (none), public, private, or protected
§ These are keywords that help to set the visibility and accessibility of a
class, its member variables, and methods.
§ They determine whether a field or method in a class, can be used or
invoked by another method in another class or sub-class.
10/6/2025 37
Continued…
• class
– The class keyword introduces a class declaration in Java and is
immediately followed by the class name (Welcome).
• class_Name
– Is the name of a class. A Java class name is an identifier.
– By convention class names should start with capital letters
– Java is case Sensitive
10/6/2025 38
Continued…
• Line 6: public static void main(String args[])
§ Is a method of which the starting point of every java application.
§ Every Java application must contain one main method.
§ public is a keyword which specifies access level of the method.
§ The JVM executes the application by invoking the main method
10/6/2025 39
Continued…
§ static is another keyword which allows main( ) to be called
without having to instantiate a particular instance of the class.
§ void is a return type that indicates that this method performs a task
but will not return any information when it completes its task.
§ String args[] … is an array of strings which is an argument of the
main function.
10/6/2025 40
Continued…
• Line 7: [Link]( "Welcome to Java Programming!" );
– This line prints the string contained in quotation.
– [Link] is known as the standard output object.
– When [Link] completes its task, it positions the output
cursor (the location where the next character will be displayed) to
the beginning of the next line while [Link] don’t.
• Every statement of java must end in semicolon (;) so is line 7
• Lines 8 and 9
– This line depicts the end of the main method and the class
welcome.
10/6/2025 41
End of part one!
Question?
10/6/2025 42