Chapter 1OOP
Chapter 1OOP
Chapter 1OOP
net/publication/353286457
CITATIONS READS
0 8,390
1 author:
Naol Getachew
Mattu University
27 PUBLICATIONS 0 CITATIONS
SEE PROFILE
All content following this page was uploaded by Naol Getachew on 16 July 2021.
BY: NAOL G. 1
II. Imperative programming paradigm
The word “imperative” comes from the Latin “impero” meaning “I command”. It’s the same word
we get “emperor” from, and that’s quite apt. You’re the emperor. You give the computer little
orders to do and it does them one at a time and reports back. The paradigm consists of several
statements, and after the execution of all of them, the result is stored. It’s about writing a list of
instructions to tell the computer what to do step by step.
In an imperative programming paradigm, the order of the steps is crucial, because a given step will
have different consequences depending on the current values of variables when the step is
executed.
Example in C:
#include <stdio.h>
int main()
{
int sum = 0;
sum += 1;
sum += 2;
printf("The sum is: %d\n", sum); //prints-> The sum is 55
return 0;
}
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
BY: NAOL G. 3
1.2.1. 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.
Here are the four main principles in more detail.
Abstraction:
Abstraction means using simple things to represent complexity. We all know how to
turn the TV on, but we don’t need to know how it works in order to enjoy it. In Java,
abstraction means simple things like objects, classes, and variables represent more
complex underlying code and data. This is important because it lets avoid repeating the
same work multiple times.
Encapsulation:
Encapsulation describes the ability of an object to hide its data and methods from the
rest of the world and is one of the fundamental principles of object-oriented
programming.
In Java, a class encapsulates the fields, which hold the state of an object, and the
methods, which define the actions of the object. Encapsulation enables you to write
reusable programs. It also enables you to restrict access only to those features of an
object that are declared public. All other fields and methods are private and can be used
for internal object processing.
Inheritance:
Inheritance is an important feature of object-oriented programming languages. It enables
classes to include properties of other classes. The class that inherits the properties is
called a child class or subclass, and the class from which the properties are inherited is
called a parent class or superclass. This feature also helps in reusing already defined
code.
Polymorphism:
Polymorphism is the ability for different objects to respond differently to the same
message. In object-oriented programming languages, you can define one or more
methods with the same name. These methods can perform different actions and return
different values.
One form of polymorphism in Java is method overloading. That’s when different
meanings are implied by the code itself. The other form is method overriding. That’s
when the different meanings are implied by the values of the supplied variables.
BY: NAOL G. 4
1.3. Overview of Java Programming and types of Java Program
The Java language provides certain key features that make it ideal for developing server
applications. These features include:
Object oriented
In Java, everything is an Object. Java can be easily extended since it is based on the Object
model.
Simplicity
Java is simpler than most other languages that are used to create server applications,
because of its consistent enforcement of the object model. The large, standard set of class
libraries brings powerful tools to Java developers on all platforms.
Portability
Java is portable across platforms. It is possible to write platform-dependent code in Java,
and it is also simple to write programs that move seamlessly across systems.
When Java source code compiled, the Java code gets converted to a standard, platform-
independent set of bytecodes, which are executed by a Java Virtual Machine (JVM).
Strong typing
Before you use a field, you must declare the type of the field. Strong typing in Java makes
it possible to provide a reasonable and safe solution to inter-language calls between Java
and PL/SQL applications, and to integrate Java and SQL calls within the same application.
Security
The design of Java bytecodes and JVM specification allow for built-in mechanisms to
verify the security of Java binary code. Oracle Database is installed with an instance of
Security Manager that, when combined with Oracle Database security, determines who can
call any Java methods.
There are two core types of java program namely: Java applications and 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. It need main() method to be
executed. A standalone application should be delivered and installed on each computer
before it is run. It can be either a Command-Line Interface (CLI) or Graphical User
Interface (GUI). You have already used a number of stand-alone applications such as text
editors, word processors and games applications.
BY: NAOL G. 5
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). No need of main()
method to run it. An applet can perform tasks and interact with users on their browser's
Webpages without using resources from the web server after being downloaded. Using
applets, you can do anything ranging from animations to complete games and utilities that
can be executed over the Internet.
BY: NAOL G. 6
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a
software development environment which is used to develop Java applications and applets.
It physically exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released by Oracle
Corporation:
o Standard Edition Java Platform
o Enterprise Edition Java Platform
o Micro Edition Java Platform
The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as
an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator
(Javadoc), etc. to complete the development of a Java Application.
class Test{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
BY: NAOL G. 7
We must save this program in a file called "Test.java" ensuring that the file name contains the class
name properly. This file is called the source file. Source file will always have the extension java.
If a program contains multiple classes, the file name must be the class name of the class containing
the main method.
Compile: In this step, the programmer gives the javac command and the .java files are converted
into bytecode which is the language understood by the Java virtual machine (and this is what makes
Java platform independent language).
Any compile time errors are raised at this step. To compile the above source code
(Test.java), we write the following command on ‘command prompt”, “java Test.java”.
Load: The program is then loaded into memory. This is done by the class loader which takes the
.class files containing the bytecode and stores it in the memory. The .class file can be loaded from
your hard disk or from the network as well. In our case “Test.class” file.
Verify: The bytecode verifier checks if the bytecode loaded are valid and do not breach java
security restrictions.
Execute: The JVM interprets the program one bytecode (in our case: Test.class) at a time and runs
the program. We write below command to run it, “java Test”:
BY: NAOL G. 8
1.6.Structure of Java Program
Using the Java programming language, we can develop a wide variety of applications. So, before
diving in depth, it is necessary to understand the basic structure of Java program in detail. Here
are the basic structure of a Java program.
Source: edureka.com
Documentation Section
It is used to improve the readability of the program. It consists of comments in Java which
include basic information such as the method’s usage or functionality to make it easier for
the programmer to understand it while reviewing or debugging the code.
A Java comment is not necessarily limited to a confined space, it can appear anywhere in
the code. The compiler ignores these comments during the time of execution and is solely
meant for improving the readability of the Java program.
There are three types of comments that Java supports
o Single line Comment: that start end on same line. Use “//”
o Multi-line Comment: that cover multiple of line start /* and end with */
o Documentation Comment: comment line in between of: /** and */
Package Statement
There is a provision in Java that allows you to declare your classes in a collection called
package. There can be only one package statement in a Java program and it has to be at the
beginning of the code before any class or interface declaration.
We use package keyword with package name to declare it. This statement is optional, for
example, take a look at the statement below.
o Example: package student;
BY: NAOL G. 9
Import Statement
Many predefined classes are stored in packages in Java, an import statement is used to refer
to the classes stored in other packages. An import statement is always written after the
package statement but it has to be before any class declaration. We use import keyword to
declare it.
o Example: import student.Test;
Interface Section
This section is used to specify an interface in Java. It is an optional section which is mainly
used to implement multiple inheritance in Java. An interface is a lot similar to a class in
Java but it contains only constants and method declarations.
Class Definition
A Java program may contain several class definitions, classes are an essential part of any
Java program. It defines the information about the user-defined classes in a program. A
class is a collection of variables and methods that operate on the fields. Every program in
Java will have at least one class with the main method.
Main Method Class
It is the most important section of your Java program. We define the main () method in this
section and without this Java program would not be able to execute. Execution of Java
program starts from main method. It is written inside a class. And inside main() method
we call methods and instantiate classes.
Let’s take a look at a sample program to understand how it is structured.
BY: NAOL G. 10
public static void main
o When the main method is declared public, it means that it can be used outside of
this class as well.
o The word static means that we want to access a method without making its objects.
As we call the main method without creating any objects.
o The word void indicates that it does not return any value. The main is declared as
void because it does not return any value.
o Main is the method, which is an essential part of any Java program.
String[] args: It is an array where each element is a string, which is named as args. If you
run the Java code through a console, you can pass the input parameter. The main() takes it
as an input.
System.out.println();
o The statement is used to print the output on the screen where the system is a
predefined class, out is an object of the PrintStream class. The method println prints
the text on the screen with a new line. All Java statements end with a semicolon.
BY: NAOL G. 11