Object-Oriented Programming (OOP)
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or methods that perform
operations on the data, while object-oriented programming is about creating objects
that contain both data and methods. OOP is a programming style that focuses on
creating programs using objects rather than procedures or functions.
It models software based on real-world entities, making the structure of programs more
natural and easier to understand.
Java is designed as an object-oriented language because it uses objects to represent and
solve problems.
Object-oriented programming has several advantages over procedural programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code
easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter
development time
Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You
should extract out the codes that are common for the application, and place them at a
single place and reuse them instead of repeating it.
Java Classes/Objects
1. Java is an object-oriented programming language.
2. Everything in Java is associated with classes and objects, along with its
attributes and methods. For example: in real life, a car is an object.
The car has attributes, such as weight and color, and methods, such
as drive and brake.
3. A Class is like an object constructor, or a "blueprint" for creating
objects.
4.
2. The String Class as an Example
A String object stores characters and information such as its length.
The user can perform operations like converting to uppercase, extracting substrings, or
replacing characters.
The internal working of these operations is hidden from the user.
3. Encapsulation
Encapsulation is a fundamental principle of object-oriented design.
It means combining data and methods into a single unit called an object.
The internal details of how methods work are hidden from the user.
Only the public interface (methods) is visible.
4. Real-Life Example: VCR
A VCR’s internal parts such as circuits and mechanisms are hidden from the user — this
is information hiding.
The user only interacts with buttons like Play, Stop, Record, and Pause — this is the
public interface.
A VCR can be used alone or as part of a home entertainment system — showing
modularity.
The functions of the buttons always remain the same — showing consistent behavior.
5. Java Objects Work Like Real-World Objects
Java objects hide their internal code and only expose usable methods.
Programmers use objects without knowing how methods are coded internally.
This simplifies programming and protects the internal state of the object.
6. Aim of Object-Oriented Design
The goal of OOD is to create software from reusable components called classes.
A programmer only needs to know the methods of a class, not the internal
implementation.
OOD improves reusability, readability, and flexibility in software development.
7. Identifying Objects
The first step in OOD is to identify the objects involved in solving a problem.
Each object is represented as a class in the program.
8. Identifying Data for Each Object
Every object must store some essential data.
o For a String object: the character sequence and the length.
o For a JButton object: the label text, size, and screen position.
9. Identifying Operations (Methods)
After identifying the data, the next step is to identify the operations needed for that
object.
For a String object: methods like replace(), substring(), toUpperCase(), toLowerCase().
For a JButton object: setText(), setSize(), setLocation(), addActionListener().
10. Objects Combine Data and Behavior
An object contains both the data (attributes) and the behavior (methods) that operate
on that data.
This combination models real-world entities in a natural and understandable way.
11. GUI Objects as Examples of OOD
In GUI programming, objects such as JButton, JText Field, and JLabel follow OOD
principles.
These objects contain:
o Data: label, position, size
o Methods: change label, move component, add event listeners
12. Benefits of OOD
Software becomes modular — each class is a complete, independent unit.
Code can be reused easily across different projects.
Maintenance becomes simpler, as updating one class does not affect others directly.
Large programs can be broken into smaller, manageable parts. import [Link];
Program
public class Student {
String name;
int age;
double marks;
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
Student s = new Student(); // object
[Link]("Enter name: ");
[Link] = [Link]()
[Link]("Enter age: ");
[Link] = [Link]()
[Link]("Enter marks: ");
[Link] = [Link]();
[Link]("\n--- Student Details ---");
[Link]("Name: " + [Link]);
[Link]("Age: " + [Link]);
[Link]("Marks: " + [Link]);