Interface

 
/**
 * When a class implements an interface it agrees to a contract.
 * This is forcing the developer to implement interface's methods.
 * 
 * All methods in an interface must be declared abstract.
 */

package com.minte9.oop.interfaces;

public class Interfaces {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.move();  // The cat is moving
        cat.play();  // The cat is playing
    }
}

abstract class Feline {
    public abstract void move();
}

interface Playable {
    public abstract void play();
}

class Cat extends Feline implements Playable {
    public void move() {
        System.out.println("The cat is moving");
    }
    public void play() {
        System.out.println("The cat is playing");
    }
}

Legacy Interfaces

 
/**
 * Adding a new method to the interface can break other classes.
 * Extending an interface is also not recommended.
 * Where is possible, it is better to createa a new interface.
 */

package com.minte9.oop.interfaces;

public class LegacyInterfaces {
    public static void main(String[] args) {
        ClassA obj = new ClassA();
        obj.setValue(10);  // Value set in ClassA
        obj.setType("Double");  // Type set in ClassA
    }
}

interface Variable {
    public void setValue(int n);
    public void setType(Object o); // NEW METHOD
}

class ClassA implements Variable {
    @Override
    public void setValue(int n) {
        System.out.println("Value set in ClassA");
    }
    @Override
    public void setType(Object n) {
        System.out.println("Type set in ClassA");
    }
}

/**
 * class ClassB implements Variable {
 *      @Override
 *      public void setValue(int n) {
 *        System.out.println("Value set / ClassB");
 *      }
 *  }
*/

Default Method

 
/**
 * Java 8 introduced default methods in interfaces.
 * This allowed us to add implementations in interfaces.
 * Non default methods must be all defined as abstract (without body).
 */

package com.minte9.oop.interfaces;

public class DefaultMethods {
    public static void main(String[] args) {

        Parameter p = new Parameter();
        p.setValue(10);  // Value set in Parameter Class
        p.setType("Double");  // Type set in Parameter Class

        Character c = new Character();
        c.setValue(20);  // Value set in Character Class
    }
}

interface Customable {
    void setValue(int n);
    default void setType(Object o) {
        System.out.println("Type set in default method.");
    }
}

class Parameter implements Customable {
    @Override
    public void setValue(int n) {
        System.out.println("Value set in Parameter Class");
    }
    @Override
    public void setType(Object n) {
        System.out.println("Type set in Parameter Class");
    }
}

class Character implements Customable {
    @Override
    public void setValue(int n) {
        System.out.println("Value set in Character Class");
    }

    // No need to implement setType() unless we want custom behavior
}






Questions and answers:
Clink on Option to Answer




1. What does it mean when a class implements an interface?

  • a) It agrees to implement all of the interface’s methods
  • b) It inherits code from the interface

2. All methods in a (regular) interface are:

  • a) Abstract
  • b) Static

3. Why is adding a new method to an existing interface dangerous?

  • a) It can break classes that already implement it
  • b) It makes the interface faster

4. What is the purpose of a default method in an interface?

  • a) To provide an implemented method that classes may reuse or override
  • b) To prevent classes from implementing the method

5. In the example, why doesn’t Character need to implement setType()?

  • a) It’s optional in interfaces
  • b) It inherits the default implementation


References: