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
851
votes
4 answers

Functional programming vs Object Oriented programming

I've been mainly exposed to OO programming so far and am looking forward to learning a functional language. My questions are: When do you choose functional programming over object-oriented? What are the typical problem definitions where…
Olivier Lalonde
  • 19,423
  • 28
  • 76
  • 91
842
votes
19 answers

Adding a method to an existing object instance in Python

How do I add a method to an existing object (i.e., not in the class definition) in Python? I understand that it's not generally considered good practice to do so, except in some cases.
akdom
  • 32,264
  • 27
  • 73
  • 79
835
votes
38 answers

Interface vs Base class

When should I use an interface and when should I use a base class? Should it always be an interface if I don't want to actually define a base implementation of the methods? If I have a Dog and Cat class. Why would I want to implement IPet instead…
Howler
  • 2,252
  • 6
  • 22
  • 27
788
votes
16 answers

How do I call a parent class's method from a child class in Python?

When creating a simple object hierarchy in Python, I'd like to be able to invoke methods of the parent class from a derived class. In Perl and Java, there is a keyword for this (super). In Perl, I might do this: package Foo; sub frotz { …
jjohn
  • 9,708
  • 4
  • 20
  • 21
767
votes
7 answers

How to call a parent class function from derived class function?

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is derived from parent. Within each class there is a print function. In the definition of the child's print…
IaCoder
  • 12,300
  • 11
  • 37
  • 45
766
votes
17 answers

What is the difference between __init__ and __call__?

I want to know the difference between __init__ and __call__ methods. For example: class test: def __init__(self): self.a = 10 def __call__(self): b = 20
sam
  • 18,509
  • 24
  • 83
  • 116
743
votes
11 answers

What does 'super' do in Python? - difference between super().__init__() and explicit superclass __init__()

What's the difference between: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() and: class Child(SomeBaseClass): def __init__(self): SomeBaseClass.__init__(self) I've seen…
user25785
  • 7,441
  • 3
  • 18
  • 5
706
votes
19 answers

What techniques can be used to define a class in JavaScript, and what are their trade-offs?

I prefer to use OOP in large scale projects like the one I'm working on right now. I need to create several classes in JavaScript but, if I'm not mistaken, there are at least a couple of ways to go about doing that. What would be the syntax and why…
Karim
  • 18,347
  • 13
  • 61
  • 70
672
votes
7 answers

Examples of GoF Design Patterns in Java's core libraries

I am learning GoF Java Design Patterns and I want to see some real life examples of them. What are some good examples of these Design Patterns in Java's core libraries?
unj2
  • 52,135
  • 87
  • 247
  • 375
664
votes
16 answers

Difference Between Cohesion and Coupling

What is the difference between cohesion and coupling? How can coupling and cohesion lead to either good or poor software design? What are some examples that outline the difference between the two, and their impact on overall code quality?
JavaUser
  • 25,542
  • 46
  • 113
  • 139
636
votes
29 answers

What is polymorphism, what is it for, and how is it used?

What is polymorphism, what is it for, and how is it used?
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
629
votes
16 answers

Can we instantiate an abstract class?

During one of my interview, I was asked "If we can instantiate an abstract class?" My reply was "No. we can't". But, interviewer told me "Wrong, we can." I argued a bit on this. Then he told me to try this myself at home. abstract class my { …
Ravi
  • 30,829
  • 42
  • 119
  • 173
576
votes
5 answers

How should a model be structured in MVC?

I am just getting a grasp on the MVC framework and I often wonder how much code should go in the model. I tend to have a data access class that has methods like this: public function CheckUsername($connection, $username) { try { …
Dietpixel
  • 9,983
  • 11
  • 28
  • 33
563
votes
21 answers

What is the difference between association, aggregation and composition?

What is the difference between association, aggregation, and composition? Please explain in terms of implementation.
None
562
votes
7 answers

Explaining Python's '__enter__' and '__exit__'

I saw this in someone's code. What does it mean? def __enter__(self): return self def __exit__(self, type, value, tb): self.stream.close() Here is the complete code. from __future__ import with_statement#for python2.5…
zjm1126
  • 63,397
  • 81
  • 173
  • 221