Pages

Tuesday 24 February 2015

What is Marker interface? How is it used in Java?
The marker interface is a design pattern, used with languages that provide run-time type information about objects. It provides a way to associate metadata with a class where the language does not have explicit support for such metadata. To use this pattern, a class implements a marker interface, and code that interact with instances of that class test for the existence of the interface. Whereas a typical interface specifies methods that an implementing class must support, a marker interface does not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class. There can be some hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used.
 Java utilizes this pattern very well and the example interfaces are:
·        java.io.Serializable
·        java.rmi.
·        java.lang.
·        javax.servlet.SingleThreadModel
·        java.util.EvenListener
The "instanceof" keyword in java can be used to test if an object is of a specified type. So this keyword in combination with Marker interface can be used to take different actions based on type of interface an object implements.

What is the difference between final, finally and finalize?
 “final” is the keyword to declare a constant AND prevents a class from inherting subclasses. And overriding  super class methods.
“finally” is a block of code that always executes when the try block is finished, unless System.exit() was called.
“finalize()” is an method that is invoked before an object is discarded by the garbage collector.

No comments:

Post a Comment