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
147
votes
11 answers

Why should the "PIMPL" idiom be used?

Backgrounder: The PIMPL Idiom (Pointer to IMPLementation) is a technique for implementation hiding in which a public class wraps a structure or class that cannot be seen outside the library the public class is part of. This hides internal…
JeffV
  • 52,985
  • 32
  • 103
  • 124
147
votes
10 answers

In PHP, can you instantiate an object and call a method on the same line?

What I would like to do is something like this: $method_result = new Obj()->method(); Instead of having to do: $obj = new Obj(); $method_result = $obj->method(); The result doesn't actually matter to me in my specific case. But, is there a way to…
weotch
  • 5,788
  • 6
  • 35
  • 42
146
votes
13 answers

Encapsulation vs Abstraction?

Here are the brief definitions of encapsulation and abstraction. Abstraction: The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the…
M Sach
  • 33,416
  • 76
  • 221
  • 314
146
votes
14 answers

What is the difference between dynamic and static polymorphism in Java?

Can anyone provide a simple example that explains the difference between Dynamic and Static polymorphism in Java?
Prabhakar Manthena
  • 2,223
  • 3
  • 16
  • 30
146
votes
3 answers

What does it mean to start a PHP function with an ampersand?

I'm using the Facebook library with this code in it: class FacebookRestClient { ... public function &users_hasAppPermission($ext_perm, $uid=null) { return $this->call_method('facebook.users.hasAppPermission', array('ext_perm' =>…
Alex Mcp
  • 19,037
  • 12
  • 60
  • 93
146
votes
21 answers

Must Dependency Injection come at the expense of Encapsulation?

If I understand correctly, the typical mechanism for Dependency Injection is to inject either through a class' constructor or through a public property (member) of the class. This exposes the dependency being injected and violates the OOP principle…
urig
  • 16,016
  • 26
  • 115
  • 184
145
votes
12 answers

Typescript: How to extend two classes?

I want to save my time and reuse common code across classes that extend PIXI classes (a 2d webGl renderer library). Object Interfaces: module Game.Core { export interface IObject {} export interface IManagedObject extends IObject{ …
Vadorequest
  • 16,593
  • 24
  • 118
  • 215
144
votes
7 answers

Is there a benefit to defining a class inside another class in Python?

What I'm talking about here are nested classes. Essentially, I have two classes that I'm modeling. A DownloadManager class and a DownloadThread class. The obvious OOP concept here is composition. However, composition doesn't necessarily mean…
fuentesjr
  • 50,920
  • 27
  • 77
  • 81
144
votes
22 answers

Difference between Encapsulation and Abstraction

I had an interview today. I had a question from OOP, about the difference between Encapsulation & Abstraction? I replied to my knowledge that Encapsulation is basically binding data members & member functions into a single unit called Class. Whereas…
WpfBee
  • 2,837
  • 6
  • 23
  • 29
142
votes
11 answers

Is there a use-case for singletons with database access in PHP?

I access my MySQL database via PDO. I'm setting up access to the database, and my first attempt was to use the following: The first thing I thought of is global: $db = new PDO('mysql:host=127.0.0.1;dbname=toto', 'root', 'pwd'); function…
seriousdev
  • 7,519
  • 8
  • 45
  • 52
142
votes
11 answers

Nested or Inner Class in PHP

I'm building a User Class for my new website, however this time I was thinking to build it little bit differently... C++, Java and even Ruby (and probably other programming languages) are allowing the use of nested/inner classes inside the main…
Lior Elrom
  • 19,660
  • 16
  • 80
  • 92
142
votes
24 answers

Is returning null bad design?

I've heard some voices saying that checking for a returned null value from methods is bad design. I would like to hear some reasons for this. pseudocode: variable x = object.method() if (x is null) do something
koen
  • 13,349
  • 10
  • 46
  • 51
141
votes
23 answers

What is the use of making constructor private in a class?

Why should we make the constructor private in class? As we always need the constructor to be public.
giri
  • 26,773
  • 63
  • 143
  • 176
141
votes
10 answers

Best practice: ordering of public/protected/private within the class definition?

I am starting a new project from the ground up and want it to be clean / have good coding standards. In what order do the seasoned developers on here like to lay things out within a class? A : 1) public methods 2) private methods 3) public vars 4)…
tempname
  • 1,413
  • 2
  • 10
  • 5
140
votes
7 answers

Modelling an elevator using Object-Oriented Analysis and Design

There are a set of questions that seem to be commonly-used in interviews and classes when it comes to object-oriented design and analysis. This is one of them; unfortunately, my OOP professor in college never actually gave an answer to it, and so…
Kate Bertelsen
  • 3,900
  • 5
  • 24
  • 18