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
297
votes
15 answers

Abstract class in Java

What is an "abstract class" in Java?
keyur
296
votes
5 answers

Benefits of prototypal inheritance over classical?

So I finally stopped dragging my feet all these years and decided to learn JavaScript “properly”. One of the most head-scratching elements of the languages design is its implementation of inheritance. Having experience in Ruby, I was really happy…
287
votes
21 answers

Do subclasses inherit private fields?

This is an interview question. Does subclasses inherit private fields? I answered "No", because we can't access them using the "normal OOP way". But the interviewer thinks that they are inherited, because we can access such fields indirectly or…
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
282
votes
8 answers

What does it mean that Javascript is a prototype based language?

One of the major advantages with Javascript is said to be that it is a prototype based language. But what does it mean that Javascript is prototype based, and why is that an advantage?
Jonas Pegerfalk
  • 9,016
  • 9
  • 29
  • 29
281
votes
11 answers

Understanding __getitem__ method in Python

I have gone through most of the documentation of __getitem__() in the Python docs, but I am still unable to grasp the meaning of it. So all I can understand is that __getitem__() is used to implement calls like self[key]. But what is the use of…
Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
274
votes
15 answers

Class vs. static method in JavaScript

I know this will work: function Foo() {}; Foo.prototype.talk = function () { alert('hello~\n'); }; var a = new Foo; a.talk(); // 'hello~\n' But if I want to call Foo.talk() // this will not work Foo.prototype.talk() // this works correctly I…
lostyzd
  • 4,515
  • 3
  • 19
  • 33
274
votes
7 answers

What is the difference between aggregation, composition and dependency?

What is the difference between aggregation, composition and dependency?
sevugarajan
  • 9,717
  • 12
  • 34
  • 31
272
votes
27 answers

Is it bad practice to make a setter return "this"?

Is it a good or bad idea to make setters in java return "this"? public Employee setName(String name){ this.name = name; return this; } This pattern can be useful because then you can chain setters like this: list.add(new…
Ken Liu
  • 22,503
  • 19
  • 75
  • 98
269
votes
4 answers

Understanding the difference between __getattr__ and __getattribute__

I am trying to understand the difference between __getattr__ and __getattribute__, however, I am failing at it. The answer to the Stack Overflow question Difference between __getattr__ vs __getattribute__ says: __getattribute__ is invoked before…
user225312
  • 126,773
  • 69
  • 172
  • 181
267
votes
15 answers

How do I get a PHP class constructor to call its parent's parent's constructor?

I need to have a class constructor in PHP call its parent's parent's (grandparent?) constructor without calling the parent constructor. // main class that everything inherits class Grandpa { public function __construct() { } } class…
Paulo
  • 4,275
  • 2
  • 20
  • 20
264
votes
9 answers

Why should I prefer to use member initializer lists?

I'm partial to using member initializer lists for my constructors, but I've long since forgotten the reasons behind this. Do you use member initializer lists in your constructors? If so, why? If not, why not?
oz10
  • 153,307
  • 27
  • 93
  • 128
257
votes
4 answers

Difference between virtual and abstract methods

Here is some code from MSDN: // compile with: /target:library public class D { public virtual void DoWork(int i) { // Original implementation. } } public abstract class E : D { public abstract override void DoWork(int…
iJade
  • 23,144
  • 56
  • 154
  • 243
253
votes
6 answers

Object Oriented Javascript best practices?

I'm finding myself coding a big project in Javascript. I remember the last one was quite an adventure because hacky JS can quickly becomes unreadable and I want this code to be clean. Well, I'm using objects to construct a lib, but there are several…
Bite code
  • 578,959
  • 113
  • 301
  • 329
253
votes
6 answers

Print all properties of a Python Class

I have a class Animal with several properties like: class Animal(object): def __init__(self): self.legs = 2 self.name = 'Dog' self.color= 'Spotted' self.smell= 'Alot' self.age = 10 self.kids = 0 …
Idr
  • 6,000
  • 6
  • 34
  • 49
253
votes
12 answers

What is the best method to merge two PHP objects?

We have two PHP5 objects and would like to merge the content of one into the second. There are no notion of subclasses between them so the solutions described in the following topic cannot apply. How do you copy a PHP object into a different object…
Veynom
  • 4,079
  • 2
  • 19
  • 24