Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're mixing up interfaces with abstract classes:</p> <p>An <strong>interface</strong> describes the contract that all implementing classes must obey. It is basically a list of methods (and, importantly, documentation of what they should return, how they should behave etc.</p> <p>Notice that <strong>NONE</strong> of the methods have a body. Interfaces do not do that. You can, however, define static final constants in an interface.</p> <pre><code>public interface Shape { /** * returns the area of the shape * throws NullPointerException if either the height or width of the shape is null */ int getSize(); /** * returns the height of the shape */ int getHeight(); /** * returns the width of the shape */ int getWidth(); } </code></pre> <p>An <strong>abstract</strong> class implements some of the methods, but not all. (Technically the abstract class <em>could</em> implement all methods, but that wouldn't make for a good example). The intention is that extending classes will implement the abstracted methods.</p> <pre><code>/* * A quadrilateral is a 4 sided object. * This object has 4 sides with 90' angles i.e. a Square or a Rectangle */ public abstract class Quadrilateral90 implements Shape { public int getSize() { return getHeight() * getWidth(); } public abstract int getHeight(); // this line can be omitted public abstract int getWidth(); // this line can be omitted } </code></pre> <p>Finally, the extending object implements all the remaining methods both in the abstract parent class and in the interface. Notice that getSize() is not implemented here (though you could override it if you wanted to).</p> <pre><code>/* * The "implements Shape" part may be redundant here as it is declared in the parent, * but it aids in readability, especially if you later have to do some refactoring. */ public class Square extends Quadrilateral90 implements Shape { public int getHeight() { // method body goes here } public int getWidth() { // method body goes here } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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