Unit 1: Introduction




Introduction to Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a way of writing computer programs by thinking in terms of real-world things (objects) instead of only instructions.

Simple idea:

Instead of focusing on what to do, OOP focuses on who does it.

Example: A Car is an object.

  • It has properties: color, speed, model
  • It has actions: start, stop, accelerate

OOP helps programmers:

  • Write clean and organized code
  • Reuse code
  • Maintain and modify programs easily

Objects

An object is a real-world entity that has:

  • Data (variables)
  • Behavior (methods/functions)

Example: Object: Mobile Phone

Data (Properties)Behavior (Actions)
BrandCall
PriceSend Message
ColorBrowse Internet

In Java, everything works around objects.

Classes

A class is a blueprint or template used to create objects.

Simple meaning:

  • Class → Design
  • Object → Actual item made from the design

Example: Class: Student

Objects: Ram, Shyam, Geeta

Each student object has:

  • Name
  • Roll number
  • Marks

Abstraction

Abstraction means showing only important details and hiding unnecessary details.

Real-life example:

When you drive a car:

  • You use steering, brakes, and accelerator
  • You do NOT need to know how the engine works internally

In programming:

  • Only essential features are shown to the user
  • Complex code is hidden

Benefit: Makes programs easy to use and understand.

Encapsulation

Encapsulation means wrapping data and methods together into a single unit (class) and protecting data from outside access.

Real-life example: A capsule contains medicine inside and protects it.

In Java:

  • Data is kept private
  • Access is allowed using methods (get/set)

Benefit:

  • Improves security
  • Prevents misuse of data

Inheritance

Inheritance allows one class to use properties and methods of another class.

Simple meaning:

  • Child class uses features of Parent class

Real-life example:

  • Father → Car, House
  • Son → Inherits car and house

Example in Java:

  • Class Animal → eat(), sleep()
  • Class Dog → bark() + eat(), sleep()

Benefit:

  • Code reuse
  • Less duplication

Polymorphism

Polymorphism means one name, many forms.

Real-life example:

A person can be:

  • A father at home
  • An employee at office
  • A customer in a shop

In programming: Same method behaves differently in different situations.

Example: Method: draw()

  • Circle → draws circle
  • Square → draws square

Benefit:

  • Flexible and dynamic code

OOP in Java

Java is a pure object-oriented programming language (almost everything is an object).

Java supports:

  • Objects
  • Classes
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Because of OOP, Java programs are:

  • Modular
  • Secure
  • Easy to maintain

Characteristics of Java

Java has several powerful features:

FeatureExplanation
SimpleEasy syntax similar to English
Object-OrientedBased on OOP concepts
Platform IndependentRun anywhere using JVM
SecureNo pointer, strong memory management
RobustHandles errors efficiently
MultithreadedCan perform multiple tasks at once
PortableSame code runs on different machines

The Java Environment

Java program runs using three main components:

1. JDK (Java Development Kit)

Used by programmers to write and develop programs.

2. JRE (Java Runtime Environment)

Used to run Java programs.

3. JVM (Java Virtual Machine)

Converts Java code into machine-readable code.

Working Flow: Java Code → Compiler → Bytecode → JVM → Output

Java Source File Structure

A Java program follows a fixed structure.

Basic Structure

package statement; import statements; class ClassName { variables; methods; main method; }

Important Points

  • File name = Class name
  • Main method is the starting point of execution
  • Code is written inside a class

Compilation and Execution of Java Program

Step-by-Step Process:

  1. Write Java code - File saved as .java

  2. Compilation

    • Java compiler (javac) converts code into bytecode

    • Output file: .class

  3. Execution

    • JVM runs the bytecode using java command

Example

javac Hello.java java Hello

Conclusion

Object-Oriented Programming and Java help programmers:

  • Think in real-world terms
  • Write reusable and secure code
  • Build large and complex applications easily

Java’s OOP nature, platform independence, and simplicity make it one of the most popular programming languages in the world.

Defining Classes in Java

A class is a blueprint that defines:

  • Data (variables)
  • Behavior (methods)

Simple meaning: A class tells Java what an object will look like and what it can do.

Example:

class Student { int rollNo; String name; void display() { System.out.println(rollNo + " " + name); } }

Key Points:

  • Class keyword is class
  • Everything in Java is written inside a class
  • File name should match class name

Constructors

A constructor is a special method used to initialize objects.

Simple meaning: When an object is created, the constructor automatically runs.

Characteristics

  • Name is same as class name
  • No return type (not even void)
  • Called automatically

Example

class Student { int rollNo; String name; Student(int r, String n) { rollNo = r; name = n; } }

Types of Constructors:

  1. Default Constructor – No parameters
  2. Parameterized Constructor – Takes parameters

Methods

A method is a block of code that performs a specific task.

Example:

void show() { System.out.println("Hello Java"); }

Why Methods?

  • Avoid code repetition
  • Improve readability
  • Easy maintenance

Access Specifiers (Access Modifiers)

Access specifiers control the visibility of classes, methods, and variables.

ModifierVisibility
publicAccessible everywhere
privateAccessible only within same class
protectedAccessible within package or subclass
defaultAccessible within same package

Example:

public int marks; private int salary;

Static Members

Static members belong to the class, not to objects.

Simple meaning: Only one copy exists, shared by all objects.

Example:

class College { static String collegeName = "ABC College"; }

Static can be:

  • Variables
  • Methods
  • Blocks

Benefit:

  • Saves memory
  • Used for common data

Comments in Java

Comments are non-executable statements used to explain code.

Types of Comments:

  1. Single-line

// This is a comment
  1. Multi-line

/* This is a multi-line comment */
  1. Documentation

/** This is documentation comment */

Data Types in Java

Data types define what kind of data a variable can store.

Types of Data Types:

A. Primitive Data Types

Data TypeSizeExample
int4 bytes10
float4 bytes10.5
double8 bytes99.99
char2 bytes'A'
boolean1 bittrue

B. Non-Primitive Data Types

  • String
  • Arrays
  • Classes
  • Objects

Variables

A variable is a container used to store data.

Types of Variables:

TypeDescription
LocalDeclared inside method
InstanceBelong to object
StaticBelong to class

Example:

int age = 20;

Operators

Operators are symbols used to perform operations.

Types of Operators:

TypeExamples
Arithmetic+, -, *, /
Relational>, <, ==
Logical&&,
Assignment=, +=
Unary++, --
Ternary?:

Example:

int sum = a + b;

Control Flow Statements

Control flow decides which code runs and how many times.

Decision-Making Statements

  1. if

if (age > 18) { System.out.println("Adult"); }
  1. if-else

  2. switch

Looping Statements

LoopUse
forFixed iterations
whileCondition-based
do-whileExecutes at least once

Jump Statements

  • break
  • continue
  • return

Arrays

An array stores multiple values of same data type.

Example:

int[] marks = {70, 80, 90};

Types of Arrays:

  1. Single-dimensional
  2. Multi-dimensional

Example of 2D Array:

int[][] matrix = { {1, 2}, {3, 4} };

Benefits:

  • Stores multiple values efficiently
  • Easy data management

Conclusion

Fundamental programming structures in Java help to:

  • Organize code logically
  • Control program flow
  • Store and manipulate data
  • Build real-world applications efficiently

These concepts are the foundation of Java programming and essential for interviews and exams.