Introduction To Object-Oriented Programming: Chapter - 1
Introduction To Object-Oriented Programming: Chapter - 1
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”;
paradigm based on the concept of "objects", which may contain data, in the
Car Object
• 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.
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.
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.
• 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).
• 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.
• 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.