Can I have an empty Java class?

11,772

Of course...

class AObstacle { }

(Plus whatever inheritance model you're using.) There's nothing stopping you from doing this.

Remember that a class isn't really a thing that you're defining. A type is. The class is just the language/syntax construct used to describe the type. If the type being described has no attributes or operations aside from the inheritance model, then there's nothing else to add to it.

Though you are adding one thing. You're giving it a name. It doesn't sound like much, but defining a semantic concept with a concrete name (particularly in a statically typed environment) is very important. Your type now has an identity apart from other types in the system. If things are added to it later, there's a place to add them without refactorings and breaking changes.

Share:
11,772
Andrew Willoughby
Author by

Andrew Willoughby

Updated on August 28, 2022

Comments

  • Andrew Willoughby
    Andrew Willoughby over 1 year

    I'm creating a grid based game.

    I need to implement a set of obstacles that take random positions within the grid. I've created an abstract class ALifeForm, that holds the common methods for every item within the grid. Obviously, abstract classes can't be initialised, so I was going to create a new class AObstacle, which will extend ALifeForm.

    Only issue is, my AObstacle class isn't specialised. All the methods it needs are within ALifeForm.

    Can I have an empty class? Is it bad programming practice? And if so, what can I implement instead?