MCS-024 Block-1 (Object Oriented Technology and Java)
MCS-024 Block-1 (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
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
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
SUBODH KUMAR 10
INHERITANCE
SUBODH KUMAR 11
POLYMORPHISM
SUBODH KUMAR 12
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
SUBODH KUMAR 15
COMPARISON OF OBJECT ORIENTED AND PROCEDURE ORIENTED
APPROACHES / BENEFITS OF OOPS
SUBODH KUMAR 17
OBJECT-BASED PROGRAMMING
SUBODH KUMAR 18
OBJECT ORIENTED PROGRAMMING
SUBODH KUMAR 19
APPLICATIONS OF OOPS
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
SUBODH KUMAR 25
CLASS
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
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
SUBODH KUMAR 31
TERMS USED IN INHERITANCE
SUBODH KUMAR 32
INHERITANCE
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
SUBODH KUMAR 40
SYNTAX
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
SUBODH KUMAR 41
APPLICATION
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
SUBODH KUMAR 46
WHAT IT DOES
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
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
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
additive +-
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ?:
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
if(condition) {
statement 1; //executes when condition is true
}
SUBODH KUMAR 62
SWITCH STATEMENT
SUBODH KUMAR 63
SYNTAX
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
SUBODH KUMAR 64
LOOP STATEMENTS
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.
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