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
188
votes
20 answers

What is "loose coupling?" Please provide examples

I can't seem to grok the concept of "loose coupling." I suppose it doesn't help that the word "loose" usually has a negative connotation, so I always forget that loose coupling is a good thing. Will somebody please show some "before" and "after"…
trebormf
  • 3,156
  • 3
  • 25
  • 21
188
votes
20 answers

Method chaining - why is it a good practice, or not?

Method chaining is the practice of object methods returning the object itself in order for the result to be called for another method. Like this: participant.addSchedule(events[1]).addSchedule(events[2]).setStatus('attending').save() This seems to…
Ilari Kajaste
  • 3,207
  • 2
  • 23
  • 25
187
votes
15 answers

Class with single method -- best approach?

Say I have a class that's meant to perform a single function. After performing the function, it can be destroyed. Is there any reason to prefer one of these approaches? // Initialize arguments in constructor MyClass myObject = new MyClass(arg1,…
JW.
  • 50,691
  • 36
  • 115
  • 143
185
votes
12 answers

Is the PIMPL idiom really used in practice?

I am reading the book "Exceptional C++" by Herb Sutter, and in that book I have learned about the PIMPL idiom. Basically, the idea is to create a structure for the private objects of a class and dynamically allocate them to decrease the compilation…
Renan Greinert
  • 3,376
  • 3
  • 19
  • 29
183
votes
12 answers

Get PHP class property by string

How do I get a property in a PHP based on a string? I'll call it magic. So what is magic? $obj->Name = 'something'; $get = $obj->Name; would be like... magic($obj, 'Name', 'something'); $get = magic($obj, 'Name');
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
182
votes
15 answers

Why should I avoid multiple inheritance in C++?

Is it a good concept to use multiple inheritance or can I do other things instead?
Hai
  • 4,764
  • 8
  • 29
  • 26
179
votes
18 answers

Can I extend a class using more than 1 class in PHP?

If I have several classes with functions that I need but want to store separately for organisation, can I extend a class to have both? i.e. class a extends b extends c edit: I know how to extend classes one at a time, but I'm looking for a method to…
atomicharri
  • 2,445
  • 5
  • 22
  • 23
177
votes
5 answers

When to use nested classes and classes nested in modules?

I'm pretty familiar with when to use subclasses and modules, but more recently I've been seeing nested classes like this: class Foo class Bar # do some useful things end end As well as classes nested in modules like so: module Baz class…
cmhobbs
  • 2,469
  • 3
  • 24
  • 30
177
votes
23 answers

Object-orientation in C

What would be a set of nifty preprocessor hacks (ANSI C89/ISO C90 compatible) which enable some kind of ugly (but usable) object-orientation in C? I am familiar with a few different object-oriented languages, so please don't respond with answers…
anon
177
votes
9 answers

How do you effectively model inheritance in a database?

What are the best practices for modeling inheritance in databases? What are the trade-offs (e.g. queriability)? (I'm most interested in SQL Server and .NET, but I also want to understand how other platforms address this issue.)
Even Mien
  • 44,393
  • 43
  • 115
  • 119
174
votes
10 answers

When to use static vs instantiated classes

PHP is my first programming language. I can't quite wrap my head around when to use static classes vs instantiated objects. I realize that you can duplicate and clone objects. However in all of my time using php any object or function always ended…
user73119
  • 2,413
  • 3
  • 21
  • 21
172
votes
4 answers

How do I initialize the base (super) class?

In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x = x class SubClass(SuperClass): def __init__(self, y): self.y = y # how do I initialize the SuperClass…
Jeremy
  • 1,771
  • 2
  • 11
  • 4
172
votes
5 answers

What is the __del__ method and how do I call it?

I saw a class in which a __del__ method is defined. This method is used to destroy an instance of the class. However, I cannot find a place where this method is used. How is this method used? Like that: obj1.del()?. How do I call the __del__ method?
Verrtex
  • 4,317
  • 9
  • 34
  • 33
171
votes
6 answers

Understanding prototypal inheritance in JavaScript

I am new to JavaScript OOP. Can you please explain the difference between the following blocks of code? I tested and both blocks work. What's the best practice and why? First block: function Car(name){ this.Name =…
Dasha Salo
  • 5,159
  • 5
  • 26
  • 28
171
votes
17 answers

Java Multiple Inheritance

In an attempt to fully understand how to solve Java's multiple inheritance problems I have a classic question that I need clarified. Lets say I have class Animal this has sub classes Bird and Horse and I need to make a class Pegasus that extends…