Object oriented programming Chapter 01

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Mettu University

Department of Information Technology

Object-Oriented Programming(OOP) in Java

Chapter One:
Introduction to Object-Oriented Programming (OOP)

12/06/24 Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Outlines
 Overview of Computer Programming
 Overview of OO Principles
 Java Programming Language
 The Features of Java Technology
 Java Development Environment
 Editing, Compiling and Interpreting.

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Overview of Computer Programming
 Computer programming (programming or coding) is the process of
writing, testing, debugging/troubleshooting, and maintaining the source code
of computer programs.
 A programming language is an artificial language that can be used to control
the behavior of a machine, particularly a computer.

Characteristics of a Programming Language:


 Every language has syntax and semantics:
 Syntax: is form of its declarations, expressions, statements and program units.
 Semantic: is the meaning of its program.
 A main purpose of programming languages is to provide instructions to a
computer. 12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Programming Paradigm
 Programming languages are characterized by Programming Paradigm.
 A programming paradigm is a style or "way" of programming.
 There are Object-oriented programming and procedural programming
paradigms. Both of them are high-level programming languages. These two
are important concepts, and it is also important to know the difference
between them.
 procedural programming is defined as a programming language derived
from the structure programming and based on calling procedures.
 The procedures are the functions, routines, or subroutines that consist of the
computational steps required to be carried.
 It follows a step-by-step approach in order to break down a task into a set of
variables and routines via a sequence of instructions.
Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com 12/06/24
Cont.…
 Examples of Procedural programming include C, Fortran, Pascal, and VB.
 Object-oriented programming is a computer programming design
philosophy or methodology that organizes/ models software design around
data or objects rather than functions and logic.
 A new features and new way of thinking about the process of decomposing
problems and developing programming solutions.
 Computation is performed by message passing: objects communicate with
one another via message passing
 The examples of object-oriented programming are - .NET, C#, Python, Java,
VB.NET, and C++.
 Read in detail the difference between procedural and Object-oriented
12/06/24
programming
Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com
Overview of OO principles
 An object is a software bundle of related state and behavior.
 Real-world objects share two characteristics: They all have state and behavior.
 E.g. Dogs have state (name, color, breed) and behavior (barking, wagging
tail).
 Identifying the state and behavior for real-world objects is a great way to begin
thinking in terms of OOP.
 An object stores its state in fields (variables in some programming languages)
and exposes its behavior through methods (functions in some programming
languages).
 Methods operate on an object's internal state and serve as the primary
mechanism for object-to-object communication.
12/06/24

 A class is the blueprint from which individual objects are created.


Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com
Overview of OOP principles

Other Four Principles of Object Oriented Programming


 Abstraction
 Encapsulation
 Inheritance
 Polymorphism

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Data abstraction
 Any representation of data in which the implementation details are hidden
(abstracted).
 Abstraction means to focus on the essential features of an element or object.
 Data abstraction is the development of classes, objects, types in terms of their
interfaces and functionality, instead of their implementation details.
 Your abstraction should include the responsibilities, the attributes, and the
methods of interest to your application—and ignore the rest.
 That is why the abstraction of a student would include the person's name and
address, but probably not his or her height and weight.
 Abstraction is used to manage complexity.

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Encapsulation
 Encapsulation is the process of binding both attributes and methods
together within a class.
 Encapsulation is simply the combination of data members &
process(functions) into a single entity called an object.
 Also Encapsulation is the hiding of data implementation by restricting
access.
 i.e hiding data and methods within an Object.
 Encapsulation provides the security that keeps data and methods safe from
inadvertent changes.

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Inheritance
 An important feature of OOP is inheritance and its main advantage is for
code reusability.
 Inheritance the ability to create classes that share the attributes and
methods of existing classes, but with more specific features.
 Subclasses inherit attributes and methods from the super classes above them
 The original class is the parent or base class.
 The new class is the child or derived class.
 The child class receives the attributes and methods of the parent class.

Student

Regular Extension
12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Types of Inheritance
 Single Inheritance − A subclass derives from a single super-class.
 Multiple Inheritance − A subclass derives from more than one super-classes
 Multilevel Inheritance − A subclass derives from a super-class which in
turn is derived from another class and so on.
 Hierarchical Inheritance − A class has a number of subclasses each of
which may have subsequent subclasses, continuing for a number of levels, so
as to form a tree structure.
 Hybrid Inheritance − A combination of multiple and multilevel inheritance
so as to form a lattice structure..

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Types of Inheritance

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Polymorphism
 Polymorphism is originally a Greek word that means the ability to take
multiple forms.
 Polymorphism definition is that Poly means many and morphos means forms.
 Polymorphism means one name, many forms.
 It describes the feature of languages that allows the same word or symbol to be
interpreted correctly in different situations based on the context.
 Let us consider two classes, Circle and Square, each with a method findArea().
 Though the name and purpose of the methods in the classes are same, the
internal implementation, i.e., the procedure of calculating area is different for
each class. When an object of class Circle invokes its findArea() method, the
operation finds the area of the circle without any conflict with the findArea()
method of the Square class.
12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Java Programming Language
 Java is a computer programming language that is concurrent, class-based,
object-oriented, and specifically designed to have as few implementation
dependencies as possible.
 Java is guaranteed to be Write Once, Run Anywhere (WORA)
 Java runs on a variety of platforms, such as Windows, Mac OS, and the
various versions of UNIX.
 Java is now used to develop large-scale enterprise applications, to
enhance the functionality of web servers, to provide applications for
consumer devices (e.g., cell phones, pagers and PDAs) and for many other
purposes
 The History of Java….
