Java Reference
In-Depth Information
marker interface? Why would any class implement a marker interface? As the name suggests, a marker interface is
used to mark the class with a special meaning that can be used in a particular context. The meaning added to a class
by a marker interface depends on the context. The developer of the marker interface has to document the meaning
of the interface and the consumer of the interface will make use of its intended meaning. For example, let's declare a
marker interface called Funny , as shown below. The meaning of this Funny interface is up to the developer who uses it.
public interface Funny {
// No code goes here
}
Every interface defines a new type, so does a marker interface. Therefore, you can declare a variable of type Funny .
Funny simon = an object of a class that implements the Funny interface;
What can you access using the variable simon , which is of type Funny ? You cannot access anything using the
simon variable, except the all methods of the Object class. You could do that without implementing the Funny
interface to your class, too. Typically, a marker interface is used with the instanceof operator to check if a reference
type variable refers to an object, whose class implements the marker interface. For example, you may write code like
Object obj = any java object;
...
if (obj instanceof Funny) {
// obj is an object whose class implements the Funny interface. Display a message on the
// standard output that we are using a Funny object. Or, do something that is intended
// by the developer of the Funny interface
System.out.println("Using a Funny object");
}
Java API has many marker interfaces. The java.lang.Cloneable , java.io.Serializable , and java.rmi.Remote
are a few of the marker interfaces in the Java class library. If a class implements the Cloneable interface, it means that
the developer of that class intended to allow the cloning of the objects of that class. You need to take additional steps of
overriding the clone() method of the Object class in your class, so the clone() method can be called on objects of your
class because the clone() method has been declared protected in the Object class. Even though your class overrides the
clone() method, the object of your class cannot be cloned until your class implements the Cloneable marker interface.
You can see that implementing Cloneable interface associates a meaning to the class that its object can be cloned. When
the clone() method of the Object class is invoked, Java performs a check if the object's class implements the Cloneable
interface. If the object's class does not implement the Cloneable interface, it throws an exception at runtime.
Java 5 introduced a specialized form of interface known as annotations. It can be used to associate a meaning to
any element, for example, a class, a method, a variable, a package, etc., of a Java program. I will discuss annotations in
the next chapter.
Functional Interfaces
An interface with just one abstract method is known as a functional interface. The static and default methods are
not counted to designate an interface a functional interface. No additional steps, other than what I have already
discussed, are needed to declare an interface as functional. I will discuss more about functional interfaces in the
chapter on lambda expressions. The Walkable and Swimmable interfaces are examples of functional interfaces as they
contain only one abstract method. The Singer interface is an example of a non-function interface as it contains more
than one abstract method.
 
Search WWH ::




Custom Search