I have

public class BasicEnemy extends GameObject{ /*а тут должно быть еще и extends Enemies*/ ... } 

I need to make an abstract class, ala:

 public abstract class Enemies{ ... } 

But how to be if my BasicEnemy class already inherits a GameObject (you can't do without it).

  • 2
    Use an interface instead of an abstract class. - Sergey Gornostaev
  • What prevents you from writing public abstract class Enemies extends GameObject and public class BasicEnemy extends Enemies ? If there really is a good reason, then only the interface, as advised by Sergey. - Sergey

2 answers 2

Inheritance in Java is hierarchical: you can define a GameObject class, from it any other

 class GameObjSubСlass extends GameObject { void doSomething() {...} ... } 

from him - one more

 class GameObjSubSubСlass extends GameObjSubСlass { void doSomethingElse() {...} ... } 

from him, in turn, still subclass (s)

 class DangerousEnemy extends GameObjSubSubСlass { void doSomethingSpecial() {...} ... } class DreadfulEnemy extends GameObjSubSubСlass { ... } 

and so on. Any class from the inheritance hierarchy will have those methods and fields that are defined in any of its ancestors: the doSomething() , doSomethingElse() and doSomethingSpecial() methods will be available to the object of the GameObjSubСlass class.
If you want classes from different hierarchies to have some common features, then you should describe the interface with the methods that describe these common features, and implement the methods of these interfaces in each of these classes. For example, if you want some types of friends and some types of enemies to fly and others to crawl, you can do something like this:

 class GameObject { // write methods as needed } class Enemy extends GameObject { void fight() { /* your code */ } // write more methods as needed } class Friend extends GameObject { void embrace() { /* your code */ } // write more methods as needed } interface CanFly { void fly(); } interface CanCrawl { void crawl(); } class FlyingFriend extends Friend implements CanFly { public void fly() { /* your code */ } } class CrawlingFriend extends Friend implements CanCrawl { public void crawl() { /* your code */ } } class FlyingEnemy extends Enemy implements CanFly { public void fly() { /* your code */ } } class CrawlingEnemy extends Enemy implements CanCrawl { // Рожденный ползать летать не может! public void crawl() { /* your code */ } } class TerribleEnemy extends Enemy implements CanFly, CanCrawl { // Этот может и ползать, и летать - ужас! public void crawl() { /* your code */ } public void fly() { /* your code */ } } 

    do not do multiple inheritance in java, use the interface

    • In what sense is it impossible? Explain. For example, I have a GG abstraction and the abstraction of CC dances from it, in turn from the SS there is already a child class Player with the implementation of redefined functions from CC | GG - GenCloud
    • @GenCloud Plural in the sense that you can’t declare a class like this: class A extends B, C, D {} - Artem Konovalov
    • 2
      @ArtemKonovalov, this is clear, but it was about combining the executable functions of similar classes on properties into one abstraction - GenCloud