0% found this document useful (0 votes)
2 views42 pages

CHAPTER ONE

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 42

Chapter 1:

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

• OOp, concepts in java is to improve code readability


and reusability by defining a Java program efficiently.
The main principles of object-oriented
programming are abstraction, encapsulation,
inheritance, and polymorphism. These concepts
aim to implement real-world entities in programs.

6
1.2.1. Packages

• The classes of the Java standard class library are


organized into packages
• A java package is a group of similar types of classes
and interfaces .
• Package in java can be categorized in two form,
built-in package and user-defined package.
• There are many built-in packages such as java, lang,
awt, javax, swing, net, io, util, sql etc.

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

Public class Student1{


int id;//data member (also instance variable)
String name;//data member(also instance variable)

public static void main(String args[]){


Student1 s1=new Student1();//creating an object of
Student
System.out.print(s1.id); Constructor:
we will see in next chapter
System.out.print(s1.name);
} }
Output:0 null
14
1.2.4. Class Members
• A class can have three kinds of members:

– fields: data variables which determine the status of


the class or an object

– methods: executable code of the class built from


statements. It allows us to manipulate/change the
status of an object or access the value of the data
member

– nested classes and nested interfaces


15
1.2.5. Encapsulation, Inheritance and
Polymorphism
• Encapsulation
– mechanism that binds together code and the data
it manipulates and keeps both safe from the
outside world.
• Inheritance
– process by which one object acquires the
property of another object.
• Polymorphism
– In Java, polymorphism refers to the fact that you can
have multiple methods with the same name in the same
class.
16
Con…
Abstraction
Abstraction is a process which displays only the
information needed and hides the unnecessary
information. We can say that the main purpose of
abstraction is data hiding. Abstraction means selecting
data from a large number of data to show the
information needed, which helps in reducing
programming complexity and efforts. 17
1.3. Why Java ?
The answer is that Java enables users to develop and
deploy applications on the Internet for servers,
desktop computers, and small hand-held devices.
The future of computing is being profoundly
influenced by the Internet, and Java promises to
remain a big part of that future. Java is the Internet
programming language.
Java is a general purpose programming language.

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…

• Java Run-time Environment (JRE): Java Run-time


Environment helps in running the
p r o g ra m s . J R E c o n t a i n s t h e J V M , t h e j a v a
classes/packages and the run-time libraries. If we
do not want to write programs, but only execute
the programs written by others, then JRE alone will
be sufficient. 22
Continue…

• Java Virtual Machine (JVM): Java Virtual Machine is


important part of the JRE, which actually runs the
programs (.classfiles), it uses the java class libraries
and the run-time libraries to execute those
programs. Every operating system(OS)
or platform will have a different JVM.
23
Continue…
• Just In Time Compiler (JIT): JIT is a module inside
the JVM which helps in compiling certain parts of byte
code into the machine code for higher performance.
Note that only certain parts of byte code will be
compiled to the machine code, the other parts are
usually interpreted and executed.
• Java is distributed in two packages - JDK and JRE.
When JDK is installed it also contains
the JRE, JVM and JIT apart from the compiler,
debugging tools. When JRE is installed it contains
the JVM and JIT and the class libraries.

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:

data type variable name

int total;

int count, temp, result;

Multiple variables can be created in


one declaration 25
Continue…

When the field modifier is required variable


declaration syntax is look like this
Field modifier data type variable name
types of modifiers: access control modifiers or
static

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)

{ // begin of the class

public static void main(String[] args) // main method

{ // begin of main method

// display a greeting and exit // comments

System.out.println("Hello, World!"); // method call

System.exit(0); // another method call,


// every statement must end with a semicolon

} // end of main method

} // end of the class 33


Decision-making statements Example:

public class check


{
public static void main(String[] args)
{
int x=20,y=60;
If(x>y){
System.out.println(“20>60”); }
Else{
System.out.println(“60>20”); }
}}
out put: 60>20 34
The looping statements Example:
public class loop
{
public static void main(String[] args)
{
for(int i=0;i<=10;i++)
{
System.out.print(i);
}
}
}
Output:0 1 2 3 4 5 6 7 8 9 10

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

• Normally, array is a collection of similar type of elements that have contiguous


memory location.

• 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

You might also like