Link to home
Start Free TrialLog in
Avatar of mohet01
mohet01Flag for India

asked on

interface keyword

hello
I have been through interface keyword and the class that can implement it.
But I would exactly understand the scenarios in which the usage or power of interface is? Example?

Sham
Avatar of mohet01
mohet01
Flag of India image

ASKER

hello
I know core java.
Sham
Avatar of Gurvinder Pal Singh
I think very simple explanation is to think about interface as some description of
external properties of the object irrespective of its internal structure.
Let's think that in some program you deal with many planar figures and for certain kind of
operation the only thing important for you is the existence and value of their area.
So you may think that all of these object have some method which produces this number
and for many external tasks it is immaterial for you how they calculate the area - it is their internal
business, and your program only cares about the ability of these object to return you this number.
Then when you implement these objects you will of course need to define such method and you need
to broadcast to external objects that you indeed have such method, that's why you declare
in the class declaration that this object implements this method (and it does not matter for the external programs that
for the square and the circle the actual calculation inside this method will be different).
In some cases you want to know that not just one method but that several uniformly defined methods
do exist for these object (say, maybe along with the area another external program is also interested in the perimeter of the
planar figures we were talking above), this set of methods together in fact provides
description of the objects important to some external programs which interact with them.
 This set of methods becomes interface which
stresses some important feature of a group of objects and does not care about other details
of their construction.
Interfaces allow very easily to split the job of writing big programs between different programmers - as those
who manipulate with the objects do not need to be concerned with their internal structure,
as long as they know how these objects behave.
 Perhaps, this may help as a very brief explanation.
Of course in Java books you'll find it described in much better way.
I kind of understood them myself when someone gave me this rather common example about the areas.

This is a simple example of the use of Shape interface, which I mentioned above,  in order to pass to the method a container (ArrayList in this case) with different objects implementing the same interface, i.e. in certain sense exhibiting the same behavior.
Methods which use this container as parameter, can be written by different people who do not need to deal with the
details of how area and perimeter are calculated inside each specific object, the only thing this method
has to know is how these objects behave, i.e. that all of them can calculate and return their area and perimeter.

import java.util.ArrayList;


public class CreationOfShapes {

    ArrayList<Shape> myShapes;

    public CreationOfShapes(){

        Circle c = new Circle(5f);

        Square s = new Square(4f);

        Rect r = new Rect(3f,5f);

        myShapes = new ArrayList<Shape>();


        myShapes.add(c);
        myShapes.add(s);
        myShapes.add(r);

        System.out.println("Total area: " + ManipulationsWithShapes.sumAreas(myShapes));
            System.out.println("Max perimeter: " + ManipulationsWithShapes.maxPerimeter(myShapes));


  


    }

             public static void main(String [] args){
        new CreationOfShapes();
    }

}

Open in new window


import java.util.ArrayList;

public class ManipulationsWithShapes {

    static float sumAreas(ArrayList a){
        float sum=0.0f;
        for(int j=0; j<a.size(); j++){
            Shape ss = (Shape)a.get(j);
            sum += ss.area();
        }
       return sum;
    }

      static float maxPerimeter(ArrayList a){
        float perim =0.0f;
        for(int j=0; j<a.size(); j++){
            Shape ss = (Shape)a.get(j);
            if(ss.perimeter() > perim)perim = ss.perimeter();
        }
       return perim; 
    }


}

Open in new window


class Circle implements Shape{
    float radius;

    Circle(float x){
        radius = x;
    }

    public float area(){
        return 3.1415f*radius*radius;
    }

   public  float perimeter(){
        return 2f*3.1415f*radius;
    }

}


class Square implements Shape{
    float edge;

    Square(float x){
        edge = x;
    }

    public float area(){
        return edge*edge;
    }

   public  float perimeter(){
        return 4f*edge;
    }

}


class Rect implements Shape{
    float a;
    float b;

    Rect(float x, float y){
        a = x;
        b = y;
    }

    public float area(){
        return a*b;
    }

   public  float perimeter(){
        return 2f*(a+b);
    }

}

Open in new window


public interface Shape {
   public float area();
    public float perimeter();
}

Open in new window

Avatar of mohet01

ASKER

Hello gurvinder
How do I publish api's using interface concept?
Sham
Avatar of mohet01

ASKER

Hello gurvinder
what is the meaning, when u say 'interface define a contract'
Any example for this and publishing api's?
Sham
I am not gurvinder, but I can explain what means
"interface defines a contract".

It is equivalent to saying that if object implements interface, then
other objects (and programmers) can expect certain kind of behavior from this object.
In particular, in response to some requests (methods) the object will provide
certain reaction, e.g. return value of expected type when given parameters or requests
of expected type. This is as if these objects, when declaring interface,
promise to fulfill some specified obligations (to fulfill some contract, imposed by this iterface).


Don't understand what is your question about publishing API;
perhaps gurvinder will explain it to you.


Avatar of mohet01

ASKER

Sorry for referring somebody here, I would not do that again
Sorry again, if i again discouraged somebody
Sham
Absolutely, no problem, it was just a joke :)
Avatar of mohet01

ASKER

Hello
If i refer the link: http://benpryor.com/blog/2006/08/23/java-advantages-of-interfaces/
I would like to understand the concept of:

1) 'interface define a contract'
2) "publishing api using interface concept"

with an example.

Sham




I thought I tried to explain what means "define a contract" above - is there something
not understandable in my explanation, please let me know what it is, maybe
I'll try to clarify - it seems clear to me, so we should find some understanding

About publishing API using interface concept: as interface
is in fact a set of method signatures - then when you say
that your object implements interface - it is equivalent
to publishing (announcing) that this object has a set of methods
with certain parameters and returning some values of specified type/class -
this is equivalent to publishing API, as API is
also enumeration of the set of methods to which object provides
expected response.

Let me know what is it in particular which is not obviousd about it.
Avatar of mohet01

ASKER

1) publishing is clear to me,
2) But can u give an example for  "interface defines a contract"Am not clear about this

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mohet01

ASKER

Done