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
546
votes
34 answers

JavaScript private methods

To make a JavaScript class with a public method I'd do something like: function Restaurant() {} Restaurant.prototype.buy_food = function(){ // something here } Restaurant.prototype.use_restroom = function(){ // something here } That way…
Wayne Kao
  • 8,125
  • 5
  • 30
  • 25
543
votes
32 answers

How would one write object-oriented code in C?

What are some ways to write object-oriented code in C? Especially with regard to polymorphism. See also this Stack Overflow question Object-orientation in C.
Dinah
  • 52,922
  • 30
  • 133
  • 149
538
votes
31 answers

How should I have explained the difference between an Interface and an Abstract class?

In one of my interviews, I have been asked to explain the difference between an Interface and an Abstract class. Here's my response: Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can…
Thinker
  • 6,820
  • 9
  • 27
  • 54
535
votes
5 answers

JavaScript: Class.method vs. Class.prototype.method

What is the difference between the following two declarations? Class.method = function () { /* code */ } Class.prototype.method = function () { /* code using this.values */ } Is it okay to think of the first statement as a declaration of a static…
postrational
  • 6,306
  • 3
  • 22
  • 27
531
votes
14 answers

Is there a performance difference between i++ and ++i in C?

Is there a performance difference between i++ and ++i if the resulting value is not used?
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
528
votes
25 answers

When to use an interface instead of an abstract class and vice versa?

This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage. When would one want to use an interface and when would one want to use an abstract class?
Chirantan
  • 15,304
  • 8
  • 49
  • 75
492
votes
29 answers

What are the differences between struct and class in C++?

This question was already asked in the context of C#/.Net. Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design. I'll start…
palm3D
  • 7,970
  • 6
  • 28
  • 33
484
votes
15 answers

Should we @Override an interface's method implementation?

Should a method that implements an interface method be annotated with @Override? The javadoc of the Override annotation says: Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is…
Benno Richters
  • 15,378
  • 14
  • 42
  • 45
483
votes
36 answers

Use of .apply() with 'new' operator. Is this possible?

In JavaScript, I want to create an object instance (via the new operator), but pass an arbitrary number of arguments to the constructor. Is this possible? What I want to do is something like this (but the code below does not work): function…
Prem
  • 15,911
  • 11
  • 31
  • 35
474
votes
28 answers

Why Doesn't C# Allow Static Methods to Implement an Interface?

Why was C# designed this way? As I understand it, an interface only describes behaviour, and serves the purpose of describing a contractual obligation for classes implementing the interface that certain behaviour is implemented. If classes wish to…
Kramii
  • 8,379
  • 4
  • 32
  • 38
468
votes
22 answers

What does the 'static' keyword do in a class?

To be specific, I was trying this code: package hello; public class Hello { Clock clock = new Clock(); public static void main(String args[]) { clock.sayTime(); } } But it gave the error Cannot access non-static field in…
Ali
  • 261,656
  • 265
  • 575
  • 769
463
votes
18 answers

What is the difference between class and instance methods?

What's the difference between a class method and an instance method? Are instance methods the accessors (getters and setters) while class methods are pretty much everything else?
Devoted
  • 177,705
  • 43
  • 90
  • 110
446
votes
40 answers

Difference between abstraction and encapsulation?

What is the precise difference between encapsulation and abstraction?
Rocky
  • 4,471
  • 3
  • 18
  • 5
440
votes
20 answers

Does JavaScript have the interface type (such as Java's 'interface')?

I'm learning how to make OOP with JavaScript. Does it have the interface concept (such as Java's interface)? So I would be able to create a listener...
The Student
  • 27,520
  • 68
  • 161
  • 264
415
votes
20 answers

Is there a performance difference between i++ and ++i in C++?

We have the question is there a performance difference between i++ and ++i in C? What's the answer for C++?
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465