
Understanding Java Interfaces: Constants, Extending, Implementing
Learn about Java interfaces, including handling empty interfaces, constants, extending interfaces through inheritance, and implementing methods in Java. Dive into the basics and explore advanced concepts for effective Java programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
More on Interfaces Empty interfaces Constants in interfaces Extending interfaces (Java 8) Static methods (Java 8) Default definitions the diamond problem lambda expressions
Interface Summary An interface is a data type variables can have that data type including (especially) parameters for methods such variables (methods) called polymorphic An interface lists public methods each implementing class implements them each class has its own implementation each object in that class has that skill set
Empty Interfaces The Serializable interface has no methods public interface Serializable {} Exists only to say OK for binary output ObjectOutputStream writeObject checks if (obj instanceof Serializable) if given non-Serializable object, throws NotSerializableException Must implement if writing to binary file public class TicTacToe implements Serializable
Constants in Interfaces Interfaces allow constants to be declared public static final int MAX_SIZE = 10000; implement the interface get the constant Constant should be part of the data type something necessary for every implementation .: MAX_SIZE probably not a good idea! interfaces for just constants are controversial use class with private constructor instead
Extending Interfaces Interfaces can be arranged using inheritance public interface Measurable { public double getArea(); public double getPerimeter(); } public interface Polygonal extends Measurable { public int getNumberOfSides(); } Polygonal has getArea and getPerimeter, too
Implementing Polygonal Anything that implements Polygonal must define all the methods mentioned in Polygonal interface... public int getNumberOfSides() ...plus all those mentioned in Measurable public double getArea() public double getPerimeter()
And So On... Can extend an interface that extends another just adding more methods to implement public interface RegularPolygonal extends Polygonal {...} Can extend more than one interface again, adding more methods to implement public interface FiniteSurface extends Measurable, Colourable { ... } Measurable Colourable Polygonal FiniteSurface RegularPolygonal
Combining Interfaces When one interface extends two others... (or more than two others) ...it may not need any more methods it just puts those two (or more) interfaces together use empty braces (no new methods required) public interface FiniteSurface extends Measureable, Colourable {}
Exercise What methods must these classes implement? public interface IA {public void is();} public interface IB {public int howMany();} public interface IC extends IB {public String whatKind();} public interface ID extends IA, IC {} public interface IE extends IA, IB {public void what();} public class A implements IE {...} public class B implements ID {...} public class C implements IA, IB {...}
Static Methods (Since Java 8) Can also add static methods public interface Expression { // missing code public static int signum(double x) { return (int)Math.signum(x); } } note: includes the definition! Must be called using interface name return Expression.signum(this.height that.height) ;
Why Static Methods? Avoid need for classes of utility methods Collections class has static methods for doing stuff with objects the implement Collection double max = Collections.max(myList); can put those methods in Collection interface double max = Collection.max(myList); benefit one less letter to type (!!!) one less thing to import (maybe) can add static methods to existing interfaces
Default Method Definitions (Since Java 8) Can add default definitions public default void clear() { while (!isEmpty()) { remove(); } } note: includes definition Implementing classes don t need to override but can override if they want to
Why Default Definitions Simplify implementing the ADT one operation defined in terms of others clear out Bag remove every item Bag is empty # elements is zero give definition as a default can call other Bag operations we know the Bag will be able to do them because they're in the Bag interface And can add operations to existing interfaces just need to give a default definition
Java 9: Private Methods (Since Java 9) Can add private methods private int signOf(double x) { return (int)Math.signum(x); } can also be static includes definition used by static/default methods, constants No other classes can call them not even classes that implement the interface
Why Private Methods Improves encapsulation can re-use code in default/static methods public default int compareTo(Measurable other) { return signOf(getArea() other.getArea()); } and constants public static Comparator<Measurable> BY_PERIMETER = (one, other) -> signOf(one.getPerimeter() other.getPerimeter());
The Diamond Problem Default methods lead to a problem: public interface Quaker { public default boolean isPacifist() { return true; } } public interface Republican { public default boolean isPacifist() { return false; } } public class Nixon implements Quaker, Republican { // which definition of isPacifist()? }
Conflicting Default Definitions Two different definitions are available which to use? how to say which to use? Java requires the class to Override Nixon must override isPacifist method public class Nixon implements Quaker, Republican { @Override public boolean isPacifist() { return false; } }
Lambda Expressions Recall lambda expressions used for interfaces with just one method Java figures out what method is being defined OK if has default or static methods default and static methods have definitions so long as only one method with no definition i.e. one abstract method hence Single Abstract Method (SAM) requirement
Note on Style Items in an interface are usually public public unless you say private NOTE: different than in classes! but say public anyway! All variables in interfaces are static static even if you don t say but say static anyway! Redundancy helps avoid errors except methods are abstract, but don t say so
Abstract Classes Intermediate between class and interface it is a class, but some methods are abstract public abstract class Application { public abstract void start(Stage primaryStage); } abstract methods have no body must say that that method is abstract must say that the class is abstract
Interfaces vs. Abstract Classes Interfaces now more like abstract classes can inherit method definitions can have static methods can have private methods But still different abstract classes can have instance variables and class variables that aren t final and are still subject to single inheritance