OOP
Object-Oriented Programming
- In Turkish
- nesne tabanlı programlama
In short
OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
What is OOP?
Object-oriented programming organizes a program as a collection of objects, each representing a thing or concept, such as a user, an order, or a bank account. An object combines data, called properties or fields, with behavior, called methods. Most OOP languages use classes, which are blueprints that describe what each kind of object contains and can do.
OOP is usually described through four core ideas. Encapsulation hides an object's internal details and exposes only a safe public interface, and abstraction focuses on what an object does rather than how it does it. Inheritance lets one class reuse and extend another, and polymorphism lets different objects respond to the same method call in their own way.
In OOP, a class is like the architectural plan for a house, and each object is an actual house built from that plan. All the houses share the same layout, but each has its own address and paint color. Languages such as Java, C#, Python, C++, Ruby, and TypeScript support OOP, and it is common in business applications, games, and user interface toolkits.
OOP is often contrasted with functional programming, which builds programs from pure functions and avoids changing shared data. Many modern languages mix both styles. A frequent piece of OOP advice is to prefer composition, building objects out of smaller objects, over deep inheritance hierarchies, which tend to become rigid and hard to change.
At a glance
Key takeaways
- OOP structures code around objects that combine data and behavior.
- A class is a blueprint; an object is an instance created from it.
- The four pillars are encapsulation, abstraction, inheritance, and polymorphism.
- Favoring composition over inheritance keeps designs flexible.
Example
// A class is a blueprint for creating objects
class BankAccount {
private balance = 0; // encapsulated: hidden from outside code
constructor(public owner: string) {}
deposit(amount: number): void {
if (amount <= 0) throw new Error("Amount must be positive");
this.balance += amount;
}
getBalance(): number { return this.balance; }
}
const account = new BankAccount("Ada"); // an object (instance)
account.deposit(50);Readers ask
What are the four pillars of OOP?
The four pillars are encapsulation (hiding internal details), abstraction (exposing only what matters), inheritance (reusing code from a parent class), and polymorphism (different objects responding to the same call in their own way).
What is the difference between a class and an object?
A class is a template that defines properties and methods. An object is a specific instance created from that class, with its own data.
What is the difference between OOP and functional programming?
OOP organizes code around objects that hold state and behavior together. Functional programming organizes code around pure functions and data that doesn't change; many languages, including JavaScript and Python, support both styles.
Often compared
See also
- ClassProgramming Fundamentals, p. 9A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.
- Design PatternSoftware Architecture, p. 13A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
- SOLIDSoftware Architecture, p. 42SOLID is a set of five object-oriented design principles that help developers write code that is easier to understand, extend, test, and maintain.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- MVCSoftware Architecture, p. 29MVC is an architectural pattern that splits an application into a Model for data and logic, a View for display, and a Controller that handles user input.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- ObjectProgramming Fundamentals, p. 39An object is a bundle of related data and behavior: values in named fields, often called properties, and functions that work on them, called methods.
- AbstractionProgramming Fundamentals, p. 1Abstraction is hiding the complicated details of how something works behind a simpler interface, so code can use it by knowing only what it does.
- ConstructorProgramming Fundamentals, p. 13A constructor is a special method that runs when a new object is created from a class, setting up its starting values so it is ready to use.
Spotted a mistake or something missing on this page?Suggest an edit