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

Introduction To Object-Oriented Programming: Chapter - 1

This document provides an introduction to object-oriented programming. It discusses different programming paradigms including imperative, functional, concurrent, logical and object-oriented programming. It defines object-oriented programming as a program modeled as a set of interacting objects. An object contains both data in the form of fields (states) and code in the form of procedures (behaviors). The principles of OOP including abstraction, encapsulation, inheritance and polymorphism are also introduced. Finally, the document provides an overview of the Java programming language as a popular object-oriented language.

Uploaded by

bahiru belina
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Introduction To Object-Oriented Programming: Chapter - 1

This document provides an introduction to object-oriented programming. It discusses different programming paradigms including imperative, functional, concurrent, logical and object-oriented programming. It defines object-oriented programming as a program modeled as a set of interacting objects. An object contains both data in the form of fields (states) and code in the form of procedures (behaviors). The principles of OOP including abstraction, encapsulation, inheritance and polymorphism are also introduced. Finally, the document provides an overview of the Java programming language as a popular object-oriented language.

Uploaded by

bahiru belina
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter -1-

Introduction to Object-Oriented Programming

By Chimdesa Asebe
Department of Computer Science Wollo
University,KIoT
Object Oriented Programming-CoSc 2082 1
Introduction

Programming Paradigms:
It is a paradying style of programming
It is different from programming languages because it is approach but
programming language is artificial computer language.
Types of Programming Paradigms
Imperative
Concurrent
Functional
Object oriented
 Logic
Object Oriented Programming-CoSc 2082 2
Cont..
i) Imperative Programming -writing source codes that is impressive.
focuses on how to execute, defines control flow as statements that change a
program state.
implements assignment statements, conditional and unconditional statements, and
looping.
Example 1: assignment statement Example 2 :conditional statement

int y; if(x=1)
y=5; cout<<x;
else
cout<<“Wrong”;

Object Oriented Programming-CoSc 2082 3


Cont…
ii) Functional Programming- a program will be treated as a collection of
mathematical functions.
Means that, programs considered as a collection of functions.
The main difference from Imperative programming is:
Do not have memory location
Do not have assignment.
iii) Logical programming- it works based on the normal premise
conclusion approach.
• is applicable in artificial propagation.
iv) Concurrent programming- provides execution of operations
concurrently.
Means that, define how to execute different tasks at the same time.

Object Oriented Programming-CoSc 2082 4


Cont..
v )Object oriented programming:
• Definition 1 :- Object oriented programming-is a program paradigms in which
a software system modeled as a set of objects that interacts each other objects
can be identified by their state-specify identity and behavior-specify action.
Means that, all programs treated as an object.

• Definition 2 :- Object-oriented programming (OOP) is a programming

paradigm based on the concept of "objects", which may contain data, in the

form of fields, often known as attributes(States); and code, in the form of

procedures, often known as methods(Behavior).

Object Oriented Programming-CoSc 2082 5


Object
Definition:- An object is a software bundle that has State and Behavior.
• An object is a building block which contains variables(States) and methods(behaviour).
Example 1, dogs have states (name, color, hungry, breed) and behaviors (bark, fetch, and wag
tail).
Example 2, car have states(current gear, number of gears, color, number of wheels) and
behaviors(braking, accelerating, slowing down, changing gears)
Example 3, a person is an object which has certain properties such as height, gender, age, etc. It
also has certain methods such as move, talk, and so on.
• In object-oriented programming, a computer program may be seen as a collection of individual
units, or objects, that act on each other.
• Each object is capable of receiving messages, processing data, and sending messages to other
objects.
Means, Software objects interact and communicate with each other by sending messages to each
other.
• Example: when you want your dog to gather a herd of goats, you whistle and send him out.
Object Oriented Programming-CoSc 2082 6
Cont..
A software object maintains its state in one or more variable and implements its behavior with
methods.

Object Oriented Programming-CoSc 2082 7


Cont…

• So our car object look like the following figure.

Car Object

