Can I have an empty Java class?

前端 未结 2 1445
暗喜
暗喜 2021-01-06 16:38

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

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-06 17:22

    Well to do it you don't need to have an abstract class and a class that extends it, or an empty class(wich is possible too).

    First way:

    You just need to implement two classes: The class that contains the methods and the variables you need to use and the second calss that has an instance of your first class:

    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
      }
    }
    

    Because if you implement a class that extends an abstract class it should implement all its methods.

    Second way:

    • Or if you want your second class to be specialized you could have :

    your first class wich should not be abstract and the second one can extend it and use all its methods, and have its specific methods

提交回复
热议问题