Monday, 20 April 2020

What is an Interface in Java? Why it is used?

An interface in Java is a pure abstract class. It consist only static final and abstract classes only. All the classes that implements interface must implement abstract methods otherwise declare that class as abstract classes.

There are so many uses for introduction of java interfaces, some of them are listed below
  1. In Java to implement multiple inheritance we use interfaces.
  2. I would say the main use is polymorphism, or the ability to perform the same operation on a number of different objects. If different objects all implement the same interface and have the same method, you can store all of those objects in a Vector, for example, and iterate through the Vector calling that method on each one. 
  3. One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation.
  4. the most important use for interfaces is to reduce coupling between components in your software.
  5. An interface allows to represent an agreement between classes on how they will talk to each other without being tied to the actual implementations.
  6. This allows us to replace implementations by others (very useful for testing, or changing use cases) without changing the compiled code.
 Among the above, the most useful and frequently used is implementing multiple inheritance.


The main advantages of interfaces are
  • we can achieve security because we are not highlighting our internal implementation.
  • Enhancement will become very easy, because without effecting outside person we can change our internal implementation.
  • Two different systems can communicate via interface.

Declaring an interface

Just like a class, an interface also be declared before using in your program. An interface in java is declared using "interface" keyword. The general syntax is

interface interface-name

{

 public static final variables;

 public void abstract methods;

}
  • The interface-name is any valid java identifier name.
  • By default all variables in an interface are public static final and methods are public void type.
  • The interface members are declared public because they must available to their child classes.

For example:
interface father

{



  int height;

  int weight 

  void age();

}
  1. When ever a class implements the above interface, it must implement the age() method, otherwise it generates a compile time error.If you don't want to implement, then declare that class as abstract.
  2. Whenever we are implementing an interface method compulsory it should be declared as public otherwise we will get compile time error.

The above two points are discussed in more details when we are implementing interfaces.

0 comments:

Post a comment

Find Us On Facebook

Contact Us

Name

Email *

Message *

C Programming

More

C++ Tutorial

More

Java Tutorial

More

software engineering

More

MS Office

More

Database Management

More

Advertisement

Top