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

Is it possible to get the non-enumerable inherited property names of an object?

In JavaScript we have a few ways of getting the properties of an object, depending on what we want to get. 1) Object.keys(), which returns all own, enumerable properties of an object, an ECMA5 method. 2) a for...in loop, which returns all the…
dkugappi
  • 2,664
  • 5
  • 21
  • 22
129
votes
6 answers

How should I declare default values for instance variables in Python?

Should I give my class members default values like this: class Foo: num = 1 or like this? class Foo: def __init__(self): self.num = 1 In this question I discovered that in both cases, bar = Foo() bar.num += 1 is a well-defined…
int3
  • 12,861
  • 8
  • 51
  • 80
129
votes
8 answers

Function pointer to member function

I'd like to set up a function pointer as a member of a class that is a pointer to another function in the same class. The reasons why I'm doing this are complicated. In this example, I would like the output to be "1" class A { public: int f(); int…
Mike
  • 58,961
  • 76
  • 175
  • 221
127
votes
8 answers

Classes vs. Functions

What is the difference between functional programming and object oriented programming? How should one decide what kind of programming paradigm should be chosen? what are the benefits of one over the other ? Functions are easy to understand even for…
multigoodverse
  • 7,638
  • 19
  • 64
  • 106
126
votes
10 answers

What is the purpose of static methods? How do I know when to use one?

I ran into unbound method error in python with this code: import random class Sample(object): def drawSample(samplesize, List): sample = random.sample(List, samplesize) return…
Curious2learn
  • 31,692
  • 43
  • 108
  • 125
126
votes
45 answers

What's the point of OOP?

As far as I can tell, in spite of the countless millions or billions spent on OOP education, languages, and tools, OOP has not improved developer productivity or software reliability, nor has it reduced development costs. Few people use OOP in any…
DrPizza
  • 17,882
  • 7
  • 41
  • 53
125
votes
25 answers

What is the difference between an Instance and an Object?

What is the difference between an Instance and an Object? Is there a difference or not?
streetparade
  • 32,000
  • 37
  • 101
  • 123
125
votes
5 answers

Why not abstract fields?

Why can't Java classes have abstract fields like they can with abstract methods? For example: I have two classes that extend the same abstract base class. These two classes each have a method that is identical except for a String constant, which…
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
125
votes
11 answers

What does the variable $this mean in PHP?

I see the variable $this in PHP all the time and I have no idea what it's used for. I've never personally used it. Can someone tell me how the variable $this works in PHP?
waiwai933
  • 14,133
  • 21
  • 62
  • 86
124
votes
6 answers

JavaScript OOP in NodeJS: how?

I am used to the classical OOP as in Java. What are the best practices to do OOP in JavaScript using NodeJS? Each Class is a file with module.export? How to create Classes? this.Class = function() { //constructor? var privateField = "" …
fusio
  • 3,595
  • 6
  • 33
  • 47
123
votes
10 answers

Creating an empty object in Python

Are there any shortcuts for defining an empty object in Python or do you always have to create an instance of a custom empty class? Edit: I mean an empty object usable for duck typing.
Smiles
  • 1,733
  • 2
  • 11
  • 13
123
votes
9 answers

What is a covariant return type?

What is a covariant return type in Java? In object-oriented programming in general?
Pops
  • 30,199
  • 37
  • 136
  • 151
122
votes
8 answers

Why does PHP 5.2+ disallow abstract static class methods?

After enabling strict warnings in PHP 5.2, I saw a load of strict standards warnings from a project that was originally written without strict warnings: Strict Standards: Static function Program::getSelectSQL() should not be abstract in…
Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
122
votes
2 answers

Overriding class constants vs properties

I would like to better understand why, in the scenario below, there is a difference in the way class constants are inherited vs. instance variables.
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
122
votes
11 answers

Why is the clone() method protected in java.lang.Object?

What is the specific reason that clone() is defined as protected in java.lang.Object?
Alex N.
  • 14,805
  • 10
  • 46
  • 54