Monday, January 25, 2016

Topic 28: Interfaces in Java

  • Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. 

  • Once it is defined, any number of classes can implement an interface

    One class can implement any number of interfaces. 

  • To implement an interface, a class must create the complete set of methods defined by the interface. Each class is free to determine the details of its own implementation. 

  • By providing the interface keyword, Java allows us to fully utilize the “one interface, multiple methods” aspect of polymorphism.

  • Interfaces support dynamic method resolution at run time.

  • An interface is defined much like a class. This is the general form of an interface:

access-specifier interface name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}

  • When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared.

  • When it is declared as public, the interface can be used by any other code. In this case, the interface must be the only public interface declared in the file, and the file must have the same name as the interface.

  • name is the name of the interface, and can be any valid identifier.

  • The methods that are declared have no bodies. They end with a semicolon after the parameter list.

  •  Each class that includes an interface must implement all of the methods.

  • Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized. All methods and variables are implicitly public.

  • To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. The general form of a class that includes the implements clause looks like this:

class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}

If a class implements more than one interface, the interfaces are separated with a comma. 

If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface. 

The methods that implement an interface must be declared public. 

The type signature of the implementing method must match exactly the type signature specified in the interface definition

  • It is both permissible and common for classes that implement interfaces to define additional members of their own.

  • We can declare interface object reference variable. An instance of any class that implements the declared interface can be referred to by such a variable. When we call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to.

An interface reference variable only has knowledge of the methods declared by its interface declaration.

  • We should be careful not to use interfaces casually in performance-critical code.

  • If a class includes an interface but does not fully implement the methods defined by that interface, then that class must be declared as abstract. Further, any class that inherits this abstract class must implement interface completely or be declared abstract itself.

  • An interface can be declared a member of a class or another interface. Such an interface is called a member interface or a nested interface

    A nested interface can be declared as public, private, or protected. This differs from a top-level interface, which must either be declared as public or use the default access level. 

    When a nested interface is used outside of its enclosing scope, it must be qualified by the name of the class or interface of which it is a member.

Here is an example that demonstrates a nested interface:

// A nested interface example.
// This class contains a member interface.
class A
{
// this is a nested interface
public interface NestedIF
{
boolean isNotNegative(int x);
}

}

// B implements the nested interface.
class B implements A.NestedIF
{
public boolean isNotNegative(int x)
{
return x < 0 ? false : true;
}
}

class NestedIFDemo
{
public static void main(String args[])
{
// use a nested interface reference
A.NestedIF nif = new B();
if(nif.isNotNegative(10))
System.out.println("10 is not negative");
if(nif.isNotNegative(-12))
System.out.println("this won't be displayed");
}
}

  • We can use interfaces to import shared constants into multiple classes by simply declaring an interface that contains variables that are initialized to the desired values. When we implement this interface in a class, all of those variable names will be in scope as constants. If an interface contains no methods, then any class that includes such an interface doesn’t actually implement anything, meaning we are free to either use or not use these variables in implemented class.

  • One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. 

    When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain. Following is an example:

// One interface can extend another.
interface A
{
void meth1();
void meth2();
}

// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A
{
void meth3();
}

// This class must implement all of A and B
class MyClass implements B
{
//method implementation statements
}


Click for NEXT article.

 

Please feel free to correct me by commenting your suggestions and feedback.

No comments:

Post a Comment