12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


The Features of Java Technology(why java?)

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


why java?
 Java is Object Oriented
 In Java, everything is an Object.
 Java can be easily extended since it is based on the Object model.
 The OOP paradigm has risen in popularity and has become the factor standard for
today’s software dev’t.
 Java is Simple
 Java is designed to be easy to learn. If you understand the basic concept of OOP, Java
would be easy to master.
 Platform independent
 Java is compiled into platform independent byte code.
 Java is Interpreted
 Java source code is passed to a compiler that generates the byte code.
 JVM interprets the byte code at runtime and executes it.
 Java is portable
 Java applications can run practically anywhere.
 You can truly write the code once and run it anywhere.

 Java is Distributed
 Java is designed for the distributed environment of the internet.

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


why java?
 Java is Secure
 Because Java technology is so prevalent on networks and in today’s enterprise
systems, it security aspects as vital.
 With Java's secure feature, it enables to develop virus-free, tamper-free systems.

 Java is Multithreaded
 With Java's multithreaded feature, it is possible to write programs that can do many
tasks simultaneously.
 This design feature allows developers to construct smoothly running interactive
applications.
 Java is High Performance
 Java versions have performed at the speed that developers demand.
 With the use of Just-In-Time (JIT) compilers, Java enables high performance.

 Java is Robust
 Robust code is reliable code.
 Java is considered a strongly typed language.
 JVM insures robustness managing all memory automatically.
 Java also includes an extensible mechanism for exception handling.

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Types of Java Program
 Different ways to write/run a Java codes are:
Application:
A stand-alone program that can be invoked from command line .
A program that has a “main”
main method
Applet:
A program embedded in a web page , to be run when the page is
browsed.
A program that contains no “main” method
 Application -Java interpreter
 Applets- Java enabled web browser (Linked to HTML via <APPLET>
tag. in html file)

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Typical Java Development Environment
 Java programs normally go through five phases-edit, compile, load, verify and execute.

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Phase 1: Creating a Program
 Phase 1 consists of editing a file with an editor program ( eg. Notepad).

 You type a Java program (source code) using the editor, make any necessary

corrections and save the program on a secondary storage device, such as your
hard drive.

 A file name ending with the .java extension indicates that the file contains Java

source code.

 For organizations that develop substantial information systems, Integrated

Development Environments (IDEs) are available from many major software


suppliers, including Sun Microsystems.

 IDEs provide tools that support the software development process, including

editors for writing and editing programs and debuggers


12/06/24for locating logic errors.

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Phase 2: Compiling a Java Program into Bytecodes
 In Phase 2, the programmer uses the command javac (the Java compiler) to

compile a program.

 For example, to compile a program called Welcome.java, you would type

javac Welcome1.java

in the command window of your system.

 If the program compiles, the compiler produces a .class file called

Welcome1.class that contains the compiled version of the program.

 The Java compiler translates Java source code into byte codes.

 Byte codes are executed by the Java Virtual Machine (JVM)

 JVM a part of the JDK and the foundation of the Java platform.

 Byte codes are platform-independent instructions.


12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Cont…

 So Java’s bytecodes are portable—that is, the same bytecodes can execute

on any platform containing a JVM that understands the version of Java in


which the bytecodes were compiled.
 The JVM is invoked by the java command.

 For example, to execute a Java application called Welcome1, you would

type the command

Java Welcome1
 in a command window to invoke the JVM, which would then initiate the

steps necessary to execute the application.

This begins Phase 3.


12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Phase 3: Loading a Program into Memory
 In Phase 3, the program must be placed in memory before it can execute

—known as loading.

 The class loader takes the .class files containing the program’s byte

codes and transfers 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 (e.g., your local college or company network, or the Internet).

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Phase 4: Bytecode Verification
 In Phase 4, as the classes are loaded, the byte code verifier examines

their byte codes to ensure that they are valid and do not violate
Java’s security restrictions.

 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).

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Phase 5: Execution
 In Phase 5, the JVM executes the program’s byte codes, thus performing the actions
specified by the program.
 JVMs typically execute byte codes using a combination of interpretation and so-called
just-in-time (JIT) compilation.
 In this process, The JVM analyzes the byte codes as they are interpreted, searching for
hot spots— parts of the byte codes that execute frequently.
 For these parts, a just-in-time (JIT) compiler— known as the Java HotSpot
compiler—translates the byte codes into the underlying computer’s machine language.
 When the JVM encounters these compiled parts again, the faster machine-language
code executes.
 Thus Java programs actually go through two compilation phases:

 source code is translated into byte codes (for portability across JVMs on different
computer platforms) and
 byte codes are translated into machine language for the actual computer on which the
program executes.
12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


How Java Works?

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Editing, Compiling and Interpreting Java program
 The easiest way to write a simple program is with a text editor (eg. Notepad, Netbeans, jEdit,…).

 The Java compiler translates Java source code into a special representation called byte code

 Java byte code is not the machine language for any traditional CPU.

 Another software tool, called an interpreter translates byte code into machine language and
executes it.
 The JVM is a software layer that provides translation between Java byte codes and the native
operating system.

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


Writing a Java Program

 A First Program in Java: Printing a Line of Text

public class Welcome{

public static void main( String args[] ){

System.out.println( "Welcome to Java Programming


World!" );

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com


n e
r O
p t e
h a
C !
of o u !
E nd k Y
h e a n
T Th

12/06/24

Prepared by: Fikadu M. Email:fikadu.meu.edu@gmail.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy