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
155
votes
12 answers

Creating anonymous objects in php

As we know, creating anonymous objects in JavaScript is easy, like the code below: var object = { p : "value", p1 : [ "john", "johnny" ] }; alert(object.p1[1]); Output: an alert is raised with value "johnny" Can this same technique be…
Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79
155
votes
12 answers

Inheritance vs. Aggregation

There are two schools of thought on how to best extend, enhance, and reuse code in an object-oriented system: Inheritance: extend the functionality of a class by creating a subclass. Override superclass members in the subclasses to provide new…
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
154
votes
16 answers

What is the definition of "interface" in object oriented programming

A friend of mine goes back and forth on what "interface" means in programming. What is the best description of an "interface"? To me, an interface is a blueprint of a class. Is this the best definition?
Daniel Kivatinos
  • 24,088
  • 23
  • 61
  • 81
153
votes
4 answers

Override ActiveRecord attribute methods

An example of what I'm talking about: class Person < ActiveRecord::Base def name=(name) super(name.capitalize) end def name super().downcase # not sure why you'd do this; this is just an example end end This seems to work, but I…
Require This
153
votes
13 answers

Abstract attributes in Python

What is the shortest / most elegant way to implement the following Scala code with an abstract attribute in Python? abstract class Controller { val path: String } A subclass of Controller is enforced to define "path" by the Scala compiler. A…
deamon
  • 89,107
  • 111
  • 320
  • 448
153
votes
7 answers

Should __init__() call the parent class's __init__()?

I'm used that in Objective-C I've got this construct: - (void)init { if (self = [super init]) { // init class } return self; } Should Python also call the parent class's implementation for __init__? class…
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
152
votes
10 answers

Real world example about how to use property feature in python?

I am interested in how to use @property in Python. I've read the python docs and the example there, in my opinion, is just a toy code: class C(object): def __init__(self): self._x = None @property def x(self): """I'm the…
xiao 啸
  • 6,350
  • 9
  • 40
  • 51
152
votes
5 answers

Java `final` method: what does it promise?

In a Java class a method can be defined to be final, to mark that this method may not be overridden: public class Thingy { public Thingy() { ... } public int operationA() {...} /** this method does @return That and is final. */ …
towi
  • 21,587
  • 28
  • 106
  • 187
152
votes
4 answers

Is MATLAB OOP slow or am I doing something wrong?

I'm experimenting with MATLAB OOP, as a start I mimicked my C++'s Logger classes and I'm putting all my string helper functions in a String class, thinking it would be great to be able to do things like a + b, a == b, a.find( b ) instead of strcat(…
stijn
  • 34,664
  • 13
  • 111
  • 163
151
votes
6 answers

What is the point of a private pure virtual function?

I came across the following code in a header file: class Engine { public: void SetState( int var, bool val ); { SetStateBool( int var, bool val ); } void SetState( int var, int val ); { SetStateInt( int var, int val );…
BeeBand
  • 10,953
  • 19
  • 61
  • 83
150
votes
6 answers

Is it possible to create static classes in PHP (like in C#)?

I want to create a static class in PHP and have it behave like it does in C#, so Constructor is automatically called on the first call to the class No instantiation required Something of this sort... static class Hello { private static…
aleemb
  • 31,265
  • 19
  • 98
  • 114
150
votes
6 answers

How do I design a class in Python?

I've had some really awesome help on my previous questions for detecting paws and toes within a paw, but all these solutions only work for one measurement at a time. Now I have data that consists off: about 30 dogs; each has 24 measurements…
Ivo Flipse
  • 10,222
  • 18
  • 50
  • 63
150
votes
4 answers

How to check if an object implements an interface?

How to check if some class implements interface? When having: Character.Gorgon gor = new Character.Gorgon(); how to check if gor implements Monster interface? public interface Monster { public int getLevel(); public int level =…
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
148
votes
6 answers

What is an abstract class in PHP?

What is an abstract class in PHP? How can it be used?
udaya
  • 9,598
  • 15
  • 48
  • 67
147
votes
12 answers

PHP abstract properties

Is there any way to define abstract class properties in PHP? abstract class Foo_Abstract { abstract public $tablename; } class Foo extends Foo_Abstract { //Foo must 'implement' $property public $tablename = 'users'; }
Tamás Pap
  • 17,777
  • 15
  • 70
  • 102