https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
An interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
An interface declaration consists of modifiers, the keyword
interface
, the interface name, a comma-separated list of parent interfaces (if any), and the interface body. For example:public interface GroupedInterface extends Interface1, Interface2, Interface3 { // constant declarations
// default: public static final // base of natural logarithms double E = 2.718282; // method signatures void doSomething (int i, double x); int doSomethingElse(String s); }
The interface body can contain abstract methods, default methods, and static methods.
An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).
Default methods are defined with the
default
modifier, Static methods with the
static
keyword. All abstract, default, and static methods in an interface are implicitly
public
, so you can omit the public
modifier.All constant values defined in an interface are implicitly
public
, static
, and final
Default Methods In Java
Before Java 8, interfaces could have only abstract methods. The implementation of these methods has to be provided in a separate class. So, if a new method is to be added in an interface then its implementation code has to be provided in the class implementing the same interface. To overcome this issue, Java 8 has introduced the concept of default methods which allow the interfaces to have methods with implementation without affecting the classes that implement the interface.
Important Points:
- Interfaces can have default methods with implementation from java 8 onwards.
- Interfaces can have static methods as well similar to static method of classes.
- Default methods were introduced to provide backward comparability for old interfaces so that they can have new methods without effecting existing code.
Using an Interface as a Type
When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
// //////////////// //////////////////////// //////////////////////// //////////////////////// //////////////
// A simple program to Test Interface default // methods in java interface TestInterface { // abstract method public void square( int a); // default method default void show() { System.out.println( "Default Method Executed" ); } } class TestClass implements TestInterface { // implementation of square abstract method public void square( int a) { System.out.println(a*a); } public static void main(String args[]) { TestClass d = new TestClass(); d.square( 4 ); // default method executed d.show(); } }
///////////////////
Static Methods: The interfaces can have static methods as well which is similar to static method of classes.
|
///////////////////
// A simple Java program to demonstrate multiple
// inheritance through default methods.
interface
TestInterface1
{
// default method
default
void
show()
{
System.out.println(
"Default TestInterface1"
);
}
}
interface
TestInterface2
{
// Default method
default
void
show()
{
System.out.println(
"Default TestInterface2"
);
}
}
// Implementation class code
class
TestClass
implements
TestInterface1, TestInterface2
{
// Overriding default show method
public
void
show()
{
// use super keyword to call the show
// method of TestInterface1 interface
TestInterface1.
super
.show();
// use super keyword to call the show
// method of TestInterface2 interface
TestInterface2.
super
.show();
}
public
static
void
main(String args[])
{
TestClass d =
new
TestClass();
d.show();
}
}
Default Methods
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
public interface TimeClient { void setTime(int hour, int minute, int second); void setDate(int day, int month, int year); void setDateAndTime(int day, int month, int year, int hour, int minute, int second); LocalDateTime getLocalDateTime(); }
========>>>>
All method declarations in an interface, including default methods, are implicitly
public
, so you can omit the public
modifier.public interface TimeClient { void setTime(int hour, int minute, int second); void setDate(int day, int month, int year); void setDateAndTime(int day, int month, int year, int hour, int minute, int second); LocalDateTime getLocalDateTime(); static ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) { System.err.println("Invalid time zone: " + zoneString + "; using default time zone instead."); return ZoneId.systemDefault(); } } default ZonedDateTime getZonedDateTime(String zoneString) { return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString)); } }
Extending Interfaces That Contain Default Methods
When you extend an interface that contains a default method, you can do the following:
- Not mention the default method at all, which lets your extended interface inherit the default method.
- Redeclare the default method, which makes it
abstract
. - Redefine the default method, which overrides it.
Example 1:
public interface AnotherTimeClient extends TimeClient { }
Any class that implements the interface
AnotherTimeClient
will have the implementation specified by the default method TimeClient.getZonedDateTime
.Example 2:
public interface AbstractZoneTimeClient extends TimeClient { public ZonedDateTime getZonedDateTime(String zoneString); // <<== become abstract }
Any class that implements the interface
AbstractZoneTimeClient
will have to implement the method getZonedDateTime
; this method is an abstract
method like all other nondefault (and nonstatic) methods in an interface.
Example 3:
public interface HandleInvalidTimeZoneClient extends TimeClient {
// rewrite default method default public ZonedDateTime getZonedDateTime(String zoneString) { try { return ZonedDateTime.of(getLocalDateTime(),ZoneId.of(zoneString)); } catch (DateTimeException e) { System.err.println("Invalid zone ID: " + zoneString + "; using the default time zone instead."); return ZonedDateTime.of(getLocalDateTime(),ZoneId.systemDefault()); } } }
Any class that implements the interface
HandleInvalidTimeZoneClient
will use the implementation of getZonedDateTime
specified by this interface instead of the one specified by the interface TimeClient
.Static Methods
In addition to default methods, you can define static methods in interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.) This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class. The following example defines a static method that retrieves a
ZoneId
object corresponding to a time zone identifier; it uses the system default time zone if there is no ZoneId
object corresponding to the given identifier. (As a result, you can simplify the method getZonedDateTime
):public interface TimeClient { // ... static public ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) { System.err.println("Invalid time zone: " + zoneString + "; using default time zone instead."); return ZoneId.systemDefault(); } } default public ZonedDateTime getZonedDateTime(String zoneString) { return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString)); } }
Like static methods in classes, you specify that a method definition in an interface is a static method with the
static
keyword at the beginning of the method signature. All method declarations in an interface, including static methods, are implicitly public
, so you can omit the public
modifier.Summary of Interfaces
An interface declaration can contain method signatures, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods.
A class that implements an interface must implement all the methods declared in the interface.
An interface name can be used anywhere a type can be used.
A class that implements an interface must implement all the methods declared in the interface.
An interface name can be used anywhere a type can be used.
Quiz
Question 1: What methods would a class that implements the
Answer 1:
java.lang.CharSequence
interface have to implement?Answer 1:
charAt
, length
, subSequence
, and toString
.
Question 2: What is wrong with the following interface?
public interface SomethingIsWrong { void aMethod(int aValue) { System.out.println("Hi Mom"); } }
Answer 2: It has a method implementation in it. Only default and static methods have implementations.
Question 3: Fix the interface in Question 2.
Answer 3:
Answer 3:
public interface SomethingIsWrong { void aMethod(int aValue); }
Alternatively, you can define
aMethod
as a default method:public interface SomethingIsWrong { default void aMethod(int aValue) { System.out.println("Hi Mom"); } }
Question 4: Is the following interface valid?
public interface Marker { }
Answer 4: Yes. Methods are not required. Empty interfaces can be used as types and to mark classes without requiring any particular method implementations. For an example of a useful empty interface, see
java.io.Serializable.
No comments:
Post a Comment