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) |
|---|---|
| Brand | Call |
| Price | Send Message |
| Color | Browse 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:
| Feature | Explanation |
|---|---|
| Simple | Easy syntax similar to English |
| Object-Oriented | Based on OOP concepts |
| Platform Independent | Run anywhere using JVM |
| Secure | No pointer, strong memory management |
| Robust | Handles errors efficiently |
| Multithreaded | Can perform multiple tasks at once |
| Portable | Same 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
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:
-
Write Java code - File saved as
.java -
Compilation
-
Java compiler (
javac) converts code into bytecode -
Output file:
.class
-
-
Execution
-
JVM runs the bytecode using
javacommand
-
Example
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:
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
Types of Constructors:
- Default Constructor – No parameters
- Parameterized Constructor – Takes parameters
Methods
A method is a block of code that performs a specific task.
Example:
Why Methods?
- Avoid code repetition
- Improve readability
- Easy maintenance
Access Specifiers (Access Modifiers)
Access specifiers control the visibility of classes, methods, and variables.
| Modifier | Visibility |
|---|---|
| public | Accessible everywhere |
| private | Accessible only within same class |
| protected | Accessible within package or subclass |
| default | Accessible within same package |
Example:
Static Members
Static members belong to the class, not to objects.
Simple meaning: Only one copy exists, shared by all objects.
Example:
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:
-
Single-line
-
Multi-line
-
Documentation
Data Types in Java
Data types define what kind of data a variable can store.
Types of Data Types:
A. Primitive Data Types
| Data Type | Size | Example |
|---|---|---|
| int | 4 bytes | 10 |
| float | 4 bytes | 10.5 |
| double | 8 bytes | 99.99 |
| char | 2 bytes | 'A' |
| boolean | 1 bit | true |
B. Non-Primitive Data Types
- String
- Arrays
- Classes
- Objects
Variables
A variable is a container used to store data.
Types of Variables:
| Type | Description |
|---|---|
| Local | Declared inside method |
| Instance | Belong to object |
| Static | Belong to class |
Example:
Operators
Operators are symbols used to perform operations.
Types of Operators:
| Type | Examples |
|---|---|
| Arithmetic | +, -, *, / |
| Relational | >, <, == |
| Logical | &&, |
| Assignment | =, += |
| Unary | ++, -- |
| Ternary | ?: |
Example:
Control Flow Statements
Control flow decides which code runs and how many times.
Decision-Making Statements
-
if
-
if-else
-
switch
Looping Statements
| Loop | Use |
|---|---|
| for | Fixed iterations |
| while | Condition-based |
| do-while | Executes at least once |
Jump Statements
- break
- continue
- return
Arrays
An array stores multiple values of same data type.
Example:
Types of Arrays:
- Single-dimensional
- Multi-dimensional
Example of 2D Array:
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.