Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the example you gave you don't need two interfaces you only need the <code>Collideable</code> interface. The Collideable interface can be defined as follows:</p> <pre><code>interface Collideable { void collideWith(Collideable other); String getType(); } </code></pre> <p>Then <code>Spaceship</code>, <code>Plane</code>, and <code>Asteroid</code> would all implement <code>collideWith</code> and <code>getType</code>. As an example implementation, Spaceship's implementation would look like this:</p> <pre><code>class Spaceship implements Collideable { public void collideWith(Collideable other) { System.out.println(this.getType() + " collides with " + other.getType()); } public String getType(){ return "Spaceship"; } } </code></pre> <p>You can go one step further and declare Collideable as an abstract class, providing the implementation of collideWith since it is always the same. For example:</p> <pre><code>abstract class Collideable { void collideWith(Collideable other){ System.out.println(this.getType() + " collides with " + other.getType()); } String getType(); } </code></pre> <p>As a side note and general tip, your <code>Visitor</code> interface is poor design. The purpose of an interface is to define methods that <strong>all</strong> classes implementing that interface must provide a method definition (implementation) of. However your interface is very specific: it provides several types (<code>Spaceship</code>, <code>Asteroid</code>, etc) as method parameters. Therefore it is bad design, why did you even need that interface to begin with? It doesn't look like it can be used anywhere else. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload