I have two programs one implemented using interfaces and the other implemented using only classes -
I've read that the advantage of using an interface is that it can provide it's own implementation of methods of super class but that can be done using abstract classes or method overriding. What purpose does interfaces serve?
In what kind of hierarchy and situation using interface will be most beneficial?
INTERFACE
interface Shape
{
void area(int x, int y);
}
class Rectangle implements Shape
{
@Override
public void area(int length, int breadth)
{
System.out.println(length*breadth);
}
}
class Triangle implements Shape
{
@Override
public void area(int base, int height)
{
System.out.println(base*height);
}
}
public class ShapeUsingInterface
{
public static void main(String X[])
{
Rectangle r = new Rectangle();
Triangle t = new Triangle();
r.area(5, 4);
t.area(6, 3);
}
}
CLASS
class Shape
{
void Area(int x, int y)
{
System.out.println(x*y);
}
}
class Rectangle extends Shape
{
}
class Triangle extends Shape
{
@Override
void Area(int base, int height)
{
System.out.println((0.5)*base*height);
}
}
public class CalculateArea
{
public static void main(String X[])
{
Rectangle r = new Rectangle();
Triangle t = new Triangle();
r.Area(4, 5);
t.Area(6, 8);
}
}
To explain why interfaces are used, you have to first understand a problem with inheritance. It's called the Diamond Problem. Simply, if a class, D
inherits from two classes B
and C
, and B
and C
both inherit from the same class A
, which implementation of the methods from A
does D
get?
What we're left with is a method ambiguity that Java does not like! So the way of preventing this is to make sure that D
can only ever have one superclass, so it could inherit from either A
, B
or C
, but never more than one. So that prevents the problem, but we lose all of the perks that multiple inheritance offers!
Enter the Interface. This allows us to have the perks of multiple inheritance (referencing the single class as various different types) and still avoid the diamond problem, because the implementing class provides the method. This removes the method ambiguity.
This means that, for example:
public abstract class MyClass {
public void doSomething();
}
public class MyConcreteClass extends MyClass {
public void doSomething() {
// do something
}
}
You can either refer to an instance of MyConcreteClass
as
MyClass class = new MyConcreteClass();
or
MyConcreteClass class = new MyConcreteClass();
But never anything else, given this implementation. You can't extend
more classes because you'll potentially get the diamond problem, but you CAN include an Interface
public class MyConcreteClass extends MyClass implements Serializable {
}
All of a sudden, you can say..
Seralizable myClass = new MyConcreteClass();
Because myClass
is a Serializable
. This is excellent for decoupling classes from one another, when a method might not need to know that it is an instance of MyConcreteClass
, only that it has a necessary subset of methods.
In short: You can implement many interfaces, but you can only inherit one class.
Interface defines the contract. Say for example:
interface MusicPlayer
{
public void shuffle();
}
Now each class implementing the interface can have their own algorithm for implementing shuffle
method, its upto them. Abstract class is similar to interface, where an abstract class defines few methods to be common and leave other methods to be implemented in our own way. Like
abstract class MusicPlayer
{
public void turnOff()
{
//kill the app
}
abstract public void shuffle();
}
And using interfaces you can implement multiple interfaces but only extend a single class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With