Object Oriented Programming-CoSc 2082 8


Principles of OOP
• OOP has four basic concepts on which it is totally based.
1) Abstraction: It refers to, providing only essential information to the outside world and
hiding their background details.
For example, a web server hides how it processes data it receives, the end user just hits the
button and gets the data back.
Means that, ignoring some aspects and focusing on the essential only.
2) Encapsulation: Encapsulation is a process of binding data members (variables, properties)
and member functions (methods) into a single unit.
It is also a way of restricting access to certain properties or component. (hiding
data/information)
3) Inheritance: The ability to create a new class from an existing class is called inheritance. Using
inheritance, we can create a child class from a parent class such that it inherits the properties and
methods of the parent class and can have its own additional properties and methods.
For example, if we have a class Vehicle that has properties like Color, Price, etc, we can create 2 classes
like Bike and Car from it that have those 2 properties and additional properties that are specialized for
them like a Car has numberOfWindows while a Bike cannot. Same is applicable to methods.
Object Oriented Programming-CoSc 2082 9
Cont…
4) Polymorphism: The word polymorphism means having many forms. Typically,
polymorphism occurs when there is a hierarchy of classes and they are related by
inheritance. C++ polymorphism means that a call to a member function will cause a
different function to be executed depending on the type of object that invokes the
function.
Means, different objects or things can have same interface but respond in
accordance with their behavior Or ability of an object having different forms.

Interface-specifies how an object communicate with an other objects.

Object Oriented Programming-CoSc 2082 10


