CHAPTER ONE
CHAPTER ONE
CHAPTER ONE
Introduction to Object-Oriented
Programming (OOP)
1
1.1 Overview of OOP?
Programming Paradigms
A programming paradigm is a fundamental style of
computer programming, a way of building the
structure and elements of computer programs.
Two paradigms of programming approach:
1 . “ W h a t i s h a p p e n i n g ? ” ( p ro c e s s o r i e n t e d
approach i.e. Procedural approach )
a programmer specifies a set of instructions to
perform a specific task in the form of procedures
or functions.
• C, Fortran etc.
2
Continue…
Data centric,
Become unmanageable when logic becomes
complex
No reuse functionality
2. “Who is being affected?”(object oriented
approach )
Object-Oriented Programming is a methodology
or paradigm to design a program using classes and
objects. It simplifies the software development and
maintenance by providing some concepts:
Like Object,Class,Inheritance…
3
Con..
• Object-oriented programming is a method used for
designing a program using classes and objects. Object-
oriented programming is also called the core of java.
• Object-oriented programming organizes a program
around objects and well-defined interfaces. This can also
be characterized as data controlling for accessing the
code.
• In this type of approach, programmers define the data
type of a data structure and the operations that are applied
to the data structure. This implies software development
and maintenance by using some of the concepts such as
Object , Class, Abstraction ,
Inheritance ,Polymorphism and Encapsulation 4
Continue…
object oriented paradigm came into consideration
to reuse structures and functions and perform
complex task with the help of abstraction.
• C++, Java
Procedural vs. Object-Oriented
Programming
• The unit in procedural programming is function, and
unit in object-oriented programming is class
• Procedural programming concentrates on creating
functions, while object-oriented programming starts
from isolating the classes, and then look for the methods
inside them. 5
1.2 Overview of OO principles
6
1.2.1. Packages
7
1.2.2.classes
• Class
“Class” refers to a blueprint. It defines the variables and
methods the objects support
– A class is a group of objects that has common
properties. It is a template or blueprint from which
objects are created.
A class is declared by use of the class keyword. The
classes that have been used up to this point are
actually very limited examples of its complete form.
Classes can (and usually do) get much more complex.
The general form of a class definition is shown here:
8
Con…
access specifiers class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}// ...
type methodnameN(parameter-list) {
// body of method
}} 9
Con..
Class Example:
Puplic class Employee {
// fields
String name;
double salary;
// a method
void pay () {
System.out.println("Pay to the order of " +
name + " $" + salary);
}}
10
Modifiers of the classes
• A class can also has modifiers
– public
• publicly accessible
• without this modifier, a class is only
accessible within its own package
– abstract
• no objects of abstract classes can be created
– final
• can not be subclasses
11
1.2.3.objects
• Objects
– An entity that has state and behavior is known as an
object e.g. chair, marker, pen, table, car etc. It can
be physical or logical (tengible and intengible). The
example of integible object is banking system.
– Object is an instance of a class. Class is a
template or blueprint from which objects are
created. So object is the instance(result) of a class.
Each object has a class which defines its data and
behavior
12
Continue…
• An object contains both data and methods that
manipulate that data
– The data represent the state of the object
– Data can also describe the relationships between
this object and other objects
• Example: A CheckingAccount might have
– A balance (the internal state of the account)
– An owner (some object representing a person)
• creating an object is instantiation
13
Simple Example of Object and Class
18
1.3.1. Characteristics of Java
• Java Is Simple
According to Sun, Java language is simple because:
syntax is based on C++ (so easier for programmers to
learn it after C++).
• Java Is Object-Oriented
Object-oriented means we organize our software as a
combination of different types of objects that
incorporates both data and behaviour.
• Java is Platform Independent
Java code is compiled by the compiler and converted
into bytecode.This bytecode is a platform independent
code because it can be run on multiple platforms i.e.
Write Once and Run Anywhere(WORA). 19
Continue...
• Java Is Robust
Robust simply means strong. Java uses strong memory
management.
• Java Is Secure
Java is secured because Programs run inside virtual
machine sandbox.
• Java is Distributed
We can create distributed applications in java. RMI and
EJB are used for creating distributed applications. We
may access files by calling the methods from any
machine on the internet. 20
1.3.2. The JVM and Byte Code
• Java Development Kit (JDK): Java Development Kit
contains two parts. One part contains the utilities like javac,
debugger, jar which helps in compiling the source code
(.java files) into byte code (.class files) and debug the
programs. The other part is the JRE, which contains the
utilities like java which help in running/executing the byte
code. If we want to write programs and run them, then we
need the JDK installed.
21
Continue…
24
1.4. Variables
• A variable is a name for a location in memory
• A variable must be declared by specifying the variable's
name and the type of information that it will hold
Variable declaration syntax:
int total;
26
Continue…
variable's name means the reserved memory
location and data type specify type of
information that store in the location.
There are different data types in java.
• Primitive Data Types:
boolean int
char long
float double
short
• Reference types (composite)
– classes
– arrays 27
Variable Initialization
A variable can be given an initial
value in the declaration.
Example:
int sum = 0;
int base = 32,
max = 149;
When a variable is referenced in a
program, its current value is used.
28
Assignment
• An assignment statement changes the value of a
variable.
• The assignment operator is the = sign
total = 55;
• The value that was in total is overwritten
29
Constants…
• A constant is an identifier that is similar
to a variable except that it holds the
same value during its entire existence.
• As the name implies, it is constant, not
variable.
• The compiler will issue an error if you
try to change the value of a constant.
• In Java, we use the final modifier to
declare a constant. Example
30
1.5. Operators
• Assignment: =, +=, -=, *=, …
• Numeric: +, -, *, /, %, ++, --, …
• Relational: ==. !=, <, >, <=, >=, …
• Boolean: &&, ||
31
1.6. Control Flow Statements
•Sequential Processing
-Execute instructions in order
• Decision-making statements
-Choose code to execute based on data value
It includes : if, if-else, switch
• The looping statements
-Repeat operations for multiple data values
It includes : for, while, do – while
• The branching statements
–It includes : break, continue, return
32
Sequential Processing Example:
public class Hello // public class ClassName (same as filename)
35
The branching statements Examples:
public class jumb
{
public static void main(String[] args)
{
int x=20,y=60;
for(int i=0;i<=10;i++)
{
if(i%2==0){
continue;}
System.out.print(i);
}}
}
Output: 1 3 5 7 9
36
1.7. Array
• Java array is an object the contains elements of similar data type. It is a data
structure where we store similar elements. We can store only fixed set of elements
in a java array.
• Array in java is index based, first element of the array is stored at 0 index.
37
Continue…
38
Types of Array in java
There are two types of array.
• Single Dimensional Array
• Multidimensional Array
Single Dimensional Array in java
• Syntax to Declare an Array in java
• dataType[] arr; (or)
• dataType arr[];
• Instantiation of an Array in java
• arrayRefVar=new datatype[size];
39
Let's see the simple example of one dimension java
array, where we are going to declare, instantiate,
initialize and traverse an array.
class Testarray{
public static void main(String args[]){
int a[]=new int[3];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
//printing array
for(int i=0;i<a.length;i++)//length is the property of arra
y
System.out.print(a[i]); }}
Output: 10 20 70 40
Multidimensional array in java
In such case, data is stored in row
and column based index (also known
as matrix form).
Syntax to Declare Multidimensional
Array in java
dataType[][] arrayRefVar; (or)
dataType arrayRefVar[][];
Example to instantiate
Multidimensional Array in java
int[][] arr=new int[2][2];//2 row an
d 2 column 41
Let's see the simple example to declare,
instantiate, initialize and print the 2Dimensional
array.
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" "); }
System.out.println(); } }}
Output: 1 2 3
245
445
42