Unit - I
INTRODUCTION TO OOP AND JAVA
Overview of OOP : Object Oriented Programming paradigms, Features of Object
Oriented Programming, Java Buzzwords
Overview of Java : Data Types, Variables, and Arrays - Operators - Control
Statements - Programming Structures in Java - Defining Classes in Java -
Constructors - Methods - Access Specifiers - Static Members - JavaDoc Comments
1.1 Overview of OOP: Object Oriented Programming paradigm
➢ Object Oriented Programming (OOP) is a programming paradigm based on the
concept of objects.
➢ It is basically a bottom-up solving approach.
➢ It is developed in order to remove flaws of procedural programming.
➢ Each object consists of data attributes and methods.
The methods or functions operate on data attributes.
1.2 Features of Object Oriented Programming :
1. Classes
2. Objects
3. Abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism
1. Classes:
➢ A class is a blueprint or template for creating objects.
➢ It defines the data (attributes) and methods (functions) common to all objects of
that type.
Syntax:
class class_name
{
private:
// variable declaration;
// function declaration;
public:
// variable declaration;
// function declaration;
};
2. Objects:
➢ An object is an instance of a class.
➢ It contains real values and behaves according to the class definition.
Syntax:
class_name object_name;
3. Abstraction:
➢ Abstraction means representing only essential features by hiding all the
background details.
➢ Shows only relevant features and hides unnecessary details.
➢ Helps in reducing complexity and increasing efficiency.
4. Encapsulation:
➢ Encapsulation means binding of data and methods together in a single entity
called a class.
➢ Hides internal details and protects the object's state from external modification.
Illustration:
+------------+
| Data |
| Method |
| Method |
+------------+
5. Inheritance:
➢ Inheritance is the property by which new classes are created from existing (old)
classes.
➢ Allows a class (child) to inherit properties and behavior from another class
(parent).
➢ Promotes code reuse and hierarchical classification.
Types of Inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
6. Polymorphism:
➢ Polymorphism is the ability to take more than one form.
➢ Allows objects of different classes to respond to the same method in different
ways.
➢ Supports method overriding and overloading.
Advantages of OOP
● Modularity: Code is organized into classes and objects.
● Reusability: Code can be reused through inheritance.
● Maintainability: Easy to maintain and update code.
● Security: Encapsulation helps protect data.
Common OOP Languages
● Java
● Python
● C++
● C#
● Ruby
1.3 Java buzzwords (Characteristics)
➢ Java was designed with a set of guiding principles known as Java Buzzwords.
➢ Java is getting popular because of its powerful features.
The following is a list of buzzwords that make Java popular:
1. Simple :
Easy to learn and use; syntax similar to C/C++ but without complexity.
2. Object-Oriented :
Everything is treated as an object, supporting encapsulation, inheritance, and
polymorphism.
3. Secure :
Provides a secure environment using features like bytecode verification, sandboxing,
and no direct memory access.
4. Platform Independent:
Java programs run on any OS using the Java Virtual Machine (JVM). "Write Once,
Run Anywhere".
5. Robust :
Strong memory management, exception handling, and compile-time error checking
make Java reliable.
6. Multithreaded :
Supports multithreading (executing multiple threads simultaneously) for
high-performance apps.
7. Interpreted :
Java bytecode is interpreted (or Just-In-Time compiled) by the JVM.
8. High Performance :
Although interpreted, Java is faster than traditional interpreted languages due to
Just-In-Time (JIT) compilation.
1.4 Overview of Java:
➢ Java is a high-level, object-oriented, platform-independent programming language
developed by Sun Microsystems
➢ It was first released in 1995 and is widely used for building web, desktop, mobile,
and enterprise applications.
Java Architecture
● Java Source Code (.java)
● Java Compiler (javac) → Converts to Bytecode (.class)
● Java Virtual Machine (JVM) → Executes bytecode on any platform
Main Components of Java
● JDK (Java Development Kit) – Tools for developing Java programs
(includes compiler, debugger, etc.)
● JRE (Java Runtime Environment) – Contains JVM and libraries needed
to run Java applications
● JVM (Java Virtual Machine) – Executes Java bytecode and provides
platform independence
Execution Process in Java:
1. Text Editor
2. Java Source Program (.java)
3. Java Compiler (javac)
4. Java (java)
5. Output
Applications of Java
● Android app development
● Web applications (Java EE, Spring)
● Desktop GUI applications (JavaFX, Swing)
● Enterprise software (banking, e-commerce)
● Scientific computing
● Game development
1.5 Data Types:
data types specify the kind of data a variable can hold. They are mainly divided into
two categories:
1. Primitive Data Types - These are the most basic data types built into the
language.
● int (4 bytes)
● long (8 bytes)
● float (4 bytes)
● char (1 byte)
● double (8 bytes)
● boolean (True or False)
2. Non-Primitive Data Types - These are created by the programmer or provided
by Java. They refer to objects.
● String - Sequence of characters
● Arrays - Collection of fixed-size elements of same type
● Classes - Custom types defined using class keyword
1.6 Variables
➢ A variable is an identifier that denotes a storage location.
➢ It acts as a container to hold values that can be changed during program
execution.
Syntax :
datatype variable_name;
Example :
int age = 25;
1.7 Arrays
➢ An array is a collection of similar types of elements.
➢ It allows you to store multiple values under a single variable name, accessible by
an index.
Key Characteristics of Arrays
● Fixed in size (declared once, cannot be changed)
● Indexed (starts from 0)
● Can store primitive types or objects
● Stored in heap memory
Syntax :
dataType[ ] arrayName = new dataType[size];
Example :
Int[ ] numbers = new int[5]; // Declaration with size
Int[ ] marks = {90, 80, 85, 95, 88}; // Declaration with values
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
Example: One-dimensional Array
class Sample
{
public static void main(String args[ ])
{
int a[ ] = new int[2];
System.out.println("Storing array");
a[0] = 1;
a[1] = 2;
System.out.println("Element at a[2] = " + a[1]);
}
}
Two-Dimensional Array:
A two-dimensional array uses arrays in which elements are stored in rows and
columns.
Syntax:
datatype array_name = new datatype[rows][columns];
Example :
int[ ][ ] matrix = new int[3][3];
1.8 Operators
Operators are the symbols used in expressions for evaluating values.
Types of Operators:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment Operators
6. Decrement Operators
1. Arithmetic Operators:
➢ Arithmetic operators are used to perform basic arithmetic operations.
➢ +, -, *, /, % are the arithmetic operators.
Example :
class Arithmetic
{
public static void main(String args[ ])
{
int a = 10, b = 20, c;
c = a + b;
System.out.println("Addition = " + c);
}
}
2. Relational Operator:
➢ Relational operators establish the relation among the operands. The result is in
Boolean Value.
➢ <, >, <=, >= are relational operators.
Example :
class Relational
{
public static void main(String args[ ])
{
int a = 10, b = 20, c;
if (a > b)
{
System.out.println("a is big");
}
else
{
System.out.println("b is big");
}
}
}
3. Logical Operators:
Logical operators are used to compare two operands. These two are Boolean
operators.
Example:
class Logical
{
public static void main(String args[ ])
{
boolean op1 = true, op2 = false;
boolean ans1, ans2;
ans1 = op1 & op2;
ans2 = op1 | op2;
System.out.println(ans1);
System.out.println(ans2);
}
}
4. Assignment Operator:
➢ Assignment operators are used to assign a value to a variable.
➢ = (equal to) is an assignment operator.
5. Conditional Operator:
The conditional operator is ?.
Syntax:
condition ? statement1 : statement2;
1.9 Control Statements
Programmers can take decisions in their program with the help of control statements.
1. if statement
2. if - else
3. while
4. do - while
5. switch
6. for loop
1. If Statement:
Types:
1. Simple
2. Compound
Syntax for Simple:
if (condition)
Statement;
Syntax for Compound:
if (condition)
{
statement1;
statement2;
}
Example (Simple if statement):
class Demo
{
public static void main(String args[ ])
{
int a = 10, b = 5;
if (a > b)
System.out.println("a is big");
}
}
2. If-else statement:
Syntax:
if (condition)
statement;
else
Statement;
Example (if-else):
class Demo
{
public static void main(String args[ ])
{
int a = 2, b = 5;
if (a > b)
System.out.println("a is big");
else
System.out.println("b is big");
}
}
If - else - if :
Syntax:
if (condition)
statement;
else if (condition II)
statement;
else
Statement;
Example:
class Demo
{
public static void main(String args[ ])
{
int a = 5, b = 5;
if (a > b)
System.out.println("a is big");
else if (b > a)
System.out.println("b is big");
else
System.out.println("Both are equal");
}
}
3. while Statement:
A while loop is a loop that repeats a block of code as long as the given condition is
true. The condition is checked before each repetition.
Syntax:
while (condition) {
statement 1;
...
statement n;
}
Example:
class Demo
{
public static void main(String args[ ])
{
int count = 1;
int i = 0;
while (count <= 2) {
i = i + 1;
System.out.println(i);
count++;
}
}
}
4. do-while Statement:
A do-while loop is a loop that executes the code block at least once and then repeats
it as long as the condition is true.
Syntax:
do {
statement 1;
...
statement n;
} while (condition);
Example:
class Sample
{
public static void main(String args[ ])
{
int count = 1, i = 0;
do
{
i = i + 1;
System.out.println(+i);
count++;
}
while (count <= 3);
}
}
5. for loop:
➢ A for loop in Java is a control flow statement that allows code to be executed
repeatedly based on a boolean condition.
➢ It provides a compact way to initialize a loop variable, test a condition, and update
the variable, all in a single line of syntax.
Syntax:
for (initialization; condition; inc/dec) {
statement 1;
...
statement n;
}
Example:
class Sample
{
public static void main(String args[ ])
{
for (int i = 0; i <= 3; i++)
{
System.out.println("i = " + i);
}
}
}
6. Switch Case in Java:
The switch statement allows you to choose between multiple possible paths of
execution, depending on the value of a single variable.
Example
class Sample
{
public static void main(String args[])
{
int choice;
System.out.println("1. A");
System.out.println("2. B");
System.out.println("Enter choice:");
choice = (int) System.in.read();
switch (choice)
{
case 1:
System.out.println("You selected A");
break;
case 2:
System.out.println("You selected B");
break;
default:
System.out.println("None");
}
}
}
1.10 Programming Structures in Java
1. Documentation section
↓
2. Package Statement
↓
3. Import Statement
↓
4. Class Definitions
↓
5. Main Method Class
1. Documentation Section
● Comment lines at the top of the program
● Used to describe the program (e.g., purpose, author, date)
● Example:
// This program print hello world in console
2. Package Statement Section
● Declares the package the class belongs to
● Used to group related classes together
● Must be the first statement (if used)
● Example:
package mypackage;
3. Import Statement
● Brings in other Java classes or packages
● Example:
import java.util.Scanner;
4. Class Definitions
● The core of every Java program
● Classes contain data and methods
● Example:
class Circle
{
//Variable:
//Method();
}
5. Main Method Class
○ Contains the main() method, which is the entry point of execution
○ Required for stand-alone applications
○ Example:
public class MainClass
{
public static void main(String[] args)
{
System.out.println("Hello, Java!");
}
}
1.11 Constructors :
➢ A constructor in Java is a special method used to initialize objects.
➢ It is invoked automatically when an instance of a class is created and has the
same name as the class.
➢ constructors do not have a return type, not even void.
Syntax :
class Test
{
Test( )
{
// constructor body
}
}
Example :
class Distance
{
int km;
int m;
Distance(int k, int m)
{
km = k;
meter = m;
}
public static void main(String args[ ])
{
Distance shop = new Distance(1, 200);
Distance hotel = new Distance(2, 800);
}
}
1.12 Access Specifiers :
An access specifier (also known as an access modifier) in Java is a keyword used to
control the visibility or accessibility of classes, constructors, methods, and variables
to other classes and packages.
Types :
1. Public
2. Private
3. Default
1. Public :
Allows classes, methods, and data fields accessible from any class.
2. Private :
Allows classes, methods, and data fields accessible from only within the own class.
3. Default :
The default access modifier is used when no access modifier is specified. It allows
access only within the same package.
Example :
package myPackage;
public class ClassA
{
public int pub = 1;
int def = 2; // default (package-private)
private int pri = 3;
public void show( )
{
System.out.println("public: " + pub);
System.out.println("default: " + def);
System.out.println("private: " + pri);
}
}
package myPackage;
public class ClassB
{
public void test( )
{
ClassA a = new ClassA();
✅
System.out.println(a.pub); // public - accessible
✅
System.out.println(a.def); // default - accessible (same package)
❌
// System.out.println(a.pri); // private - NOT accessible
}
}
1.13 Static Members :
➢ Static members in Java are variables or methods that are declared
using the static keyword.
➢ They are shared among all instances of a class and can be accessed
without creating an object of the class.
Example:
public class MyClass
{
static int count = 0; // static variable
static void sayHello() // static method
{
System.out.println("Hello!");
}
public static void main(String[ ] args)
{
MyClass.sayHello(); ✅
// No object needed
✅
System.out.println(MyClass.count); // Accessing static variable
}
}
1.14 Java Doc Comments :
➢ Java Doc Comments are special comments used to generate documentation
using the javadoc tool.
➢ They start with /** and end with */. These are placed above classes,
methods, constructors, or fields to describe their purpose.
Types:
1. Class level comments
2. Member level comments
Class-level Comments
● Placed above a class definition.
● Describe the overall purpose and role of the class.
Member-level Comments
These are used for:
● Fields (variables)
● Methods and Constructors
Example:
/**
* This is a simple Hello class.
*/
public class Hello
{
/**
* Prints "Hello, World!" to the console.
*/
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}