Questions tagged [class]

A template for declaring a type of object.

A template for declaring a type of object.

Classes are the bread and butter building blocks for Object Oriented Programming languages.

292 questions
183
votes
5 answers

When do you use a struct instead of a class?

What are your rules of thumb for when to use structs vs. classes? I'm thinking of the C# definition of those terms but if your language has similar concepts I'd like to hear your opinion as well. I tend to use classes for almost everything, and use…
RationalGeek
  • 10,077
  • 7
  • 38
  • 56
145
votes
6 answers

What is the point of having every service class have an interface?

At the company I work at, every service class has a corresponding interface. Is this necessary? Notes: Most of these interfaces are only used by a single class We are not creating any sort of public API With modern mocking libraries able to…
Bob Roberts
  • 1,777
  • 2
  • 11
  • 12
127
votes
10 answers

Don't Use "Static" in C#?

I submitted an application I wrote to some other architects for code review. One of them almost immediately wrote me back and said "Don't use static. You can't write automated tests with static classes and methods. static is to be avoided." I…
Infin8Loop
  • 1,459
  • 2
  • 11
  • 16
103
votes
8 answers

Why have private static methods?

I just wanted to clear up a question I have. What is the point of having a private static method as opposed to a normal method with private visibility? I would have thought an advantage to having a static method is that it can be called without an…
85
votes
14 answers

Do objects in OOP have to represent an entity?

Does an object have to represent an entity? By an entity I mean something like a Product, Motor, a ParkingLot etc, a physical, or even a clear-cut non-physical conceptual object -- something that is well defined, with some core data clearly…
Dennis
  • 8,157
  • 5
  • 36
  • 68
65
votes
11 answers

Why is it good to split a program into multiple classes?

I'm still a student in high school (entering 10th grade), and I have yet to take an actual computer course in school. Everything I've done so far is through books. Those books have taught me concepts such as inheritance, but how does splitting a…
kullalok
  • 1,075
  • 3
  • 9
  • 10
63
votes
5 answers

When to use primitive vs class in Java?

I see that Java has Boolean (class) vs boolean (primitive). Likewise, there's an Integer (class) vs int (primitive). What's the best practice on when to use the primitive version vs the class? Should I basically always be using the class version…
Casey Patton
  • 5,211
  • 7
  • 33
  • 41
59
votes
8 answers

How do I prove or disprove "God objects" are wrong?

Problem Summary: Long story short, I inherited a code base and a development team I am not allowed to replace and the use of God Objects is a big issue. Going forward, I want to have us re-factor things but I am getting push-back from the teams who…
honestduane
  • 715
  • 1
  • 6
  • 5
57
votes
5 answers

Where should I put functions that are not related to a class?

I am working on a C++ project where I have a bunch of math functions that I initially wrote to use as part of a class. As I've been writing more code, though, I've realized I need these math functions everywhere. Where is the best place to put them?…
coconut
  • 673
  • 1
  • 5
  • 5
51
votes
4 answers

Why and when should I make a class 'static'? What is the purpose of 'static' keyword on classes?

The static keyword on a member in many languages mean that you shouldn't create an instance of that class to be able to have access to that member. However, I don't see any justification to make an entire class static. Why and when should I make a…
Saeed Neamati
  • 18,142
  • 23
  • 87
  • 125
46
votes
4 answers

Why prefer non-static inner classes over static ones?

This question is about whether to make a nested class in Java to be a static nested class or an inner nested class. I searched around here and on Stack Overflow, but couldn't really find any questions regarding the design implications of this…
Frank
  • 14,407
  • 3
  • 41
  • 66
41
votes
1 answer

Suggest a best practice to create Constants class

There is a debate between my team members about the declaration of a Constants class. We are moving the constant variables into a separate class like below. public class Constants { public const string StateId = "ST"; public const string…
user46506
  • 943
  • 3
  • 10
  • 11
38
votes
3 answers

When to use a Singleton and when to use a static class

I've searched about this here and on StackOverflow and found some differences between the two. But I'm still not sure in what cases one would prefer a Singleton, and in what cases one would choose to use a static class. (In languages which don't…
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
37
votes
4 answers

How to best to organize class and interface files?

OK .. after all the discussion I'm changing my question slightly to better reflect a concrete example that I am dealing with. I have two classes ModelOne and ModelTwo, These classes perform similar type of functionality but are unrelated to each…
Peter M
  • 2,029
  • 2
  • 17
  • 22
29
votes
9 answers

In OOP, isn't the 'protected' keyword required?

Some modern languages (e.g. Swift, Dart) do not support the protected access modifier keyword. Swift is a protocol-oriented language, but I've heard that Dart is a completely object-oriented language. Why don't these modern languages ​​support…
1
2 3
19 20