Object-Oriented Programming
Object-Oriented Thinking
Object-Oriented Programming (OOP) conceptualizes solving problems by identifying and utilizing objects, representing entities from the real world. This approach aligns closely with human cognitive processes, thus simplifying coding and enhancing program comprehensibility. The term "object-oriented" implies a focus on these entities to address various programming challenges.
Key Concepts: Class and Object
- Class: A class is essentially a blueprint or a template that describes the characteristics and behaviors shared by a group of similar objects. It defines the structure of the objects by specifying their attributes (properties) and methods (functions or behaviors).
- Object: An object is an instance of a class. It represents a specific example or occurrence of the class. In programming languages like Java, objects are created from classes.
Defining a Class
To define a class in Java, you follow a structured format:
public class ClassName {
// Attributes (properties)
DataType attributeName = initialValue;
// Methods (behaviors)
ReturnType methodName(Parameters) {
// Method body
}
// Constructor(s)
// Other class components like inner classes, code blocks
}
Precautions When Defining a Class:
- Class names should begin with a capital letter, adhere to camel case notation, avoid using reserved keywords, and be meaningful.
- A Java file can contain multiple classes, but only one can be public. The public class must share its name with the filename.
Example: Car Class
Here’s how a simple class might look using a car as an example:
public class Car {
private String name;
private double price;
public void start() {
System.out.println(name + " with price " + price + " startup successful.");
}
public void run() {
System.out.println(name + " with price " + price + " is running.");
}
}
Constructors
Constructors are special methods used to initialize new objects. They can be parameterized or no-argument.
Types of Constructors
- No-Argument Constructor: Initializes an object with default settings.
- Parameterized Constructor: Allows initialization of an object with specific values.
Usage of Constructors
Car myCar = new Car(); // Using no-argument constructor
The 'this' Keyword
In Java, this
refers to the current object instance. It is often used in constructors and methods to distinguish class fields from parameters with the same name.
Example Using 'this':
public Car(String name, double price) {
this.name = name;
this.price = price;
}
Encapsulation
Encapsulation is a fundamental concept in OOP that involves bundling the data (attributes) and code (methods) that operates on the data into a single unit or class and restricting access to some of the object's components. This is achieved by making attributes private and providing public getter and setter methods to modify and view the values.
JavaBean Standard
A JavaBean is a standard entity class example in Java which encapsulates several components:
- Private Members: Attributes are private to enforce encapsulation.
- Getters and Setters: Methods to get and set the values of the attributes.
- No-Argument Constructor: Essential for creating instances without setting properties initially.
Through detailed organization and structuring, OOP enables developers to create modular, reusable, and more manageable code.