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

How do you implement a class in C?

Assuming I have to use C (no C++ or object oriented compilers) and I don't have dynamic memory allocation, what are some techniques I can use to implement a class, or a good approximation of a class? Is it always a good idea to isolate the "class"…
Ben Gartner
  • 14,413
  • 10
  • 36
  • 34
168
votes
11 answers

How do I prevent the modification of a private field in a class?

Imagine that I have this class: public class Test { private String[] arr = new String[]{"1","2"}; public String[] getArr() { return arr; } } Now, I have another class that uses the above class: Test test = new…
Hossein
  • 40,161
  • 57
  • 141
  • 175
167
votes
13 answers

JavaScript pattern for multiple constructors

I need different constructors for my instances. What is a common pattern for that?
codeholic
  • 5,680
  • 3
  • 23
  • 43
167
votes
21 answers

Singleton with Arguments in Java

I was reading the Singleton article on Wikipedia and I came across this example: public class Singleton { // Private constructor prevents instantiation from other classes private Singleton() {} /** * SingletonHolder is loaded on…
Scott
166
votes
13 answers

What is Delegate?

I am confused that what is the actual role of a delegate? I have been asked this question many times in my interviews, but I don't think that interviewers were satisfied with my answer. Can anyone tell me the best definition, in one sentence, with…
Naveed
  • 41,517
  • 32
  • 98
  • 131
165
votes
18 answers

How do I create an abstract base class in JavaScript?

Is it possible to simulate abstract base class in JavaScript? What is the most elegant way to do it? Say, I want to do something like: var cat = new Animal('cat'); var dog = new Animal('dog'); cat.say(); dog.say(); It should output: meow bark
Sabya
  • 11,534
  • 17
  • 67
  • 94
164
votes
6 answers

Private vs Protected - Visibility Good-Practice Concern

I've been searching and I know the theoretic difference. public - Any class/function may access the method/property. protected - Only this class and any subclasses may access the method/property. private - Only this class may access the…
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
164
votes
7 answers

Static and Sealed class differences

Is there any class that be implemented in static class? means: static class ABC : Anyclass Is there any class which can be inherited in both sealed class and static class? means: static class ABC : AClass {} And sealed class ABC : AClass…
Saurabh Mahajan
  • 2,937
  • 7
  • 25
  • 32
162
votes
10 answers

Why doesn't Java Map extend Collection?

I was surprised by the fact that Map is not a Collection. I thought it'd make a LOT of sense if it was declared as such: public interface Map extends Collection> After all, a Map is a collection of Map.Entry,…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
161
votes
3 answers

Declaring javascript object method in constructor function vs. in prototype

In creating javascript objects, I can put a method declaration either in the constructor function or in the prototype. For example, say I want a Dog class that has a Name property and a Bark method. I can put the declaration of the Bark method…
Joe Alfano
  • 10,149
  • 6
  • 29
  • 40
160
votes
7 answers

How to do method overloading for null argument?

I have added three methods with parameters: public static void doSomething(Object obj) { System.out.println("Object called"); } public static void doSomething(char[] obj) { System.out.println("Array called"); } public static void…
Phani
  • 5,319
  • 6
  • 35
  • 43
159
votes
17 answers

Is there more to an interface than having the correct methods

So lets say I have this interface: public interface IBox { public void setSize(int size); public int getSize(); public int getArea(); //...and so on } And I have a class that implements it: public class Rectangle implements IBox { …
Ali
  • 261,656
  • 265
  • 575
  • 769
157
votes
10 answers

Why are private fields private to the type, not the instance?

In C# (and many other languages) it's perfectly legitimate to access private fields of other instances of the same type. For example: public class Foo { private bool aBool; public void DoBar(Foo anotherFoo) { if…
RichK
  • 11,318
  • 6
  • 35
  • 49
157
votes
5 answers

Default constructor vs. inline field initialization

What's the difference between a default constructor and just initializing an object's fields directly? What reasons are there to prefer one of the following examples over the other? Example 1 public class Foo { private int x = 5; private…
Barry Brown
  • 20,233
  • 15
  • 69
  • 105
157
votes
1 answer

Difference between a Factory, Provider and a Service?

What is the difference between the terms Factory, Provider and Service? Just getting into NHibernate and its Repository pattern (POCO classes, etc).
mrblah
  • 99,669
  • 140
  • 310
  • 420