In: Computer Science
Abstraction is the basis for good Object-Oriented design that is modular, reusable, and maintainable.
Interfaces and Abstract classes are two mechanisms in Java that provide high-level abstractions. An interface or abstract class is something which is not concrete, something which is incomplete.
For this discussion, what do you think is meant by “Programming to an interface”? What are some differences between Abstract Classes and Interfaces? Provide a simple example application with an interface definition and a class which implements the interface.
Interface : Interface in object oriented programming is done to achieve abstraction for the highly changeable program or code . It is basically a blur print or layout for the class. It consist of only static variables and abstract method which has no body.
Below is an example of an interface and how is it implemented in a class:
interface display{
void show();
}
class hello implements display
{
public void show()
{
System.out.println("Hello everyone");
}
public static void main(String args []){
hello obj = new hello ();
obj. show ();
}
}
In the above code an interface display is defined which has a method show() with no body defined . Class hello implements the interface display and then it can use its method and define its body accordingly. When an object of that class is created and is used to call the show() method then the function that is defined in the class hello is performed on calling the method show(). This way method show() of interface can be used by multiple classes with different functioning. It gives flexibility to use the code multiple times which is written once and you don’t have to write it again and again. It is also easy to maintain the codes.
Meaning of “Programming to an interface”:
By the above statement “Programming to an interface” it is meant that framing the classes based on the interfaces which defines the behavior or functioning or attributes of the class. In this process first the interface is created with its static variables and abstract methods and then the real classes are created implementing the interface.
Basically when there is a need for multiple implementations then programing to interface is done . It is a concept and not a keyword that is used for implementing interfaces. This design principal basically refers to the loose coupling of the different modules of an application . It is used when there is a dependencies among the modules . In such a scenario using interfaces helps to make changes to a particular module without changing all the other modules .
Difference between Abstract class and Interface :
Abstract class:
public abstract class Vehicle
{
public abstract void speed();
public abstract void color()
{
System.out.println(“Color of the vehicle is white”);
}
}
Interface:
public interface Vehicle
{
void speed ();
void color ();
}
Example of interface implementation :
import java.io.*;
interface shape
{
void show();
}
class square implements shape
{
int side = 4;
public void show()
{
System.out.println("The shape is square with number of sides " + side);
}
}
class Main
{
public static void main (String[] args)
{
square obj = new square ();
obj.show();
}
}
Screenshot :