JAVA PROGRAMMING
Computer program? Computer programs, known as software, are instructions to the computer, telling it what
to do.
Computers do not understand human languages, so you need to use computer languages in computer
programs.
Programming? is the creation of a program that is executable by a computer and performs the required tasks.
Levels of Programming Language?
1. Low-level (machine language, Assembly Language)
2. High-level Language (Java, C++,C#, Python, COBOL, FORTRAN…)
Each of these languages was designed for a specific purpose

Java- is a powerful programming language.


 Java, developed by Sun Microsystems, is widely used for developing platform independent Internet
applications.
 Java is the preferred language for meeting many organizations’ enterprise programming needs.
 Java has also become the language of choice for implementing Internet-based applications and software for
devices that communicate over a network.
Object Oriented Programming-CoSc 2082 11
Characters of Java (Features of Java programming language
• Java is simple, object oriented, distributed, interpreted, robust, secure, portable, high
performance, multithreaded, and dynamic
• Java is a full-featured, general-purpose programming language that can be used to
develop robust mission-critical applications.
• Today, it is employed not only for web programming but also for developing
standalone applications across platforms on servers, desktop computers, and mobile
devices.
• Java is now very popular for developing applications on web servers. These
applications process data, perform computations, and generate dynamic webpages.
Many commercial Websites are developed using Java on the backend.
• Java is a versatile programming language: You can use it to develop applications for
desktop computers, servers, and small handheld devices.
Example, The software for Android cell phones is developed using Java.

Object Oriented Programming-CoSc 2082 12


Cont…

• Java is Simple: Because java is built from the ground up with modern programming
concepts in mind, those who have worked with other languages will notice a degree of
familiarity.
• Java is Object oriented Programming: fully object oriented.
• Java is Interpreted: java is originally compiled into a byte code which is not targeted at any
specific platform.

Object Oriented Programming-CoSc 2082 13


Cont…
Java is Portable: means java applications can practically run any where .
Java is Robust: java is strongly typed including memory management, garbage collection
and provides an exception handling mechanism.
Means critical applications.
Java is Secure: java’s memory management and compile time checking are the main
features that contribute to the security of java.

Object Oriented Programming-CoSc 2082 14


Advantages and Disadvantages of Java

Advantages
• Java is platform independent. Once it's compiled, you can run the bytecode on any
machine with a Java interpreter. You do not have to recompile for each platform.
• Java is safe. Certain common programming bugs and dangerous operations are prevented
by the language and compiler.
• Java standardizes many useful operations like managing network connections and
providing graphical user interfaces.
Disadvantages:
• Running bytecode through the interpreter is not as fast as running machine code, which is
specific to that platform.
• Because it is platform independent, it is difficult to use platform specific features (e.g.,
Windows taskbar, quick launch) in Java.
• Java interpreter must be installed on the computer in order to run Java programs.

Object Oriented Programming-CoSc 2082 15


JAVA TOOLS - The Java Language Specification, API, JDK, JRE, and IDE
Java syntax is defined in the Java language specification, and the Java library is defined in the
Java application program interface (API).
The JDK is the software for compiling and running Java programs.
An IDE is an integrated development environment for rapidly developing programs.
Computer languages have strict rules of usage. If you do not follow the rules when writing a
program, the computer will not be able to understand it.
 The Java language specification and the Java API define the Java standards.
The Java language specification is a technical definition of the Java programming language’s
syntax and semantics.
 The application program interface (API), also known as library, contains predefined classes
and interfaces for developing Java programs.

Object Oriented Programming-CoSc 2082 16


Cont...
•Java is a powerful language that can be used in many ways. It comes in three editions:
Java Standard Edition (Java SE) ->to develop client-side applications. The applications can run on
desktop.
Java Enterprise Edition (Java EE) ->to develop server-side applications, such as Java servlets,
JavaServer Pages (JSP), and JavaServer Faces (JSF).
Java Micro Edition (Java ME) -> to develop applications for mobile devices, such as cell phones.
•In this section we uses Java SE to introduce Java programming.
•Java SE is the foundation upon which all other Java technology is based.
•There are many versions of Java SE. The latest, Java SE 8, is used in this section. Oracle releases each
version with a Java Development Toolkit (JDK).
•For Java SE 8, the Java Development Toolkit is called JDK 1.8 (also known as Java 8 or JDK 8).
•The JDK consists of a set of separate programs, each invoked from a command line, for compiling,
running, and testing Java programs.
•JDK comes with a lot of other powerful tools such as jar, jhat, jdb, javap, javadoc, jcmd, javah, keytool,
jconsole, jps, jstatd, jstat, jinfo, jmap, jstack and many more to enable Java developers to program better.
Object Oriented Programming-CoSc 2082 17
Cont…

The program for running Java programs is known as JRE (Java Runtime Environment).
Instead of using the JDK, you can use a Java development tool (e.g., NetBeans, Eclipse,
and TextPad)—software that provides an integrated development environment (IDE) for
developing Java programs quickly. Editing, compiling, building, debugging, and online
help are integrated in one graphical user interface.
Java Virtual Machine (JVM), Rather than a physical machine, the virtual machine is a
program that interprets Java bytecode.
This is one of Java’s primary advantages: Java bytecode can run on a variety of hardware
platforms and operating systems. Java source code is compiled into Java bytecode, and
java bytecode is interpreted by the JVM. Your Java code may use the code in the Java
library. The JVM executes your code along with the code in the library.

Object Oriented Programming-CoSc 2082 18


Typical Java Development Environment

How to Creating, Compiling, and Executing a Java Program??


You have to create your program and compile it before it can be executed. This
process is repetitive.
If your program has compilation errors, you have to modify the program to fix
them, then recompile it.
If your program has runtime errors or does not produce the correct result, you
have to modify the program, recompile it, and execute it again.

• Java programs normally go through five phases—edit, compile, load, verify


and execute.

Object Oriented Programming-CoSc 2082 19


Cont…

Object Oriented Programming-CoSc 2082 20


Cont…
Phase 1: Creating a Program
• Phase 1 consists of editing a file with an editor program, normally known simply as an
editor.
• You type a Java program (typically referred to as 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.
IDEs provide tools that support the software development process, including editors for
writing and editing programs and debuggers for locating logic errors—errors that cause
programs to execute incorrectly. Popular IDEs include Eclipse and NetBeans.

Object Oriented Programming-CoSc 2082 21


Phase 2: Compiling a Java Program into Bytecodes

• In Phase 2, you use the command javac (the Java compiler) to compile a program.
For example, to compile a program called Welcome.java, you’d type
javac Welcome.java
• If the program compiles, the compiler produces a .class file called Welcome.class that
contains the compiled version of the program.
• The Java compiler translates Java source code into bytecodes that represent the tasks to
execute in the execution phase (Phase 5). Bytecodes are executed by the Java Virtual
Machine (JVM)—a part of the JDK and the foundation of the Java platform.
• Java’s bytecodes are portable—without recompiling the source code, the same byte- codes
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 Welcome, you’d type the command java Welcome in a command window to invoke
the JVM, which would then initiate the steps necessary to execute the application. This
begins Phase 3.
Object Oriented Programming-CoSc 2082 22
Phase 3: Loading a Program into Memory

• In Phase 3, the JVM places the program in memory to execute it—this is known as
loading.
• The JVM’s class loader takes the .class files containing the program’s bytecodes
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).
Phase 4: Bytecode Verification
In Phase 4, as the classes are loaded, the bytecode verifier examines their bytecodes
to ensure that they’re 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).

Object Oriented Programming-CoSc 2082 23


Phase 5: Execution

• In Phase 5, the JVM executes the program’s bytecodes, thus performing the actions
specified by the program.
• In early Java versions, the JVM was simply an interpreter for Java bytecodes. This caused
most Java programs to execute slowly, because the JVM would interpret and execute one
bytecode at a time.
• Some modern computer architectures can execute several instructions in parallel. Today’s
JVMs typically execute bytecodes using a combination of interpretation and so-called
just-in-time (JIT) compilation. In this process, the JVM analyzes the bytecodes as
they’re interpreted, searching for hot spots— parts of the bytecodes that execute
frequently. For these parts, a just-in-time (JIT) compiler—known as the Java HotSpot
compiler—translates the bytecodes into the underlying computer’s machine language.

Object Oriented Programming-CoSc 2082 24


Simple Java APPLICATION Code
• This section introduces Java application programming. We begin with examples of
programs.
• You can use any text editor or IDE to create and edit a Java source-code file. This section
demonstrates how to create, compile, and run Java programs from a command window.
• From the command window, you can use the NotePad to create the Java source code file,
as shown in Figure below.

Object Oriented Programming-CoSc 2082 25


Cont…
Caution:
• The source file must end with the extension .java and must have exactly the same name as the
public class name.
• For example, the file for the source code in previous example should be named Welcome.java,
since the public class name is Welcome.
• A Java compiler translates a Java source file into a Java bytecode file. The following command
compiles Welcome.java:
javac Welcome.java
• If there are no syntax errors, the compiler generates a bytecode file with a .class extension.
• So the preceding command generates a file named Welcome. class, as shown in Figure (a) below.
• The Java language is a high-level language while Java bytecode is a low-level language.
• The bytecode is similar to machine instructions but is architecture neutral and can run on any
platform that has a Java Virtual Machine (JVM), as shown in Figure (b) below.
• Rather than a physical machine, the virtual machine is a program that interprets Java bytecode.
• This is one of Java’s primary advantages: Java bytecode can run on a variety of hardware
platforms and operating systems. Object Oriented Programming-CoSc 2082 26
Cont…
• Figure (a) Java source code is translated into bytecode. (b) Java bytecode can be executed
on any computer with a Java Virtual Machine.

Object Oriented Programming-CoSc 2082 27


Cont..
• Do not use the extension .class in the command line when executing the program.
Use java ClassName to run the program. If you use java ClassName.class in the
command line, the system will attempt to fetch ClassName.class.class.

Object Oriented Programming-CoSc 2082 28


Cont…

• If you execute a class file that does not exist, a NoClassDefFoundError will
occur. If you execute a class file that does not have a main method or you
mistype the main method (e.g., by typing Main instead of main), a
NoSuchMethodError will occur.

Object Oriented Programming-CoSc 2082 29

You might also like