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.
Note: In Java8 Release, Interfaces contains both static and default methods also.
There are so many uses for introduction of java interfaces, some of them are listed below
The main advantages of interfaces are
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
For example:
The above two points are discussed in more details when we are implementing interfaces.
There are so many uses for introduction of java interfaces, some of them are listed below
- In Java to implement multiple inheritance we use interfaces.
- 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.
- 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.
- the most important use for interfaces is to reduce coupling between components in your software.
- An interface allows to represent an agreement between classes on how they will talk to each other without being tied to the actual implementations.
- This allows us to replace implementations by others (very useful for testing, or changing use cases) without changing the compiled code.
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(); }
- 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.
- 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
Note: only a member of this blog may post a comment.