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
336
votes
16 answers

When should I use a struct instead of a class?

MSDN says that you should use structs when you need lightweight objects. Are there any other scenarios when a struct is preferable over a class? Some people might have forgotten that: structs can have methods. structs cannot be inherited. I…
Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
336
votes
6 answers

When should I be using classes in Python?

I have been programming in python for about two years; mostly data stuff (pandas, mpl, numpy), but also automation scripts and small web apps. I'm trying to become a better programmer and increase my python knowledge and one of the things that…
metersk
  • 11,803
  • 21
  • 63
  • 100
331
votes
3 answers

Advantages of using prototype, vs defining methods straight in the constructor?

I am wondering if there are any advantages of using any of these over the other, and which way should I go? Constructor approach: var Class = function () { this.calc = function (a, b) { return a + b; }; }; Prototype approach: var…
Leo
  • 3,822
  • 3
  • 21
  • 26
328
votes
14 answers

How to check whether a variable is a class or not?

I was wondering how to check whether a variable is a class (not an instance!) or not. I've tried to use the function isinstance(object, class_or_type_or_tuple) to do this, but I don't know what type a class would have. For example, in the following…
jeeyoungk
  • 3,315
  • 2
  • 18
  • 6
328
votes
8 answers

Explanation of the UML arrows

I have recently been studying UML and drawing simple diagrams with ordinary plain arrows between classes, but I know it's not enough. There are plenty of other arrows: generalization, realisation and etc. which have meaning to the diagram…
faya
  • 5,535
  • 11
  • 37
  • 49
321
votes
20 answers

Python function overloading

I know that Python does not support method overloading, but I've run into a problem that I can't seem to solve in a nice Pythonic way. I am making a game where a character needs to shoot a variety of bullets, but how do I write different functions…
Bullets
  • 3,251
  • 3
  • 15
  • 6
317
votes
14 answers

Why is it necessary to set the prototype constructor?

In the section about inheritance in the MDN article Introduction to Object Oriented Javascript, I noticed they set the prototype.constructor: // correct the constructor pointer because it points to Person Student.prototype.constructor = Student; …
trinth
  • 5,919
  • 9
  • 40
  • 45
317
votes
5 answers

What does it mean to hydrate an object?

When someone talks about hydrating an object, what does that mean? I see a Java project called Hydrate on the web that transforms data between different representations (RDMS to OOPS to XML). Is this the general meaning of object hydration; to…
Jim
  • 3,173
  • 2
  • 15
  • 4
316
votes
23 answers

How to compare objects by multiple fields

Assume you have some objects which have several fields they can be compared by: public class Person { private String firstName; private String lastName; private String age; /* Constructors */ /* Methods */ } So in this…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
312
votes
15 answers

How do you create a static class in C++?

How do you create a static class in C++? I should be able to do something like: cout << "bit 5 is " << BitParser::getBitAt(buffer, 5) << endl; Assuming I created the BitParser class. What would the BitParser class definition look like?
andrewrk
  • 30,272
  • 27
  • 92
  • 113
312
votes
12 answers

difference between variables inside and outside of __init__() (class and instance attributes)

Is there any difference at all between these classes besides the name? class WithClass (): def __init__(self): self.value = "Bob" def my_func(self): print(self.value) class WithoutClass (): value = "Bob" def…
Teifion
  • 108,121
  • 75
  • 161
  • 195
304
votes
8 answers

How to get the parents of a Python class?

How can I get the parent class(es) of a Python class?
John Smith
  • 12,491
  • 18
  • 65
  • 111
303
votes
19 answers

Using property() on classmethods

I have a class with two class methods (using the classmethod() function) for getting and setting what is essentially a static variable. I tried to use the property() function with these, but it results in an error. I was able to reproduce the…
Mark Roddy
  • 27,122
  • 19
  • 67
  • 71
297
votes
3 answers

Chain-calling parent initialisers in python

Consider this - a base class A, class B inheriting from A, class C inheriting from B. What is a generic way to call a parent class initialiser in an initialiser? If this still sounds too vague, here's some code. class A(object): def…
shylent
  • 10,076
  • 6
  • 38
  • 55
297
votes
11 answers

Constructors in Go

I have a struct and I would like it to be initialised with some sensible default values. Typically, the thing to do here is to use a constructor but since go isn't really OOP in the traditional sense these aren't true objects and it has no…
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200