Monday, 26 August 2013

Why do we use Interface to define the methods and implement and define body of it in Concrete Class?

Why do we use Interface to define the methods and implement and define
body of it in Concrete Class?

Let me clear my question with an example. Suppose I have an interface I in
which method abc() is defined. I have another two class say A and B which
implements I and override abc() method.

Now my question is why do we user interface just to define the methods and
not implemented directly in a class without defining and implementing
interface?like...
interface I{
public void abc();
}
class A implements I{
@Override
public void abc() { ... }
}
class B implements I{
@Override
public void abc() { ... }
}
instead of
class A {
public void abc() { ... }
}
class B {
public void abc() { ... }
}

Explaination with small example will be very helpful. Thank You.

No comments:

Post a Comment