Chapter One
Chapter One
By Minichil A.
Basics of Programming
3 A programming paradigm:
is a fundamental style of programming.
is an approach to solve problem using some programming language.
There are lots for programming language that are known but all of them need to follow
some strategy when they are implemented, and this methodology/strategy is paradigms
2
Types of Programming Paradigms
Procedural Programming:
Procedural programming is a programming paradigm based upon the concept of
procedure call.
In Procedural programming, programs are sequences of instructions to be executed.
They focus heavily on splitting up programs into named sets of instructions called
procedures.
Procedures, also known as routines, subroutines, methods, or functions simply contain
a series of computational steps to be carried out to solve a problem
Procedural programming language includes: Fortran, C, Pascal
3
Object Oriented Programming:
The program is written as a collection of classes and object which are meant for communication.
It depends upon objects (having both data and methods) that aims to incorporate the advantages
of modularity and reusability.
More emphasis is on data rather than procedure
Objects, which are usually instances of classes, are used to interact with one another to design
applications and computer programs.
Object-oriented programming takes the view that what we really care about the objects we want
to manipulate rather than the logic required to manipulate them.
4
Principles of Object Oriented Programming
Objects
01
Dynamic Binding Encapsulation
02
Classes OOP Data Abstraction
03
Inheritance
04 Polymorphism.
5
Cont…
Classes Is the structure and behavior (data and code) that will be shared by a set of objects
A class can be described as a blueprint that portrays the practices or expresses all the
variables.
Data Abstraction Data abstraction is one of the most essential and important features of object oriented
programming
Data Abstraction means displaying only essential information and hiding the details
Data abstraction refers to providing only essential information about the data to the outside
world, hiding the background details or implementation. 6
Cont.…
Objects
Customer Class
Solomon
Wolkite
Name
Birr 1234
Address
Attributes or Data
Current Balance Chala
Addis Ababa
Birr 2500
Insert Address ()
Methods Foziya
Update Balance () Adama
Birr 1752
Delete Address ()
7
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation
Is a programming mechanism that binds together code and the data it manipulates, and that
keeps both safe from outside interference and misuse.
If a field is declared private, it cannot be accessed by anyone outside the class, thereby
hiding the fields within the class.
Encapsulation also lead to data abstraction or hiding. As using encapsulation also hides the
data.
Class is a program-code-template that allows developers to create an object that has both
variables (data) and behaviors (functions or methods).
8
Cont…
Encapsulation
9
Inheritance
Inheritance is the process by which objects of one class can get the properties of objects of
another class.
Inheritance means one class of objects inherits the data and behaviors from another class.
Inheritance is the process of forming a new class from an existing class or base class
The new class that is formed is called derived class. Derived class is also known as a child
class or sub class
Inheritance helps in reducing the overall code size of the program, which is an important
concept in object-oriented programming
10
Cont..
Class: Academic
Name
Address
NoPeople()
Salary Marks
getSalary() Average()
11
Access modes can be classified into different categories:
Public: When the member is declared as public, it is accessible to all the functions of the program.
Private: When the member is declared as private, it is accessible within the class only.
Protected: When the member is declared as protected, it is accessible within its own class as well as
Types of Inheritances:
12
Single inheritance: is defined as the inheritance in which a derived class is inherited from
the only one base class.
Multilevel inheritance is a process of deriving a class from another derived class
Multiple inheritance is the process of deriving a new class that inherits the attributes from
two or more classes.
Hierarchical inheritance is defined as the process of deriving more than one class from a
base class
Hybrid inheritance is a combination of more than one type of inheritance
13
Single Inheritance Multilevel Inheritance Multiple Inheritance
14
Benefits of Inheritance:
One of the key benefits of inheritance is to minimize the amount of duplicate code in
an application by sharing common code amongst several subclasses
Reusability - facility to use public methods of base class without rewriting the same.
Extensibility - extending the base class logic as per business logic of the derived class.
Data hiding - base class can decide to keep some data private so that it cannot be
altered by the derived class
15
Polymorphism
Polymorphism is one of the OOPs feature that allows us to perform a single action in different
ways.
As the word suggests, ‘poly’ means ‘many’ and ‘morph’ points at ‘forms’; thus,
polymorphism would mean ‘a property of having many forms
It is the ability of an object to take many forms
Inheritance lets users inherit attributes and methods, and polymorphism uses these methods to
perform different tasks
In java, we use method overloading and method overriding to achieve polymorphism
Is a feature that allows one interface to be used for a general class of actions
Polymorphism plays a main role in allocate objects having different internal structures to share
the same external interface
16
Cont..
Shape
Calculate Area()
17
Dynamic Binding
Binding refers to the linking of a procedure call to the code to be executed in response to the call
Dynamic binding means that the code related with a given procedure call is not known until the time of
the call at run time.
Dynamic binding is associated polymorphism and inheritance.
In the section above, on polymorphism, it was stated that 'at runtime, objects of the class Circle would
invoke the Calculate Area method from the Circle class while objects of the class Square would invoke
the Calculate Area method from the Square sub-class’.
This idea is called dynamic binding or late binding. This means that the code associated with a given
procedure call is not known until the time of the call at runtime.
For the Calculate Area method, the code that is executed depends on the class of the object for which it is
called
18
Introduction to Java
History of JAVA
James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in
June 1991. The small team of Sun Engineers called “Green Team”.
Originally designed for small, embedded systems in electronic appliances like set- top boxes.
Firstly, it was called "Green talk" by James Gosling and file extension was “.gt”
After that, it was called Oak and was developed as a part of the Green project.
Finally, it come up with a name of Java with file extension “.java”
Java is an Object-Oriented, Multi-threaded programming language
Java enables users to develop and deploy applications on the internet for servers, desktop
computers, and small hand-held devices.
19
Features of Java
01 Object-Oriented 06 Multithreaded
08 Dynamic
03 Robust
10
05 Simple Secure
20
Object- Oriented :
objects that incorporate both data (Data Members) and behavior (Member Functions).
Java comes with an extensive set of classes; organize in packages that can be used in
program by inheritance.
22
Robustness
Java is robust (strong) because.
It uses strong memory management
Java provides automatic garbage collection which runs on the Java Virtual Machine to get
rid of objects which are not being used by a Java application anymore
Java tries to kill circumstances, which can lead to potential system failures, by stressing
chiefly on runtime checking and compile time checking
Java encourages error-free programming by being strictly typed and performing run time
checks
Java also includes the concept of exception handling, which keeps serious errors and reduces
all kind of threat of crashing the system
23
Distributed
24
Multithreaded :
26
Dynamic
27
Compiled and Interpreted
Java can be considered both a compiled and an interpreted language because its source code is first
compiled into a binary bytecode
This byte-code runs on the Java Virtual Machine (JVM), which is usually a software-based
interpreter
The Java interpreter converts the Java bytecode (.class file) into the code understand by the
operating system
Java interpreter is a computer program (system software) that implements the JVM. It is responsible
for reading and executing the program.
It is designed in such a way that it can read the source program and translate the source code
instruction by instruction. It converts the high-level program into assembly language (machine
language).
28
Secure
Java is secured because Java Programs run inside a virtual machine sandbox
Security is an important feature of Java and this is the strong reason that programmer use this language for
programming on internet
With Java’s security framework, it empowers to create frameworks, which are free of viruses and tampering.
Classloader in Java is a part of the Java Runtime Environment (JRE) which is used to load Java classes into
the Java Virtual Machine dynamically
It adds security by separating the package for the classes of the local file system from those that are imported
from network sources
29
Simple
Java is very easy to learn, and its syntax is simple (based on C++), clean, and easy to
understand.
Java has removed many complicated and rarely used features, for example, explicit pointers,
operator overloading, etc.
There is no need to remove unreferenced objects because there is an Automatic Garbage
Collection in Java.
Java has a concise, cohesive set of features that makes it easy to learn and use
Assuming that you have some programming experience, you will not find Java hard to
master
30
Java Basic Terminology
31
Java Development Kit (JDK):
It is a software development environment used for developing Java applications
JDK is a kit (or package) that includes two things:
Development Tools (provide an environment to develop your java programs)
JRE (to execute your java program)
JDK is the superset of the JRE and contains JRE with Java compiler, debugger, and core classes
Uses JVM, interpreter/loader, a compiler, an archiver, a documentation generator (Javadoc), and
set of APIs to complete the development of Java application
For the program to execute in java, we need to download and install JDK on our computer to
create, compile and run the java program
32
Java Development kit (JDK) :It includes
Javac(It is a Java Compiler)
Java(It is a java interpreter)
Javap(Java disassembler, which convert byte code into program description)
Javah(It is for java C header files)
Javadoc(It is for creating HTML document)
Jdb(It is Java debugger)
Applet viewer( It is used for viewing the applet)
33
Java Runtime Environment (JRE):
It is an installation package that provides an environment to only run (not develop) the
java program (or application) onto your machine
JRE is a part of JDK which means that JDK includes JRE
It contains a set of libraries and other files that JVM uses at runtime.
JRE includes a browser, JVM, applet supports, and plugins.
For running the java program, a computer needs JRE
34
Java Virtual Machine (JVM):
It is responsible for the program to execute line by line.
35
The Byte Code
Bytecode in Java is an intermediate machine-independent code
The bytecode is not processed by the processor rather it is processed by the Java Virtual
Machine (JVM).
It is a set of instructions for Java Virtual Machine.
The job of the JVM is to call all the required resources to compile the Java program and
make the bytecode independent
It is the biggest reason why java is known as a platform-independent language
At runtime Java virtual machine takes bytecode(.class) as an input and convert this into
machine(windows, Linux, macOS etc.) specific code for further execution.
36
Working of Java Bytecode:
JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. Machine Code
05
.java file Javac .class file
01 Source Code
02 Compiler 03 Output Code 04 Virtual Machines
37
Thank You!!