Questions tagged [oop]

Object-oriented programming is a programming paradigm using "objects": an encapsulation consisting of data fields and methods together with their interactions.

OOP

Object-oriented programming (OOP) is a programming paradigm using the concept of objects. These encapsulates data which is organized in the form of fields (often known as attributes) and code, in the form of procedures (often known as methods). Methods can access and often modify attributes of the object with which they are associated. In OOP, computer programs are designed based on having objects interacting among each other.

OOP includes features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance. Many modern programming languages now support OOP.

FAQs

  1. Interface vs Base class
  2. Prefer composition over inheritance?
  3. Polymorphism vs Overriding vs Overloading
  4. What is a class in PHP?
  5. What's the point of OOP?
  6. Inheritance vs. Aggregation
  7. Object-orientation in C
  8. What are the differences between struct and class in C++?
  9. Interface vs Abstract Class (general OO)
  10. What's the difference between a method and a function?
  11. What is the difference between an interface and abstract class?
  12. What is the difference between an abstract function and a virtual function?
  13. What is the difference between public, private, and protected?
  14. Functional programming vs Object Oriented programming
  15. Difference between abstraction and encapsulation?
  16. How do you design object oriented projects?
  17. Difference Between Cohesion and Coupling
  18. prototype based vs. class based inheritance
  19. Aspect Oriented Programming vs. Object-Oriented Programming
  20. What is polymorphism, what is it for, and how is it used?
61861 questions
231
votes
13 answers

DDD - the rule that Entities can't access Repositories directly

In Domain Driven Design, there seems to be lots of agreement that Entities should not access Repositories directly. Did this come from Eric Evans Domain Driven Design book, or did it come from elsewhere? Where are there some good explanations for…
codeulike
  • 22,514
  • 29
  • 120
  • 167
230
votes
7 answers

Aspect Oriented Programming vs. Object-Oriented Programming

Like most developers here and in the entire world, I have been developing software systems using object-oriented programming (OOP) techniques for many years. So when I read that aspect-oriented programming (AOP) addresses many of the problems that…
yeradis
  • 5,235
  • 5
  • 25
  • 26
229
votes
6 answers

How can I access "static" class variables within methods?

If I have the following code: class Foo(object): bar = 1 def bah(self): print(bar) f = Foo() f.bah() It complains NameError: global name 'bar' is not defined How can I access class/static variable bar within method…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
229
votes
10 answers

How do I declare class-level properties in Objective-C?

Maybe this is obvious, but I don't know how to declare class properties in Objective-C. I need to cache per-class a dictionary and wonder how put it in the class.
mamcx
  • 15,916
  • 26
  • 101
  • 189
229
votes
5 answers

Difference between Repository and Service Layer?

In OOP Design Patterns, what is the difference between the Repository Pattern and a Service Layer? I am working on an ASP.NET MVC 3 app, and am trying to understand these design patterns, but my brain is just not getting it...yet!!
Sam
  • 15,336
  • 25
  • 85
  • 148
228
votes
13 answers

Why is the STL so heavily based on templates instead of inheritance?

I mean, aside from its name the Standard Template Library (which evolved into the C++ standard library). C++ initially introduce OOP concepts into C. That is: you could tell what a specific entity could and couldn't do (regardless of how it does it)…
OB OB
  • 3,289
  • 6
  • 21
  • 16
224
votes
12 answers

String, StringBuffer, and StringBuilder

Please tell me a real time situation to compare String, StringBuffer, and StringBuilder?
JavaUser
  • 25,542
  • 46
  • 113
  • 139
223
votes
22 answers

Why does C# not provide the C++ style 'friend' keyword?

The C++ friend keyword allows a class A to designate class B as its friend. This allows Class B to access the private/protected members of class A. I've never read anything as to why this was left out of C# (and VB.NET). Most answers to this…
Ash
  • 60,973
  • 31
  • 151
  • 169
222
votes
16 answers

What is the dependency inversion principle and why is it important?

What is the dependency inversion principle and why is it important?
220
votes
10 answers

Why do you need explicitly have the "self" argument in a Python method?

When defining a method on a class in Python, it looks something like this: class MyClass(object): def __init__(self, x, y): self.x = x self.y = y But in some other languages, such as C#, you have a reference to the object that…
readonly
  • 343,444
  • 107
  • 203
  • 205
214
votes
10 answers

Constructors vs Factory Methods

When modelling classes, what is the preferred way of initializing: Constructors, or Factory Methods And what would be the considerations for using either of them? In certain situations, I prefer having a factory method which returns null if the…
Hemanshu Bhojak
  • 16,972
  • 16
  • 49
  • 64
213
votes
22 answers

Creating the Singleton design pattern in PHP5

How would one create a Singleton class using PHP5 classes?
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
212
votes
24 answers

How will I know when to create an interface?

I'm at a point in my development learning where I feel like I must learn more about interfaces. I frequently read about them but it just seems like I cannot grasp them. I've read examples like: Animal base class, with IAnimal interface for things…
user53885
  • 3,809
  • 11
  • 33
  • 43
209
votes
25 answers

Using the "final" modifier whenever applicable in Java

In Java, there is a practice of declaring every variable (local or class), parameter final if they really are. Though this makes the code a lot more verbose, this helps in easy reading/grasping of the code and also prevents mistakes as the intention…
surajs
  • 2,342
  • 2
  • 14
  • 7
206
votes
8 answers

How to declare Return Types for Functions in TypeScript

I checked here https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md which is the TypeScript Language Specifications but I couldn't find how I can declare a return type of the function. I showed what I was expecting in the code below:…
Tarik
  • 79,711
  • 83
  • 236
  • 349