Unit 1: Fundamental of Object Oriented programming
Object-Oriented Paradigm – Basic Concepts of Object-Oriented Programming –
Benefits of Object-Oriented Programming –Application of Object-Oriented
Programming. Java Evolution: History –Features – How Java differs from C and C++
– Java and Internet – Java and www –Web Browsers. Overview of Java: simple Java
program – Structure – Java Tokens – Statements – Java Virtual Machine.
Java
Java is a general-purpose, object-oriented programming language.
Developed by Sun Microsystems in 1991, led by James Gosling.
Originally called Oak, later renamed Java in 1995.
Object-Oriented Paradigm
What is a paradigm?: A paradigm simply means an approach or a method. In programming,
different paradigms give us different ways to write and organize code.
Common Programming Paradigms
Procedural programming is the oldest and most straightforward programming style.
It is based on a sequence of step-by-step instructions to complete a task.
Programs are built using procedures or functions that operate on data.
Follows a top-down approach – starting from the main function and dividing tasks into
sub-functions.
Focuses on how the program works rather than what needs to be done.
Examples of procedural programming languages: C, Pascal.
It is simple and suitable for linear or sequential applications, like calculators or simple
games.
OOP organizes programs around objects, which are instances of classes.
Each object contains data (attributes) and functions (methods) that operate on the data.
OOP is based on four main principles:
🔹 Encapsulation – bundling data and methods together
🔹 Inheritance – reusing code from parent classes
🔹 Polymorphism – one function behaving differently in different contexts
🔹 Abstraction – hiding complex details and showing only essentials
It makes programs more modular, reusable, and easier to manage.
OOP is well-suited for large and complex software systems.
Common object-oriented programming languages: Java, C++, Python.
Functional Programming Paradigm:Functional programming is based on writing programs
using pure functions.
It avoids changing data (immutability) and does not use loops.
Emphasizes concepts like:
🔹 Immutability – data cannot be changed
🔹 Recursion – functions calling themselves instead of using loops
🔹 Function composition – combining functions to build complex operations
Suitable for concurrent or parallel processing where safe data handling is important.
Commonly used in languages like Haskell, Scala, and also supported in JavaScript and
Python.
Features like map, filter, reduce, and lambda functions are part of functional
programming in modern languages.
Logic-Based Programming Paradigm
Based on formal logic, not instructions.
Programmer defines facts and rules.
The system uses logical reasoning to deduce answers.
Commonly used in Artificial Intelligence and expert systems.
Example language: Prolog.
Example fact: father(john, mary). – the system can answer questions like “Is John the
father of Mary?”
Imperative Programming Paradigm
Focuses on how a task is performed.
Uses step-by-step instructions that change the program's state.
Includes loops, variables, and conditions for control flow.
Resembles a recipe with clear steps.
Languages: Java, C, Python (procedurally).
Example: using a for loop to calculate the sum of numbers from 1 to 5.
Declarative Programming Paradigm
Focuses on what result is wanted, not how to get it.
No detailed control flow is provided.
Programs look clean and readable, ideal for data queries.
Languages: SQL, HTML, Prolog.
Example: SELECT SUM(marks) FROM students; – gets total marks without describing a
loop.
Paradigm Key Idea How It Works Examples Use Cases
Code is a set of Uses procedures/functions; Simple linear
Procedural step-by-step top-down approach; data is C, Pascal tasks, calculators,
instructions separate from code small programs
Uses OOP principles
Code is organized Large systems,
Object- (encapsulation, inheritance, Java, C++,
around objects and GUIs, simulations,
Oriented etc.); models real-world Python
classes games
entities
Emphasizes pure Avoids changing state or data; Haskell, Math-heavy apps,
Functional functions and uses recursion and function Scala, AI, parallel
immutability composition JavaScript programming
Uses facts and Programmer defines logic, and AI systems, expert
Logic-Based rules to derive system answers queries using Prolog systems, rule-
answers using logic inference based solutions
🔹 The Object-Oriented Paradigm (OOP) is a programming approach that models real-world
entities as objects. It focuses on data (objects) and the methods (functions) that operate on that
data, rather than procedural logic.
Characteristics:
Programs are built using classes and objects.
Focuses on real-world modeling and reusability.
Supports concepts like inheritance, encapsulation, polymorphism, and abstraction.
Basic concepts of object oriented programming language
1. Class
A class is the fundamental building block of object-oriented programming.
It acts like a blueprint or template for creating objects.
A class defines attributes (also called fields or variables) and methods (functions or
behaviors) that describe what the object will contain and how it will behave.
For example, a class Car may have attributes like color and speed, and methods like
start() and brake().
A class itself does not hold any real data—it just defines the structure.
2. Object
An object is a real instance of a class.
When a class is created and memory is allocated, it becomes an object.
Each object can hold its own data and use the behaviors defined in the class.
For example, if Car is a class, then myCar and yourCar are two different objects with their
own colors and speeds.
Objects interact with each other by sending messages (calling methods), making the
program more like a real-world model.
3. Encapsulation
Encapsulation is the concept of wrapping data and methods into a single unit known as
a class.
It helps to protect the internal state of an object from unauthorized access.
This is done using access modifiers such as private, public, and protected.
Direct access to data members is restricted, and data can only be accessed or modified
through special methods called getters and setters.
Encapsulation improves security, modularity, and maintainability of code.
4. Inheritance
Inheritance allows a class (called a child or subclass) to inherit properties and methods
from another class (called a parent or superclass).
It promotes code reuse, meaning we can create new classes without rewriting existing
code.
It also helps in building hierarchical structures like general to specific.
For instance, a class Animal can be the parent, and Dog, Cat, Bird can be child classes.
Java uses the extends keyword to implement inheritance.
5. Polymorphism
The word polymorphism means “many forms.”
In programming, polymorphism allows the same method or function name to behave
differently based on the object or arguments.
Java supports two types of polymorphism:
o Compile-Time Polymorphism (Method Overloading): Multiple methods with
the same name but different parameters.
o Run-Time Polymorphism (Method Overriding): A child class provides a
specific version of a method already defined in the parent class.
6. Abstraction
Abstraction is the process of hiding complex internal details and showing only the
essential features to the user.
It helps to reduce complexity and makes the interface cleaner and easier to use.
In Java, abstraction is achieved using abstract classes and interfaces.The actual
implementation is hidden, while the function names and behavior are made visible
Benefits of Object-Oriented Programming
1. Modularity
In OOP, the program is divided into separate classes, also called modules.
Each class handles a specific task or functionality, such as handling user login, database
connection, or payment processing.
This modular structure makes the codebase organized, clean, and easy to manage.
Changes in one module (e.g., Payment) do not affect other modules (e.g., Login),
reducing risk of errors.
2. Reusability
Once a class is written, it can be reused in multiple programs or projects.
Example: A class Person with name and age can be reused in projects like Employee
Management, Hospital System, etc.
Inheritance allows new classes to use and extend existing functionality without
rewriting it.
This promotes efficient development and reduces redundancy.
3. Maintainability
Because of encapsulation, data and logic are wrapped inside classes.
This structure protects internal code and reduces the risk of accidental changes.
It is easier to debug, modify, or enhance parts of the code without affecting the entire
system.
Maintenance is faster, which is crucial for long-term software projects.
4. Scalability
OOP is ideal for large-scale applications like banking software, e-commerce platforms,
and social media apps.
Since each class handles a small part of the system, developers can easily expand the
application by adding new classes.
Scalability ensures that as your program grows, its performance and structure remain
manageable.
5. Real-World Modeling
OOP allows programmers to model real-world entities as code.
For example, a class Car can have objects like car1, car2, with attributes like color,
speed, and methods like start(), stop().
This makes code intuitive and easier to understand, especially for beginners.
6. Security
OOP supports data hiding using encapsulation.
By marking variables private, we prevent unauthorized access or modification from
outside the class.
Controlled access is provided using getter and setter methods, which can include
validation.
This improves overall data integrity and safety.
7. Flexibility through Polymorphism
Polymorphism allows one function or method to work with different types of objects.
For example, the method draw () can be defined differently for Circle, Rectangle, and
Triangle classes.
This provides flexibility, code simplicity, and allows writing generic code that can work
across many object types.
8. Improved Collaboration
In OOP-based development, teams can work independently on different classes or
modules.
For example, one developer can work on UserModule, another on ProductModule, and
another on PaymentModule.
This enables faster development, better team coordination, and clear division of
responsibilities.
9. Extensibility
OOP makes it easy to extend existing code.
For instance, if a new feature is needed, developers can add a new class or override a
method in a subclass without modifying the existing codebase.
This supports future growth and enhancement without breaking old code.
10. Ease of Testing and Debugging
OOP promotes unit testing—each class or module can be tested independently.
Errors can be isolated quickly because of modular structure.
Reusable classes allow test cases to be reused too.
Object-Oriented Programming (OOP) – Applications
🔹 What is OOP?
OOP (Object-Oriented Programming) is a programming methodology based on the concept of
"objects", which encapsulate data and behaviour together.
🔹 Applications of OOP:
1. Real-world Systems Simulation
o OOP is ideal for simulating real-world systems like banking, airlines, or medical
systems.
2. Graphical User Interfaces (GUIs)
o Used in building GUI-based applications with components like buttons, menus,
etc., using languages like Java Swing or JavaFX.
3. Game Development
o Helps model game elements (players, enemies, items) as objects. Increases
modularity and reusability.
4. Web-Based Applications
o Backend frameworks (e.g., Java with Spring) use OOP principles for structuring
large-scale web apps.
5. Mobile Applications
o OOP is widely used in mobile app development, especially in Android
(Java/Kotlin).
6. Distributed Systems
o Objects can represent remote services or data models in distributed architecture.
7. Software Engineering Tools
o Development environments, editors, debuggers, and modeling tools often follow
OOP design.
8. Database Management Systems
o OOP supports complex data models using object-relational mapping (ORM)
frameworks like Hibernate.
Java Evolution: History & Milestones
Early History of Java
1. Java was developed by Sun Microsystems in 1991 by a team known as the Green Team,
led by James Gosling (also called the "Father of Java").
2. The language was initially named Oak, inspired by an oak tree standing outside Gosling's
office.
3. In 1995, the name Oak was changed to Java because "Oak" was already a registered
trademark of another company.
4. The name Java was chosen from a list of alternatives. It was inspired by Java coffee,
which the developers were drinking while naming the language.
🔹 Purpose of Java's Creation
5. Java was originally designed for embedded systems and consumer electronics like set-
top boxes, televisions, microwaves, and remote controls.
6. It was intended to be portable, platform-independent, and network-aware, solving the
limitations of existing languages like C and C++.
7. When the World Wide Web (WWW) started growing in the mid-1990s, Java shifted its
focus to Internet-based applications, especially for building applets and web programs.
🔹 Major Java Milestones
Year Milestone
1991 Project started at Sun Microsystems by the Green Team.
1995 Java 1.0 officially released. Introduced the slogan: "Write Once, Run Anywhere (WORA)".
1996 First version of the Java Development Kit (JDK 1.0) released.
1997 Java 1.1 introduced inner classes, JDBC, and JavaBeans.
1998 Java 2 Platform launched, and Java was split into:
Java SE (Standard Edition) – for desktop applications
Java EE (Enterprise Edition) – for web and server-side apps
Java ME (Micro Edition) – for mobile and embedded devices |
| 2004 | Java 5 introduced generics, annotations, enhanced for loop. |
| 2006 | Java was made open-source under the GNU General Public License (GPL). |
| 2010 | Oracle Corporation acquired Sun Microsystems and took over Java
development. |
| 2011–2017 | Java 7 and Java 8 brought features like try-with-resources, Lambda
expressions, Streams, and functional programming. |
| 2017 | Java 9 introduced the module system. |
| 2018 onward | Java shifted to a 6-month release cycle for faster improvements. |
| 2025 | The current latest stable version is Java SE 23. |
✅ Applications of Java & OOP
Graphical User Interfaces (GUIs) – Swing, JavaFX.
Game development – using Java engines.
Web applications – using Servlets, JSP, Spring Framework.
Mobile applications – especially Android apps (which are based on Java).
Enterprise-level systems – using Java EE (like banking, healthcare).
Distributed systems – due to built-in support for networking and RMI.
✅ Why Java is Still Relevant Today
Platform-independent and runs on JVM.
Strong community and regular updates.
Used in schools, industries, and research.
Integrates easily with cloud and big data platforms.
Huge support for frameworks like Spring, Hibernate, and Maven.
Features of Java
Simple: Easy to learn with a clean syntax, similar to C/C++ but without complex features
like pointers.
Object-Oriented: Everything is based on objects and classes.
Platform Independent: Java code compiles into bytecode that runs on any system with
JVM (Java Virtual Machine).
Secure: Java programs run inside a secure sandbox with runtime checks and public-key
encryption.
Robust: Strong memory management with automatic garbage collection and exception
handling.
Multithreaded: Supports multiple threads to run concurrently, enabling better CPU
utilization.
Architecture-Neutral: Bytecode is not tied to any specific machine architecture.
Portable: Java applications can run on any platform without recompilation.
Interpreted and High Performance: Bytecode is interpreted or Just-In-Time compiled
by JVM for speed.
Distributed: Java supports networking capabilities like sockets and Remote Method
Invocation (RMI).
Dynamic: Classes can be loaded dynamically at runtime, supporting evolving
applications.
How Java Differs from C and C++
Feature Java C C++
✅ Platform-
Independent via JVM
❌ Platform-Dependent.
(Write Once, Run ❌ Platform-Dependent. Like C,
Platform Compiles to machine
Anywhere). compiles to system-specific
Dependency code specific to
Java code compiles to machine code.
OS/hardware.
bytecode, which runs on
any system with a JVM.
✅ Automatic memory
management using
❌ Manual memory ❌ Manual memory management
Memory Garbage Collection.
management using using new, delete, or standard C
Management Developers don’t
malloc(), free(), etc. functions.
manage memory
directly.
❌ No pointers (hidden
internally for security ✅ Uses pointers ✅ Supports pointers with more
Pointers and simplicity). extensively, allowing control than Java, but with
Java avoids pointer- direct memory access. potential risk.
related errors.
Feature Java C C++
❌ Does not support
multiple class
✅ Supports multiple
Multiple inheritance directly. ✅ Supports multiple inheritance
inheritance through
Inheritance Instead, uses interfaces directly through classes.
structs and functions.
to achieve multiple
inheritance behavior.
✔️Source code is
compiled into bytecode, ✔️Compiles directly to
✔️Compiles to machine code,
which is interpreted by machine code, runs
Compilation faster but not portable across
JVM. faster on the same
platforms.
Execution is slower but platform.
portable.
✅ Provides robust, ❌ Very limited exception
✅ Supports exception handling
Exception built-in exception handling, mostly uses
using try, catch, throw, similar
Handling handling using try, error codes (return -1,
to Java.
catch, finally. etc.).
✅ Provides strong
Security
security features:
Java is a programming language that was specially designed with the Internet in mind. Its ability
to create platform-independent, secure, and network-capable applications makes it ideal for
developing web-based and distributed systems. Java provides built-in support for internet
communication, enabling programs to interact over networks and run seamlessly across different
devices using the Java Virtual Machine (JVM).
1. Internet-Centric Design
Java was originally designed for the Internet. Its features such as platform
independence, built-in security, and networking APIs made it an ideal choice for building
distributed internet-based applications.
2. Applets in Early Java
o Java applets were small programs embedded in web pages and executed by web
browsers using the Java Plugin.
o Applets enabled interactive content, animations, games, and calculators in the
browser.
o Over time, applets became obsolete due to security concerns and were replaced by
HTML5, JavaScript, and modern web frameworks.
3. Networking Support
Java supports network programming through packages like java.net, enabling
communication using:
o TCP/IP sockets (for client-server interaction)
o RMI (Remote Method Invocation) – allows invoking methods on remote objects
o URL and HTTP classes – for accessing web resources
4. Security for Internet Apps
o Java uses a sandbox model for executing untrusted code (e.g., applets) securely.
o Features like bytecode verification, class loading restrictions, and access
control make Java suitable for internet environments.
5. Write Once, Run Anywhere (WORA)
Java programs are compiled into platform-independent bytecode, executed by the JVM.
This enables the same Java program to run on any system—Windows, Mac, Linux, or
mobile.
🌍 Java and the World Wide Web (WWW)
1. Applet Integration in Web Pages
o Applets were embedded in web pages using the <applet> or <object> HTML
tags.
o They offered interactive GUI elements and browser-based games.
o Modern browsers no longer support applets due to security, compatibility, and
performance issues.
2. Java for Server-Side Programming
o Java provides powerful server-side technologies such as:
Servlets – Java classes that handle requests and responses in web
applications.
JSP (Java Server Pages) – allows mixing Java with HTML to create
dynamic pages.
Spring Framework – modern enterprise-level Java framework for
building RESTful APIs and MVC apps.
3. Java EE (Enterprise Edition)
o Java EE extends the standard Java with APIs for:
Web services, database access (JDBC)
Transaction management, messaging, and scalability
o Used in banking, retail, healthcare, and other large-scale industries.
4. Dynamic Web Content
Java EE technologies help create dynamic, database-driven web applications that
respond to user input and browser requests, unlike static HTML pages.
🌐 Web Browsers – Java Connection & Evolution
1. What Are Web Browsers?
o Web browsers are applications that allow users to access websites on the World
Wide Web.
o Examples include Google Chrome, Mozilla Firefox, Microsoft Edge, Safari.
2. Browser Rendering Engines
o Browsers interpret HTML, CSS, and JavaScript to display text, images, and
interactive features.
o Example: Chrome uses Blink, Firefox uses Gecko rendering engines.
3. Java Applet Support (Historical)
o Older browsers supported Java Applets using the Java Plugin.
o These plugins have now been removed due to security vulnerabilities and lack of
support in mobile browsers.
4. Modern Alternatives to Applets
o Today, web development uses:
HTML5 Canvas, CSS3 animations
JavaScript frameworks like React, Angular, Vue.js
Backend technologies like Java Servlets, Node.js, PHP
📝 Additional Important Points
Feature Java
Platform Independence Achieved via JVM – Java code runs on any device with JVM installed.
Memory Management Automatic – handled by Java’s Garbage Collector.
Pointers Not exposed to users – avoids memory-related bugs.
Security Strong – includes bytecode verification, sandboxing, and access controls.
Networking Built-in APIs for sockets, URLs, HTTP requests, RMI.
Web Support Strong support for server-side development using Java EE, Servlets, and JSP.
Applets Historically used for interactive web content – now deprecated.
Modern Web Usage Java is mainly used in server-side backend development, not in browsers.
Overview of Java: Simple Java Program & Structure
🔸 What is Java?
Java is a high-level,general-purpose, object-oriented, and platform-independent programming
language. We can develop two types of java programs
1.standalone application
2.web applets
1. Standalone Applications
These are traditional desktop programs.
They run independently on a computer, without the need for a web browser.
Example: Calculator, Text Editor, Inventory Management Software.
A Standalone Program in Java is a self-contained application that runs independently
on a computer without requiring a web browser.
These programs are usually executed from the command line or a desktop shortcut.They
have a main() method as the entry point.They run on the Java Virtual Machine (JVM)
installed on the system.
No internet or browser is needed to run standalone applications.
2. Web Applets
These are small Java programs that run inside a web browser.
They were mainly used for adding interactive features to web pages.
Now mostly outdated due to modern web technologies.
Require a Java-enabled browser or plug-in.
An applet is a small Java program that is embedded in a web page and runs inside a
web browser using the Java Virtual Machine (JVM).Applets are used to create
interactive features on web pages (like animations, games, calculators, etc.).They are client-side
programs (executed on the user's computer, not the server).They are written using the Applet
class from the java.applet package or later using JApplet from javax.swing.
1. Simple Java Program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
✅ Explanation:
public class Hello: Defines a class named Hello.
public static void main(String[] args): The main() method – entry point of any
standalone Java program.
System.out.println(...): Prints text to the screen.
1. Simple Java Program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
public class Hello {
class: This keyword is used to declare a class, which is a basic building block in Java.
Hello: This is the name of the class. In Java, the class name should match the filename
(e.g., Hello.java).
public: This is an access modifier. It means this class can be accessed from anywhere.
{: This curly brace opens the body of the class.
🔹 A Java program must have at least one class.
public static void main(String[] args) {
This line is the main method, and it is mandatory in every standalone Java program. It's where
execution starts.
Part Meaning
public Accessible from anywhere.
static Belongs to the class, not to an object. JVM can call it without creating an object.
void Returns nothing.
main Name of the method – the starting point of execution.
String[] args An array of String arguments passed from the command line.
{ Opens the body of the main method.
System.out.println("Hello, Java!");
System: A built-in Java class from the java.lang package.
out: A static member (object) of the System class. It represents the output stream.
println(): A method that prints the message and moves to the next line.
"Hello, Java!": A string literal – the message to be displayed.
🔹 This statement will print: Hello, Java!
} (Closing Braces)
There are two closing braces in the program:
1. One for the main() method
2. One for the Hello class
They indicate the end of blocks.
Structure of a Java Program
A Java program is made up of different parts arranged in a specific order. This structure must be
followed for the program to compile and run successfully.
✅ Basic Structure Example:
java
CopyEdit
package myPackage; // optional
import java.util.Scanner; // optional
public class Hello { // class definition
public static void main(String[] args) { // main method
System.out.println("Hello Java!"); // statement
}
}
Parts of the Structure:
Part Description
Package
Optional. Used to group related classes.
Declaration
Import Statements Optional. Used to include Java built-in or user-defined packages.
Class Declaration Java code must be written inside a class.
The starting point of execution. Must be written as public static void
Main Method
main(String[] args).
Statements Individual instructions to perform tasks.
Curly Braces {} Used to define the beginning and end of class/method blocks.
Java Tokens
Tokens are the smallest units in a Java program. Java breaks a program into tokens for
compilation
Token Type Description Example from program
Keywords Reserved words with special meaning public, class, static, void, int
Identifiers Names of variables, methods, classes HelloWorld, main, args, num
Literals Fixed values assigned to variables or used directly "Hello", 10, true
Operators Symbols for operations =, +, -, *
Separators Symbols that separate code {}, (), ;, ,
Comments Explanatory text ignored by compiler // This is a comment
1. Statements in Java
What is a Statement?: A statement in Java is a complete instruction to be executed.
✨ Types of Statements:
Types of Java Statements:
Type Syntax Example Description
Declaration int a; Declares a variable
Assignment a = 10; Assigns value to a variable
Expression a = b + c; Evaluates an expression
Input/Output System.out.println("Hi"); Displays output
Control Statement if(a > 0) {...} Controls decision-making
Looping for(int i=0; i<5; i++) {...} Repeats actions
Method Call display(); Calls another method
Example:
int a = 10; // Declaration + assignment
System.out.println(a); // Output statement
4. Java Virtual Machine (JVM)
The JVM (Java Virtual Machine) is the engine that runs Java programs. It is part of the Java
Runtime Environment (JRE).
✅ Why JVM is Important:
Converts compiled Java bytecode (.class files) into machine code.
Makes Java platform-independent – the same program can run on Windows, Mac,
Linux, etc.
Manages memory and performs automatic garbage collection.
Handles security, exception handling, and multithreading.
How JVM Works (Step-by-Step):
1. You write Java code (.java file)
2. Java Compiler converts it into Bytecode (.class file)
3. JVM reads the bytecode and executes it on your system
JVM Components:
Component Function
Class Loader Loads .class files into JVM
Bytecode Verifier Checks code for security and correctness
Interpreter Converts bytecode to machine code line by line
JIT Compiler Improves performance by compiling bytecode into native code
Garbage Collector Frees up unused memory automatically