Unit I - Introduction To OOP Concepts and Control Structure
Unit I - Introduction To OOP Concepts and Control Structure
OOP & CG
Unit I – Introduction to OOP Concepts
and Control Structure
By
Prof. Gajanan Bhusare
Unit I Contents
• Programming paradigms - Introduction to programming paradigms, Introduction
to four main Programming paradigms- procedural, object oriented, functional, and
logic & rule based. Need of object- oriented programming,
• Fundamentals of object-oriented programming: Namespaces, objects, classes,
data members, methods, messages, data encapsulation, data abstraction and
information hiding, inheritance, polymorphism. Benefits of OOP, Java as object
oriented programming language.
• Overview of Java Language: simple Java program structure: documentation
section, package statement, import statements, class definition, main method
class. Implementing Java Program, , Data types, Primitive Types vs. Reference
type, floating point numbers, operators and expressions, Java Class Libraries,
Typical Java Development Environment, and Memory Concepts.
• Control Statements: Selection Statements: if, if-else, nested if-else, Iteration
Statements: do, while, for, for-each statement, break, and continue statements
Unit I - Prof. Gajanan Bhusare 2
1
8/5/2025
PROGRAMMING PARADIMES
2
8/5/2025
• Advantages:
• Simple and easy to understand.
• Efficient for small to medium-sized applications.
• Code can be reused using functions, reducing redundancy.
• Disadvantages:
• Becomes difficult to manage for large programs.
• Less secure due to the use of global variables.
• Code modification can be complex if changes are required.
3
8/5/2025
• Advantages:
• Makes code reusable and modular.
• Easier to manage large and complex programs.
• Increases security by controlling data access.
• Disadvantages:
• Requires more memory and processing power.
• Can be complex for beginners to understand.
• Debugging can be difficult due to interdependencies.
4
8/5/2025
3. Functional Programming
• It is based on treating functions as the main building blocks of a program.
• It avoids changing data and relies on mathematical functions to perform
tasks.
• Advantages:
• Makes code easier to test and debug.
• Encourages modular and reusable code.
• Reduces unexpected errors by avoiding data modification.
• Disadvantages:
• Requires a different way of thinking compared to traditional paradigms.
• Can be less efficient for certain tasks.
• Not all languages fully support functional programming.
5
8/5/2025
• Rule Based :
• Paradigm where the program's behavior is determined by a set of rules that
define relationships between different entities and how the program should
respond to various situations.
Unit I - Prof. Gajanan Bhusare 11
6
8/5/2025
7
8/5/2025
8
8/5/2025
FUNDAMENTALS OF
OBJECT ORIENTED PROGRAMMING
9
8/5/2025
Namespaces
Objects
• The fundamental building blocks, representing instances of classes that
encapsulate data (attributes) and behavior (methods)
• An object is a real-world entity that has a particular behavior and a state.
• It can be physical or logical programming language.
• An object is an instance of a class and memory is allocated only when an
object of the class is created.
10
8/5/2025
Classes
• Class acts as a blueprint for creating objects.
• It defines the structure and behavior that objects of that class will have.
Data Members
• Data member refers to a variable that is a part of a class.
• These variables hold data associated with objects of the class.
• Data members can be of various types, including fundamental data types
(such as int, float, double, etc.), user-defined types (such as other
classes), or even arrays.
• Data members are typically declared within the body of a class.
• They can have different access specifiers like public, private, or protected,
which determine their visibility and accessibility from outside the class
Unit I - Prof. Gajanan Bhusare 22
11
8/5/2025
Methods
Messages
• Objects communicate with one another by sending and receiving information
from each other.
• Message passing involves specifying the name of the object, the name of the
function, and the information to be sent.
• messages are requests sent between objects to trigger specific actions or retrieve
information.
• Think of it as objects communicating with each other to get things done.
• A message includes the receiving object, the action to be performed (a method),
and any necessary data for that action.
Unit I - Prof. Gajanan Bhusare 24
12
8/5/2025
Data Encapsulation
• Encapsulation helps to wrap up the functions and data together in a single unit.
• By privatizing the scope of the data members it can be achieved.
• This particular feature makes the program inaccessible to the outside world.
• It also involves restricting direct access to some of the object's components,
often through access modifiers like private, protected, and public. This helps in
hiding the internal implementation details of a class and exposing only what is
necessary to the outside world
13
8/5/2025
Data Abstraction
• Hiding internal details and showing functionality is known as abstraction.
• Data abstraction is the process of exposing to the outside world only the
information that is absolutely necessary while concealing
implementation or background information.
14
8/5/2025
Information Hiding
• Data hiding means hiding the internal data within the class to prevent
its direct access from outside the class.
Access Specifiers
• Data hiding involves classes, and classes have three different kinds of
protection/access specifiers.
• Typically, the data within a class is private, and the functions are
public. Since the data is hidden, it will be safe from accidental
manipulation.
Unit I - Prof. Gajanan Bhusare 30
15
8/5/2025
Inheritance
• When one object acquires all the properties and behaviors of parent
object i.e. known as inheritance.
• It provides code reusability. It is used to achieve runtime polymorphism.
• Sub class - Subclass or Derived Class refers to a class that receives
properties from another class.
• Super class - The term "Base Class" or "Super Class" refers to the class
from which a subclass inherits its properties.
16
8/5/2025
17
8/5/2025
Polymorphism
• Polymorphism means the ability to take more than one form in the
programming language.
• With this feature, you can use the same function to perform different
tasks thus increasing code reusability.
18
8/5/2025
Benefits of OOP
• Modular, scalable, extensible, reusable, and maintainable.
• It models the complex problem in a simple structure.
• Object can be used across the program.
• Code can be reused.
• We can easily modify, append code without affecting the other code
blocs.
• Provides security through encapsulation and data hiding features.
• Beneficial to collaborative development in which a large project is
divided into groups.
• Debugging is easy.
Unit I - Prof. Gajanan Bhusare 38
19
8/5/2025
Limitations of OOP
• Requires intensive testing processes.
• Not suitable for small problems.
• Takes more time to solve problems.
• programs can consume a large amount of memory.
• The size of the programs created using this approach may become larger than
the programs written using the procedure-oriented programming approach.
• Software developed using this approach requires a substantial amount of pre-
work and planning.
• OOP code is difficult to understand if you do not have the corresponding class
documentation.
Unit I - Prof. Gajanan Bhusare 39
20
8/5/2025
21
8/5/2025
22
8/5/2025
• Multithreading: Java programs can do many things at the same time using
multiple threads. This is useful for handling complex tasks like processing
transactions.
• Just-In-Time (JIT) Compiler: Java uses a JIT compiler. It improves
performance by converting the bytecode into machine readable code at
the time of execution.
23
8/5/2025
Documentation Section
• details about the program includes the author's name, creation date,
version, program name, company name, and a brief description
• optional part of a Java program,
• To include these details, programmers typically use comments.
//Single-line comment
/* Multiline comment
in Java */
/** Documentation comment */
Unit I - Prof. Gajanan Bhusare 47
Package Statement
• Declaring the package in the structure of Java is optional.
• It comes right after the documentation section.
• You mention the package name where the class belongs.
• Only one package statement is allowed in a Java program and must come
before any class or interface declaration.
• This declaration helps organize classes into different directories based on
the modules they're used in.
• You use the keyword package followed by the package name.
24
8/5/2025
import statements
• Import statements are used to import classes, interfaces, or enums that
are stored in packages or the entire package.
• A package contains many predefined classes and interfaces.
• We need to mention which package we are using at the beginning of the
program.
• We do it by using the import keyword.
• We either import the entire package or a specific class from that
package.
Interface Section
• This is an optional section.
• The keyword interface is used to create an interface.
• An interface comprises a set of cohesive methods that lack
implementation details., i.e. method declaration and constants.
Example :
interface Code {
void write();
void debug();
}
Unit I - Prof. Gajanan Bhusare 50
25
8/5/2025
Class Definition
• This is a mandatory section in the structure of Java program.
• Each Java program has to be written inside a class as it is one of the main
principles of Object-oriented programming that Java strictly follows, i.e.,
its Encapsulation for data security.
• There can be multiple classes in a program. Some conventions need to be
followed to name a class. They should begin with an uppercase letter.
class Program{
// class definition
}
26
8/5/2025
System.out.println("Hello, Java!");
}
27
8/5/2025
Data Types
28
8/5/2025
Primitive Types:
• Direct Storage:
• store the actual data value directly within the variable itself.
• Examples:
• In many languages, these include int, float, double, char, boolean, and
similar basic data types.
• Stack Allocation:
• Primitive types are typically stored on the stack, a region of memory that
is fast to access but limited in size.
Unit I - Prof. Gajanan Bhusare 58
29
8/5/2025
• Value Semantics:
• When passed to a function, a copy of the primitive value is created,
so changes within the function do not affect the original value.
• Immutable:
• Primitive types are often immutable, meaning their values cannot be
changed after they are created.
Reference Types:
• Indirect Storage:
• Reference types store a reference (memory address) to the actual data,
which may be stored elsewhere in memory.
• Examples:
• In many languages, these include objects, arrays, strings (in some
languages), and other complex data structures.
• Heap Allocation:
• Reference types are typically stored on the heap, a larger region of
memory that is slower to access but can hold more data.
Unit I - Prof. Gajanan Bhusare 60
30
8/5/2025
31
8/5/2025
32
8/5/2025
33
8/5/2025
34
8/5/2025
35
8/5/2025
36
8/5/2025
37
8/5/2025
CONTROL STATEMENTS
38
8/5/2025
if-else
• the if-else statement is a selection control statement used to execute different
blocks of code based on a condition. It allows a program to make decisions and
follow different paths of execution.
if (condition)
{
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Unit I - Prof. Gajanan Bhusare 77
nested if-else
• a nested if-else statement involves placing an if or if-else block inside
another if or else block.
• This allows for hierarchical decision-making, where a condition is
evaluated, and if it's true (or false, for the else branch), another
condition is then evaluated within that specific branch.
39
8/5/2025
if (outerCondition) {
// Code to execute if outerCondition is true
if (innerCondition1) {
// Code to execute if outerCondition and innerCondition1 are true
} else {
// Code to execute if outerCondition is true but innerCondition1 is false
}
} else {
// Code to execute if outerCondition is false
if (innerCondition2) {
// Code to execute if outerCondition is false and innerCondition2 is true
} else {
// Code to execute if outerCondition is false and innerCondition2 is false
}
}
Unit I - Prof. Gajanan Bhusare 79
do {
// Statements to be executed
} while (condition);
40
8/5/2025
for
• The for loop in Java is an iteration statement used to repeatedly execute
a block of code a specific number of times. It is particularly useful when
the number of iterations is known beforehand.
for (initialization; condition; increment/decrement) {
// statements to be executed repeatedly
}
for-each statement
• The for-each statement, also known as the enhanced for loop, in Java
provides a simplified way to iterate over elements of arrays and collections
(like ArrayList, HashSet, etc.).
• It was introduced in Java 5 to make code more readable and concise when the
need is to simply access each element in a sequence without needing an
explicit index.
for (DataType element : collection Or Array) {
// Code to be executed for each element
}
Unit I - Prof. Gajanan Bhusare 82
41
8/5/2025
• Explanation:
• DataType: This represents the data type of the elements within the
collection Or Array.
• element: This is a variable that will sequentially hold each element
from the collection Or Array during each iteration of the loop.
• Collection Or Array: This refers to the array or collection you want to
iterate over.
• How it works:
• The for-each loop iterates through each element in the collection Or
Array from beginning to end.
• In each iteration, the current element is assigned to the element
variable, and the code inside the loop's body is executed.
Example
public class ForEachExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
42
8/5/2025
• 1. break statement:
• The break statement is used to immediately terminate the innermost loop (or
switch statement) in which it is encountered.
• When break is executed, control is transferred to the statement immediately
following the loop or switch block.
• It is commonly used when a specific condition is met within a loop, and further
iterations are no longer necessary.
Unit I - Prof. Gajanan Bhusare 85
// Output: 0, 1, 2, 3, 4
43
8/5/2025
• 2. continue statement:
• The continue statement is used to skip the current iteration of a loop
and proceed to the next iteration.
• When continue is executed, the remaining statements within the
current iteration of the loop are bypassed, and the loop's control
expression is re-evaluated for the next iteration.
• It is useful when certain conditions within a loop require skipping
specific processing steps for the current iteration, but the loop needs
to continue.
Unit I - Prof. Gajanan Bhusare 87
// Output: 0, 1, 3, 4
44
8/5/2025
THANK YOU !
45