Can I have an empty Java class?

I create a grid based on the game.

I need to implement a set of obstacles that occupy random positions in the grid. I created an abstract class ALifeFormthat contains common methods for each element in the grid. Obviously, abstract classes cannot be initialized, so I'm going to create a new class AObstaclethat will expand ALifeForm.

The only problem is that my class is AObstaclenot specialized. All the methods he needs are within ALifeForm.

Is it possible to create an empty class? Is this a bad programming practice? And if so, what can I implement instead?

+2
source share
2 answers

Of course...

class AObstacle { }

(Plus, any inheritance model you use.) There is nothing stopping you from doing this.

Remember that a class is not what you define. Type is. A class is simply a language / syntax construct used to describe a type. If the type described has no attributes or operations other than an inheritance model, then there is nothing to add to it.

Although you add one thing. You give him a name. This does not seem like much, but defining a semantic concept with a specific name (especially in a statically typed environment) is very important. Your type now has an identifier different from other types in the system. If things are added to them later, there is a place to add them without refactoring and break the changes.

+6
source

, , ( ).

:

: , , , calss, :

public class A{
  public void firstMethod(){
    //do your stuff here
  }
....
}

public class B{
  public static void main(String[] args) {
   A a=new A(); //instantiate your class here 
   a.firstMethod();// then just use its methods
  }
}

, , , .

:

  • , , , :

, ,

0

All Articles