Saturday, 31 August 2013

Interfaces - Basics & Marker Interfaces

Q1. Is Iterator a Class or Interface?
Ans: Iterator is an interface

Q2. Can we instantiate an object of an interface?
Ans. No, we can't. However, a class can contain an interface object inside it. 
Ex: If Window is an interface and there is a class WindowDecorator (irrespective of fact that whether class implements the interface or not), following is allowed:
class WindowDecorator {
    protected Window decoratedWindow; // the Window being decorated
  .........
  .........
However, one can't declare an object of Window, using new method i.e. following is not allowed
   Window w = new Window();

Q3. Can interfaces be nested or inherited?

Ans Yes. Like classes, interfaces can be nested or inherited to any level. However, there is no practical use of nested interfaces.

Q4. Is it possible to define a class inside interface?
Ans. Yes. It’s convenient to nest a class inside an interface when you want it.

Use:

1. This technique is sometimes used where the class is a constant type, return value or method argument in the interface.
2. When a class is closely associated with the use of an interface it is convenient to declare it in the same compilation unit. This proximity also helps ensure that implementation changes to either are mutually compatible.
3. One more reason is to have modifiable data within an interface (As you may know all fields in an interface are implicitly final). By declaring a class inside the interface you can have common MODIFIABLE data.

NOTES:

1. The class is always public and static and operates as a top level class. 
2. The static modifier does NOT have the same effect on a nested class as it does with class variables and methods.
3. The class methods cannot call the methods declared in the interface.

Q5. Can interface methods be made as protected or private, when a class implements these methods?

Ans. You can choose to explicitly declare the methods in an interface as public, but they are public even if you don’t say it. So when you implement an interface, the methods from the interface must be defined as public. Otherwise, they would default to package access, and you’d be reducing the accessibility of a method during inheritance, which is not allowed by Java compiler.

Q6. Does Java allow multiple inheritance?

Ans. Java does not allow multiple inheritance in classes, but it’s allowed in interfaces.

Q7. Can an anonymous class be declared as implementing an interface and extending a class?
Ans. An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

Q8. Can a class implement two interfaces which has got methods with same name and signatures or two interfaces with same variable names?
Ans. Yes, a class implement two interfaces which have got methods with same name and signatures. However, in case of same variable names, if variable is not declared in implementing class, the compiler will throw "field ambiguous" error.

Q9. What happens when a class is implementing two or more interfaces (i.e multiple inheritance) having a method with same names?
Ans. The difficulty occurs because overriding, implementation, and overloading get unpleasantly mixed together. However this works, as long as methods differ in the signature. But, overloaded methods cannot differ only by return type. In such a case, compiler gives an error.

interface I1 { void f(); }
interface I2 { int f(int i); }
interface I3 { int f(); }

class C {
     public int f() { return 1; }
}

class C2 implements I1, I2 {
     public void f() {}
     public int f(int i) { return 1; } // overloaded
}

class C3 extends C implements I2 {
     public int f(int i) { return 1; } // overloaded
}

class C4 extends C implements I3 {
     // Identical, no problem:
     public int f() { return 1; }
}

// Methods differ only by return type:
//! class C5 extends C implements I1 {}
//! interface I4 extends I1, I3 {}


Q10. Explain marker interface.
Ans. An interface contains method declarations (no implementation). Actual declaration and implementation is in Object class. A class can implement this interface simply by naming it in its implements clause without having to implement any methods.

A marker interface is just empty. It doesn’t contain any methods or variables. It’s just to tag the classes implementing the given interface. Hence, they are also known as tagged interfaces.

Ex: Serializable and Cloneable interface

In marker interfaces, Java code checks whether an object is an instance of the interface using the instanceof operator. For instance, if you call clone() method on an object without implementing cloneable interface, it throws CloneNotSupportedException.


Q11. How JVM will behave when it find some class is implementing Marker interface? Do JVM need to put some extra code in that class to handle marker interface implementation?

Ans. JVM does NOTHING at all here. It is the responsibility of the programmer to check with “objects instanceof Interface" and find out whether or not interface is implemented. As in the case of Cloneable interface, clone() method takes care of it.

protected Object clone () {

      if (this instanceOf Cloneable) {
            ....
      }
      else
      {
            throws CloneNotSupportedException;
      }
}

Q12. Explain clone() method.

Ans. clone() is a method in the Java programming language for object duplication. Since objects in Java are manipulated through reference variables, there is no direct way to copy an object.

Default implementation of clone() method throws a CloneNotSupportedException. Classes that want to allow cloning must implement the marker interface Cloneable and should override implementation of the clone() method. Remember that you must make it public and inside the method your first action must be super.clone(). Since the default implementation of Object.clone only performs a shallow copy, classes must also override clone to provide a custom implementation when a deep copy is desired.


Clone() is method in Object class, like wait(), sleep(), notify() and notifyall().


Q12. What happens if I don’t implement Cloneable interface & provide clone method on my own.

Ans. As a standard practice, we should call super.clone() in our clone implementation, which ends up calling Object’s clone() method and throws an exception if I don’t implement Cloneable interface. Now, if I forget to call super.clone() or don’t do it intentionally, in other words, there is no call to Object’s clone method, my program will compile and run successfully even if I don’t implement Cloneable interface. This is because; in this case it’s treated as a normal method and not a marker clone method.

No comments:

Post a Comment