Code 1:
public class User1 implements MyInterface
{
@Override
public void doCalculation() { }
}
public class User2 implements MyInterface
{
@Override
public void doCalculation() { }
}
interface MyInterface
{
public void doCalculation();
}
Code 2:
public class User1
{
public void doCalculation() { }
}
public class User2
{
public void doCalculation() { }
}
Here in my Code 1 I have MyInterface
which has an empty method doCalculation()
.
That doCalculation()
is used by user1 and user2 by implementing MyInterface
.
Where as in my Code 2 I have two different classes with defined doCalculation()
method.
In both the cases code1 and code2 I myself have to write the implementation. My method doCalculation()
is just an empty method.
So what is the use of MyInterface
here?
It only provides me the method name or skeleton (is that the only advantage of interface)?
Or else would I save any memory while using
MyInterface
?Is that, it only provides the empty method for an class which implements it, then why not I define it by myself as I have done in my code2.
More than that is there any more advantage on using an interface.