Chapter 1OOP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/353286457

Chapter-1 Introduction to Object Oriented Programming

Presentation · July 2021


DOI: 10.13140/RG.2.2.12670.92487

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.

The user has requested enhancement of the downloaded file.


Chapter-1
Introduction to Object Oriented Programming
1.1.Types of programming paradigm
 What is programming paradigm?
o The term programming paradigm refers to a style of programming. It does not refer
to a specific language, but rather it refers to the way you program.

I. Declarative programming paradigm


Declarative programming is a style of building programs that expresses the logic of a computation
without talking about its control flow. Declarative programming is a programming paradigm in
which the programmer defines what needs to be accomplished by the program without defining
how it needs to be implemented. In other words, the approach focuses on what needs to be achieved
instead of instructing how to achieve it.
1. Functional programming paradigm
The functional programming paradigm has its roots in mathematics and it is language independent.
The key principle of this paradigm is the execution of a series of mathematical functions. You
compose your program of short functions. All code is within a function. All variables are scoped
to the function. In the functional programming paradigm, the functions do not modify any values
outside the scope of that function and the functions themselves are not affected by any values
outside their scope. Languages that support functional programming paradigm: JavaScript, Scala
and etc.
2. Database processing approach: This programming methodology is based on data and its
movement. Structured Query Language (SQL) is good example.

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;
}

a. Procedural programming paradigm


Procedural programming (which is also imperative) allows splitting those instructions into
procedures. Procedures aren't functions. The difference between them is that functions return a
value, and procedures do not. More specifically, functions are designed to have minimal side
effects, and always produce the same output when given the same input. Procedures, on the other
hand, do not have any return value. Their primary purpose is to accomplish a given task and cause
a desired side effect.
A great example of procedures would be the well-known for loop. The for loop's main purpose is
to cause side effects and it does not return a value.
Example in C: #include <stdio.h>
int main(){
int sum = 0;
int i =0;
for(i=1;i<11;i++){
sum += i;
}
printf("The sum is: %d\n", sum); //prints-> The sum is 55 BY: NAOL G. 2
return 0; }
Languages that support the procedural programming paradigm are: C, C++, Java, Pascal and etc.
b. Object-oriented programming paradigm
OOP is the most popular programming paradigm because of its unique advantages like the
modularity of the code and the ability to directly associate real-world business problems in terms
of code. The key characteristics of object-oriented programming include Class, Abstraction,
Encapsulation, Inheritance and Polymorphism.
Example in Java:

class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}

1.2. Overview of Object-Oriented principles


Java is a class-based object-oriented programming (OOP) language that is built around the concept
of objects. OOP concepts (OOP) intend to improve code readability and reusability by defining
how to structure a Java program efficiently. The main principles of object-oriented programming
are:
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism

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.

1.4.Java Development Environment


There are the important tools to develop a give java program.
JVM
 JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because
it doesn't physically exist. It is a specification that provides a runtime environment in which
Java bytecode can be executed. It can also run those programs which are written in other
languages and compiled to Java bytecode.
 JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are
platform dependent because the configuration of each OS is different from each other.
However, Java is platform independent. There are three notions of the JVM: specification,
implementation, and instance
JRE
 JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java
Runtime Environment is a set of software tools which are used for developing Java
applications. It is used to provide the runtime environment. It is the implementation of
JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.
 The implementation of JVM is also actively released by other companies besides Sun
Micro Systems.

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.

1.5.Implementation of Java Program


Java program execution follows 5 majors’ steps:
Edit: Here the programmer uses a simple editor or a notepad application to write the java program
and in the end give it a ".java" extension.

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.

public class Test{


//main method declaration
public static void main(String args[]){
System.out.println("Hello Java");
}
}
 public class Test: This creates a class called Test. You should make sure that the class
name starts with a capital letter, and the public word means it is accessible from any other
classes.
 Comment: To improve the readability, we can use comments to define a specific note or
functionality of methods, etc for the programmer.
 Braces: The curly brackets are used to group all the commands together. To make sure that
the commands belong to a class or a method.

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

View publication stats

You might also like