Wednesday, April 3, 2013

Interfaces


In our previous  post we have discussed about the Late Binding and Early  Binding now we will discuss about how to implement Late Binding

-          We implement Late Binding through interfaces in Java. Classes is like a structure and it can be called is a fully implemented structure (providing bodies for every definition).

       -        Interfaces is a fully unimplemented structure 

i.e:  No function definition has a body inside an interface. The function will not have at least Zero body.

      -       Just as we define classes using the keyword ‘class’ we define interfaces using the keyword ‘interfaces’                                 

EX:  interfaces xyz
{
Public void funx();
Public void funy();
}

Note: Every definition inside an interface should be public.

Interview Q) 

When we compile xyz.java we get .class file even though the function funx(),funy() have no body.why and how?

Ans)  There are three types of structures in java , as given below.

i)                    Fully implemented structure                                              ---   class

ii)                   Fully un implemented structure                                        ---    Interfaces

iii)                 Partially implemented / unimplemented  structure    ---   Abstract class

The job of the java compiler is to convert any valid java structure(source code) into its equivalent  byte code.

The above example xyz.java, it’s a valid java structure. Hence it got compiled and xyz.class file was produced.

-          As we can extend one class to another class, we can implement an interface  to a class the keyword ‘implements’.

-          We should keep in mind that whenever we implement an interface to a class. we have to provide bodies for the definitions presents in the interfaces,inside the class that is implementing the interfaces.

-          References can be defined for any valid java structure but the corresponding object can be created only for classes, In other words, “we can define references in java for everything which can be represented in the form of a .class file, but the corresponding objects can be created only for classes.”

Ex:
Class A implements xyz
{
Public void functionx()
{
// Defining bodies inside the class for the definition inside the interface, where class implements the interface
}
Public void function()
{
// Defining bodies inside the class for the definition inside the interface, where class implements the interface

}
Public static void main(String args[])
{
Xyz x1 = null; // we can assign null to the reference of an interface
Xyz x2 = new xyz(); // completion error : new operator cannot be used with interfaces
A a1= new A();
a1.functionx();
a2.functiony();

Interview Q

In the above program, we can even directly define function() and function() inside Class A and call them by creating abject of class A then ,what is the use of implements ‘keyword’?

Ans:

Using ‘implement’ keyword, we can achieve ‘Late Binding’.

Differences between ‘extends’ and ‘implements’

-          Using ‘extends’ keyword we get properties of one structure to another structure.
-          Using ‘implements’ keyword we provide bodies for function definitions inside a structure(interface).
-          In side simple terms, extends means getting and implemts means giving.
-          Therefore functionality of ‘implements’ is quite opposite to that of extends.
-          Here, ‘implements’ is qgainst ‘inheritance.
A more general example as follows
Father -> son àchild1,child2,child3
Father is super class ,son is a sub class, child 1,2,3 are integers.
Son has to extend from father and can implement to all children.
This is the reason , why ,we can extend only one class but can implement any no. of interfaces.