Interfaces and Abstraction in Java
What are Interfaces and Abstraction in Java?
Interfaces are used in Java to achieve abstraction. Abstraction is a critical concept in Object-Oriented Programming and refers to the level of separation between an interface in Java (which only allows you to define functionality, not implementation) and Classes in Java ( which contain the actual methods being implemented).
Abstraction is an important tool because it helps to reduce the complexity of a program and the efforts of the developer and to improve the overall security of the program. This is done by hiding complex and unnecessary code from the user.
Interfaces are an important tool for software developers as Java only allows a class to extend one abstract class at a time. In order to work around this, we can use interfaces as Java allows a class to implement multiple interfaces.
Generally, an interface can be defined as a container that stores only calls to the methods to be implemented in a program. An interface only stores the method signature and not the method definition. Method signatures allows for an Interface to achieve complete abstraction by hiding the method implementation from the user.
An abstract class is a class that is declared as abstract. It allows you to create functionality by writing methods containing logical code that an interface can implement or override. In order for an interface to be implemented, a class must specify that it implements said interface as well as contain all the implementations of the methods called by the interface.An abstract method is defined as a method that is declared without containing any implementation code and it is written without curly brackets and is followed by a semi-colon. An abstract method can only be used in an abstract class.
Due to the fact that an interface implements methods declared in other classes, the interface itself cannot be instantiated directly. Similarly an interface cannot contain a constructor as the interface itself cannot be used to create new objects. We can also not override an interface method if it's visibility is set to private because, by default, interface's methods and attributes need to be abstract and public. This is due to the fact that they need to be visible to the interface in order for the interface to use them.

Comments
Post a Comment