OBJECT ORIENTED TECHNOLOGY AND JAVA
PROGRAMMING (MCS-024)
BLOCK-1: OBJECT ORIENTED TECHNOLOGY AND JAVA
SUBODH KUMAR
B.Tech, M.Tech – Computer Science & Engineering
Masters in Clinical Psychology
Ph.D. (Pursuing)
SUBODH KUMAR 1
UNIT-1: OBJECT ORIENTED METHODOLOGY
Paradigms of Programming Languages
Evolution of OO Methodology
Basic Concepts of OO Approach
Comparison of Object Oriented and Procedure Oriented Approaches
Benefits of OOPs
Introduction to Common OO Language
Applications of OOPs
SUBODH KUMAR 2
PARADIGMS OF PROGRAMMING LANGUAGES
The term paradigm describes a set of techniques, methods, theories and standards that
together represent a way of thinking for problem-solving.
SUBODH KUMAR 3
PARADIGMS OF PROGRAMMING LANGUAGES
SUBODH KUMAR 4
PARADIGMS OF PROGRAMMING LANGUAGES
Imperative paradigm: describe the details of how the results are to be obtained, in terms of the
underlying machine model.
The program starts from an initial state, goes through the transitions and reaches a final state.
Within this paradigm we have the procedural approach and Object Oriented approach
Procedural paradigm: Procedural languages are statement oriented with the variables holding values.
The popular programming languages in this category are Ada, Fortran, Basic, Algol, Pascal, Cobol, Modula, C,
etc.
Object Oriented paradigm: The Object Oriented paradigm is centered on the concept of the object.
Everything is focused on objects. The popular programming languages in this paradigm are C++, Simula,
Smalltalk and Java.
SUBODH KUMAR 5
PARADIGMS OF PROGRAMMING LANGUAGES
Declarative paradigm: In this paradigm programs declare or specify what is to be
computed without specifying how it is to be achieved. Declarative programming is also
known as Value-oriented programming.
Functional paradigm: In this paradigm, a program consists of a collection of functions.
The main programming languages in this category are Lisp, ML, Scheme, and Haskell.
Logic paradigm: In this paradigm programs only explain what is to be computed not how
to compute it. The Prolog language is perhaps the most common example. Mercury
language is a more modern attempt at creating a logic programming language.
SUBODH KUMAR 6
EVOLUTION OF OO METHODOLOGY
Machine language - 0 and 1.
Assembly language - pneumonic for various instructions to write
programs
Structured programming: the concept of functions (or subroutines,
procedures, subprogram)
Procedural languages - statement oriented with the variables holding
values
The basic idea behind OOP is to combine both, data and its
functions that operate on the data into a single unit called object.
Object means a real-world entity such as a pen, chair, table, computer, watch,
etc. Object-oriented programming is a methodology or paradigm to design a
program using classes and objects.
SUBODH KUMAR 7
BASIC CONCEPTS OF OO APPROACH
SUBODH KUMAR 8
OBJECT
Any entity that has state and behavior is known as an object.
For example, a chair, pen, table, keyboard, bike, etc. It can be physical
or logical.
An Object can be defined as an instance of a class. An object contains
an address and takes up some space in memory. Objects can
communicate without knowing the details of each other's data or
code. The only necessary thing is the type of message accepted and
the type of response returned by the objects.
Example: A dog is an object because it has states like color, name,
breed, etc. as well as behaviors like wagging the tail, barking, eating,
etc.
SUBODH KUMAR 9
CLASS
Collection of objects is called class.
It is a logical entity.
A class can also be defined as a
blueprint from which you can create an
individual object.
Class doesn't consume any space.
SUBODH KUMAR 10
INHERITANCE
When one object acquires all the properties and
behaviors of a parent object, it is known as
inheritance.
It provides code reusability.
It is used to achieve runtime polymorphism.
SUBODH KUMAR 11
POLYMORPHISM
If one task is performed in different ways, it is known as
polymorphism. For example: to convince the customer
differently, to draw something, for example, shape,
triangle, rectangle, etc.
In Java, we use method overloading and method
overriding to achieve polymorphism.
Another example can be to speak something; for example,
a cat speaks meow, dog barks woof, etc.
SUBODH KUMAR 12
ABSTRACTION
Hiding internal details and showing
functionality is known as abstraction.
For example phone call, we don't know
the internal processing.
In Java, we use abstract class and
interface to achieve abstraction.
SUBODH KUMAR 13
ENCAPSULATION
Binding (or wrapping) code and data together into a single unit
are known as encapsulation.
For example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation.
Java bean is the fully encapsulated class because all the data
members are private here.
SUBODH KUMAR 14
COMPARISON OF OBJECT ORIENTED AND PROCEDURE ORIENTED
APPROACHES / BENEFITS OF OOPS
OOPs makes development and maintenance easier, whereas, in a procedure-oriented
programming language, it is not easy to manage if code grows as project size
increases.
OOPs provides data hiding, whereas, in a procedure-oriented programming language,
global data can be accessed from anywhere.
OOPs provides the ability to simulate real-world event much more effectively.
We can provide the solution of real word problem if we are using the Object-
Oriented Programming language.
SUBODH KUMAR 15
COMPARISON OF OBJECT ORIENTED AND PROCEDURE ORIENTED
APPROACHES / BENEFITS OF OOPS
Figure: Data Representation in Procedure-Oriented
Programming
Figure: Data Representation in Object-Oriented Programming
SUBODH KUMAR 16
INTRODUCTION TO COMMON OO LANGUAGE
Object-based programming languages
Object Oriented programming languages
SUBODH KUMAR 17
OBJECT-BASED PROGRAMMING
Object-based programming is the style of programming that primarily supports
encapsulation and object identity.
Major features that are required for object-based programming are:
Data encapsulation
Data hiding and access mechanisms
Automatic initialization and clear-up objects
Polymorphism
SUBODH KUMAR 18
OBJECT ORIENTED PROGRAMMING
Object Oriented programming incorporates all of object-based programming
features along with additional features such as inheritance and dynamic binding.
Examples of Object Oriented languages are C++, Smalltalk, object-Pascal, and Java
SUBODH KUMAR 19
APPLICATIONS OF OOPS
Object Oriented databases
Embedded systems
Simulation and modeling
Neural networks
Decision support systems
Office automation systems
AI and expert systems
CAD/CAM systems (Computer-aided design/Computer-aided manufacturing)
Internet solutions
SUBODH KUMAR 20
SUBODH KUMAR 21
UNIT-2: OBJECT ORIENTED METHODOLOGY
Classes and Objects
Abstraction and Encapsulation
Inheritance
Method Overriding and Polymorphism
SUBODH KUMAR 22
OBJECT
Any entity that has state and behavior is known as an object.
For example, a chair, pen, table, keyboard, bike, etc. It can be physical
or logical.
An Object can be defined as an instance of a class. An object contains
an address and takes up some space in memory. Objects can
communicate without knowing the details of each other's data or
code. The only necessary thing is the type of message accepted and
the type of response returned by the objects.
Example: A dog is an object because it has states like color, name,
breed, etc. as well as behaviors like wagging the tail, barking, eating,
etc.
SUBODH KUMAR 23
OBJECT
Object Definitions:
• An object is a real-world entity.
• An object is a runtime entity.
• The object is an entity which has state and behavior.
• The object is an instance of a class.
SUBODH KUMAR 24
CLASS
A class is a group of objects which have
common properties.
It is a template or blueprint from which
objects are created.
It is a logical entity. It can't be physical.
A class in Java can contain: Fields,
Methods, Constructors, Blocks, Nested
class and interface
SUBODH KUMAR 25
CLASS
Syntax to declare a class:
class <class_name>
{
field;
method;
}
SUBODH KUMAR 26
ABSTRACTION
Abstraction is a process of hiding the implementation details and showing only functionality
to the user.
Another way, it shows only essential things to the user and hides the internal details, for
example, sending SMS where you type the text and send the message. You don't know the
internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Ways to achieve Abstraction
1. Abstract class (0 to 100%)
2. Interface (100%)
SUBODH KUMAR 27
ABSTRACT CLASS IN JAVA
A class which is declared as abstract is known as an abstract class.
It can have abstract and non-abstract methods. It needs to be extended and its method
implemented. It cannot be instantiated.
Points to Remember
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to change the body of the method.
SUBODH KUMAR 28
ENCAPSULATION
Binding (or wrapping) code and data
together into a single unit are known as
encapsulation.
For example, a capsule, it is wrapped with
different medicines.
A java class is the example of encapsulation.
Java bean is the fully encapsulated class
because all the data members are private
here.
SUBODH KUMAR 29
INHERITANCE
Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).
The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of the parent class. Moreover, you can add new methods and
fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
SUBODH KUMAR 30
WHY USE INHERITANCE IN JAVA
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
SUBODH KUMAR 31
TERMS USED IN INHERITANCE
• Class: A class is a group of objects which have common properties. It is a template or
blueprint from which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
• Reusability: As the name specifies, reusability is a mechanism which facilitates you to
reuse the fields and methods of the existing class when you create a new class. You
can use the same fields and methods already defined in the previous class.
SUBODH KUMAR 32
INHERITANCE
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
SUBODH KUMAR 33
TYPES OF INHERITANCE
SUBODH KUMAR 34
TYPES OF INHERITANCE
SUBODH KUMAR 35
METHOD OVERRIDING
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that
has been declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
• Method overriding is used to provide the specific implementation of a method which
is already provided by its superclass.
• Method overriding is used for runtime polymorphism
SUBODH KUMAR 36
POLYMORPHISM
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means
many and "morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism.
We can perform polymorphism in java by method overloading and method overriding.
If you overload a static method in Java, it is the example of compile time polymorphism.
SUBODH KUMAR 37
SUBODH KUMAR 38
UNIT-3: JAVA LANGUAGE BASICS
Introduction To Java
Basic Features
Java Virtual Machine Concepts
A Simple Java Program
Primitive Data Type And Variables
Java Keywords
Integer and Floating Point Data Type
Character and Boolean Types
Declaring and Initialization Variables
Java Operators
SUBODH KUMAR 39
INTRODUCTION TO JAVA
Java is a programming language and a platform.
Java is a high level, robust, object-oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in
the year 1995.
James Gosling is known as the father of Java.
Before Java, its name was Oak. Since Oak was already a registered company, so
James Gosling and his team changed the name from Oak to Java.
Platform: Any hardware or software environment in which a program runs, is known as a
platform. Since Java has a runtime environment (JRE) and API, it is called a platform.
SUBODH KUMAR 40
SYNTAX
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
SUBODH KUMAR 41
APPLICATION
According to Sun, 3 billion devices run Java.
There are many devices where Java is currently used. Some of them are as follows:
1. Desktop Applications such as acrobat reader, media player, antivirus, etc.
2. Web Applications such as irctc.co.in, javatpoint.com, etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games, etc.
SUBODH KUMAR 42
TYPES OF JAVA APPLICATIONS
1) Standalone Application
Standalone applications are also known as desktop applications or window-based
applications. These are traditional software that we need to install on every machine.
Examples of standalone application are Media player, antivirus, etc. AWT and Swing
are used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is called a
web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc.
technologies are used for creating web applications in Java.
SUBODH KUMAR 43
TYPES OF JAVA APPLICATIONS
3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called
an enterprise application. It has advantages like high-level security, load balancing,
and clustering. In Java, EJB (Enterprise Java Bean) is used for creating enterprise
applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile application.
Currently, Android and Java ME are used for creating mobile applications.
SUBODH KUMAR 44
WHAT IS JVM
It is:
A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its implementation
has been provided by Oracle and other companies.
An implementation Its implementation is known as JRE (Java Runtime Environment).
Runtime Instance Whenever you write java command on the command prompt to
run the java class, an instance of JVM is created.
SUBODH KUMAR 45
WHAT IT DOES
The JVM performs following operation:
Loads code
Verifies code
Executes code
Provides runtime environment
SUBODH KUMAR 46
WHAT IT DOES
JVM provides definitions for the:
Memory area
Class file format
Register set
Garbage-collected heap
Fatal error reporting etc.
SUBODH KUMAR 47
JVM ARCHITECTURE
SUBODH KUMAR 48
DATA TYPE AND VARIABLES
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
SUBODH KUMAR 49
JAVA PRIMITIVE DATA TYPES
There are 8 types of primitive data types:
boolean data type
byte data type
char data type
short data type
int data type
long data type
float data type
double data type
SUBODH KUMAR 50
JAVA PRIMITIVE DATA TYPES
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
SUBODH KUMAR double 0.0d 8 byte 51
VARIABLES
A variable is the name of a reserved area
allocated in memory. In other words, it is a
name of the memory location. It is a
combination of "vary + able" which means its
value can be changed.
1. int data=50;//Here data is variable
SUBODH KUMAR 52
VARIABLES
1) Local Variable
A variable declared inside the body of the method is
called local variable. You can use this variable only
within that method and the other methods in the
class aren't even aware that the variable exists.
A local variable cannot be defined with "static"
keyword.
SUBODH KUMAR 53
VARIABLES
2) Instance Variable
A variable declared inside the class but outside the
body of the method, is called an instance variable. It
is not declared as static.
It is called an instance variable because its value is
instance-specific and is not shared among instances.
SUBODH KUMAR 54
VARIABLES
3) Static variable
A variable that is declared as static is called a static
variable. It cannot be local.
You can create a single copy of the static variable and
share it among all the instances of the class.
Memory allocation for static variables happens only
once when the class is loaded in the memory.
SUBODH KUMAR 55
OPERATORS
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator
SUBODH KUMAR 56
JAVA OPERATOR PRECEDENCE
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%
additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
SUBODH KUMAR Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= 57
SUBODH KUMAR 58
UNIT-4: EXPRESSIONS, STATEMENTS AND AND ARRAYS
Expressions
Statements
Control Statements
Selection Statements
Iterative Statements
Jump Statements
Arrays
SUBODH KUMAR 59
CONTROL STATEMENTS
1. Decision Making statements
if statements
switch statement
2. Loop statements
do while loop
while loop
for loop
for-each loop
3. Jump statements
break statement
continue statement
SUBODH KUMAR 60
IF STATEMENT
The "if" statement is used to evaluate a condition.
The control of the program is diverted depending upon the specific condition.
The condition of the If statement gives a Boolean value, either true or false.
In Java, there are four types of if-statements given below.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
SUBODH KUMAR 61
SYNTAX
if(condition) {
statement 1; //executes when condition is true
}
SUBODH KUMAR 62
SWITCH STATEMENT
Switch statements are similar to if-else-if statements.
The switch statement contains multiple blocks of code called cases and a single case
is executed based on the variable which is being switched.
The switch statement is easier to use instead of if-else-if statements.
It also enhances the readability of the program.
SUBODH KUMAR 63
SYNTAX
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
SUBODH KUMAR 64
LOOP STATEMENTS
In programming, sometimes we need to execute the block of code repeatedly while
some condition evaluates to true. However, loop statements are used to execute the
set of instructions in a repeated order.
The execution of the set of instructions depends upon a particular condition.
In Java, we have three types of loops:
1. for loop
2. while loop
3. do-while loop
SUBODH KUMAR 65
FOR LOOP
In Java, for loop is similar to C and C++.
It enables us to initialize the loop variable, check the condition, and increment/decrement in
a single line of code.
We use the for loop only when we exactly know the number of times, we want to execute
the block of code.
for(initialization, condition, increment/decrement) {
//block of statements
}
SUBODH KUMAR 66
FOR LOOP
SUBODH KUMAR 67
FOR-EACH LOOP
Java provides an enhanced for loop to traverse the data structures like array or collection.
In the for-each loop, we don't need to update the loop variable.
The syntax to use the for-each loop in java is given below.
for(data_type var : array_name/collection_name){
//statements
}
SUBODH KUMAR 68
FOR-EACH LOOP
SUBODH KUMAR 69
DO-WHILE LOOP
The do-while loop checks the condition at the end of the loop after executing the loop
statements.
When the number of iteration is not known and we have to execute the loop at least once,
we can use do-while loop.
It is also known as the exit-controlled loop since the condition is not checked in advance.
do
{
//statements
} while (condition);
SUBODH KUMAR 70
DO-WHILE LOOP
SUBODH KUMAR 71
JUMP STATEMENTS
Jump statements are used to transfer the control of the program to the specific
statements.
In other words, jump statements transfer the execution control to the other part of
the program.
There are two types of jump statements in Java, i.e., break and continue.
SUBODH KUMAR 72
BREAK STATEMENT
As the name suggests, the break statement is used to break the current flow of the
program and transfer the control to the next statement outside a loop or switch
statement.
However, it breaks only the inner loop in the case of the nested loop.
The break statement cannot be used independently in the Java program, i.e., it can
only be written inside the loop or switch statement.
SUBODH KUMAR 73
SYNTAX
public class BreakExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 0; i<= 10; i++) {
System.out.println(i);
if(i==6) {
break;
}
}
}
}
SUBODH KUMAR 74
CONTINUE STATEMENT
Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next
iteration of the loop immediately.
Consider the following example to understand the functioning of the continue statement in Java.
public class ContinueExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 0; i<= 2; i++) {
for (int j = i; j<=5; j++) {
if(j == 4) {
continue;
}
System.out.println(j);
} } } }
SUBODH KUMAR 75