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
202
votes
9 answers

What is difference between functional and imperative programming languages?

Most of the mainstream languages, including object-oriented programming (OOP) languages such as C#, Visual Basic, C++, and Java were designed to primarily support imperative (procedural) programming, whereas Haskell/gofer like languages are purely…
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92
201
votes
10 answers

Differences between Proxy and Decorator Pattern

Can you give any good explanation what is the difference between Proxy and Decorator? The main difference I see is that when we assume that Proxy uses composition and Decorator uses aggregation then it seems to be clear that by using multiple (one…
Łukasz Rzeszotarski
  • 5,791
  • 6
  • 37
  • 68
201
votes
5 answers

What is the difference between self::$bar and static::$bar in PHP?

What is the difference between using self and static in the example below? class Foo { protected static $bar = 1234; public static function instance() { echo self::$bar; echo "\n"; echo static::$bar; …
cwd
  • 53,018
  • 53
  • 161
  • 198
200
votes
12 answers

Mutable vs immutable objects

I'm trying to get my head around mutable vs immutable objects. Using mutable objects gets a lot of bad press (e.g. returning an array of strings from a method) but I'm having trouble understanding what the negative impacts are of this. What are the…
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
199
votes
18 answers

What is the main difference between Inheritance and Polymorphism?

I was presented with this question in an end of module open book exam today and found myself lost. I was reading Head first Javaand both definitions seemed to be exactly the same. I was just wondering what the MAIN difference was for my own piece of…
Darren Burgess
  • 4,200
  • 6
  • 27
  • 43
198
votes
20 answers

Struct like objects in Java

Is it completely against the Java way to create struct like objects? class SomeData1 { public int x; public int y; } I can see a class with accessors and mutators being more Java like. class SomeData2 { int getX(); void setX(int…
Chris de Vries
  • 56,777
  • 5
  • 32
  • 27
196
votes
1 answer

Does ECMAScript 6 have a convention for abstract classes?

I was surprised that I couldn't find anything about abstract classes when reading up on ES6. (By "abstract class" I'm talking about the Java meaning of it, in which an abstract class declares method signatures that a subclass must implement in order…
obelia
  • 2,105
  • 2
  • 13
  • 11
194
votes
5 answers

When to use 'raise NotImplementedError'?

Is it to remind yourself and your team to implement the class correctly? I don't fully get the use of an abstract class like this: class RectangularRoom(object): def __init__(self, width, height): raise NotImplementedError def…
Antonio
  • 2,848
  • 2
  • 15
  • 15
192
votes
8 answers

Constructor function vs Factory functions

Can someone clarify the difference between a constructor function and a factory function in Javascript. When to use one instead of the other?
Sinan
  • 5,819
  • 11
  • 39
  • 66
192
votes
21 answers

Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

Java doesn't allow multiple inheritance, but it allows implementing multiple interfaces. Why?
abson
  • 9,148
  • 17
  • 50
  • 69
191
votes
13 answers

Missing return statement in a non-void method compiles

I encountered a situation where a non-void method is missing a return statement and the code still compiles. I know that the statements after the while loop are unreachable (dead code) and would never be executed. But why doesn't the compiler even…
c.P.u1
  • 16,664
  • 6
  • 46
  • 41
191
votes
7 answers

List all base classes in a hierarchy of given class?

Given a class Foo (whether it is a new-style class or not), how do you generate all the base classes - anywhere in the inheritance hierarchy - it issubclass of?
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
189
votes
10 answers

PHP method chaining or fluent interface?

I am using PHP 5 and I've heard of a new featured in the object-oriented approach, called 'method chaining'. What is it exactly? How do I implement it?
Sanjay Khatri
  • 4,153
  • 7
  • 38
  • 42
189
votes
6 answers

Checking if an instance's class implements an interface?

Given a class instance, is it possible to determine if it implements a particular interface? As far as I know, there isn't a built-in function to do this directly. What options do I have (if any)?
Wilco
  • 32,754
  • 49
  • 128
  • 160
188
votes
7 answers

What does "program to interfaces, not implementations" mean?

One stumbles upon this phrase when reading about design patterns. But I don't understand it, could someone explain this for me?
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383