0

I am studying the programming language Kotlin, and I just came across the idea of a Data Class. I have a background in Java programming where classes can have fields and methods. I have heard programmers calling a class that contains only fields and no methods, as a Value Class. Also, I am familiar with the idea of Structures in C++ which to my knowledge, typically contain only fields.

Question #1: I therefore wondered about classes that contain only methods. Based on my Java background, it seems to me that it would probably be a static utility class containing only static methods. Also, since interfaces in Java 9 can contain default methods, you could have a method-only structure with interfaces. But what is the term that best describes this style of method-only classes?

Question #2: I wondered also about classes that contains both fields and methods. How are these called? Is there a more specific term, other than just Class or Standard Class, in contrast to Value Class and method-only classes.

Remark: I am not looking for terms that are programming language specific, but rather for generally accepted terms that apply across programming languages.

Edit:

Dyad Class: class that contains both methods and fields.

Action Class: class that contains only methods, and no fields.

Data Class: class that contains only fields, and no methods.

Steve T
  • 141

2 Answers2

9

Question #1: what is the name of a class that contains only methods?

Stateless

Question #2: what is the name of a class that contains both fields and methods?

Stateful

These "names" (really, they're more like descriptions) come from the idea that the class fields can be thought of as an object's state. No fields, no state.

a static method -> no instance state.

A static method has no access to instance state. This is true but some people get confused and think that means you make a method static when it uses no instance state. Such people are pointing the implication arrow in the wrong direction. Besides, that thinking ignores the polymorphic power of non-static (dynamic) methods.

An object is a bag of functions that move around together. When it's properly encapsulated the outside world has no idea if it has state or not. It's really nobody else's business. Forcing the interface to change when state is added or removed violates the privacy of the class. Keep your nosy nose outta my privates.

The most famous example of a stateless object is the Gang of Fours Null Object Pattern. It's used to disable features by configuration rather than business logic. The null object follows some set interface but is packed with methods that quietly do nothing. Doing nothing doesn't require state.

Now some might point to examples of static methods like Math.pow() but remember, that's coming from a library. Your own codebase is a much more wild place. Don't pretend you're writing a library if you aren't.

From a design point of view there are two reasons to group methods together:

  1. They use the same fields
  2. They change together

Don't just focus on 1.

candied_orange
  • 119,268
1

The first question should be: what is a class?

Wikipedia definition: "a class is an extensible program-code-template for creating objects"

So to answer your first question: what is the name of a class that contains only methods?

Technically you don't need a class for a code structure that contains only methods, because it's not necessary to instantiate the class (to create objects) to call these methods. In many OO-based languages you have no choice but to create a class though. Some languages allow you to mark the class and methods static, in order to prevent instantiation.

Classes without methods (data class) are often accompanied with classes that contain only methods. A typical name for these classes with nothing but methods is SometingService. So you will see a User data class and a UserService class that contain methods to do operations on User objects. This pattern is an OO-antipattern; to quote Martin Fowler: 'it's contrary to the basic idea of object-oriented design; which is to combine data and process together' (source).

Edit: @Christophe pointed out in the comments that there are valid scenario's for creating classes that contain only methods, but still require instantiation. For example the Strategy pattern encapsulates an algorithm which may not require state. In this context, it's mostly accidental that the class doesn't contain fieds.

As for your second question: what is the name of a class that contains both fields and methods?

A class.

Rik D
  • 4,975