Object-Oriented Programming
Definition
OOP or Object-Oriented Programming, is a programming model that revolves around the concept of objects. In simple terms, objects can be thought of as real-world entities with specific properties and behavior. An object is an instance of a class, which can be considered as a blueprint or template for creating objects.
C++, Java, JavaScript, C#, PHP, Python
and more are among the programming languages that offer support for Object-Oriented Programming (OOP) concepts.
Define Object and Class
Object: A key idea in object-oriented programming (OOP) is an object, which represents a real-world thing with unique properties and behaviours. Objects can perform actions (behaviours or methods), which define their functionality, and they can contain state (properties or attributes), which describes their qualities.
Class: In object-oriented programming (OOP), a class is a key idea that acts as a model or template for building objects. It describes the typical attributes and characteristics that objects of the same kind will share.
Object-Oriented Programming benefits
- Reusability: Classes and inheritance enable code reuse.
- Modularity: Code is organized into self-contained units (classes) for easier maintenance and scalability.
- Readability: Objects and classes model real-world entities, enhancing code comprehension.
- Encapsulation: Data and behavior are bundled together, promoting information hiding.
Real world example
- Java
- Other
class BankAccount {
String account_holder_name;
String account_number;
double balance;
BankAccount(String account_holder_name, String account_number, double initial_balance) {
this.account_holder_name = account_holder_name;
this.account_number = account_number;
this.balance = initial_balance;
System.out.println("Open account: "+account_number+" for "+account_holder_name+" with "+initial_balance+" Rs.");
}
double getAccountBalance() {
return balance;
}
void creditMoney(double amount) {
balance += amount;
System.out.println("Credited " + amount + " to the account.");
}
void debitMoney(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Debited " + amount + " from the account.");
} else {
System.out.println("Insufficient funds. Cannot debit " + amount + " from the account.");
}
}
}
public class Main {
public static void main(String[] argv) {
// Create a new bank account
BankAccount account = new BankAccount("Nitesh Kumar", "123456789", 1000.0);
// Get account balance
double balance = account.getAccountBalance();
System.out.println("Account balance: " + balance);
// Credit money to the account
account.creditMoney(500.0);
// Debit money from the account
account.debitMoney(200.0);
// Get updated account balance
balance = account.getAccountBalance();
System.out.println("Updated account balance: " + balance);
}
}
currently no other languages supported
Output
Open account: 123456789 for Nitesh Kumar with 1000.0 Rs.
Account balance: 1000.0
Credited 500.0 to the account.
Debited 200.0 from the account.
Updated account balance: 1300.0
Object-Oriented Principles
- Inheritance
- Encapsulation
- Polymorphism
- Data Abstraction