Java Interface
- Any service requirement specification (SRS) is considered as an "interface".
- Inside "interface", every method is always abstract whether we are declaring or not hence, the interface is considered as 100% pure abstract class.
- Any contract between client and service provider is considered as an "interface".
Interface declaration and implementation
- For each and every method present in the interface we have to provide implementation otherwise, we have to declare the class as abstract then next level child class is responsible for providing the implementation.
- Every method present in an interface is always public and abstract we are declaring or not hence, whenever we are implementing an interface's for every method compulsory we should declare as public otherwise we will get Compile-Time Error.
example:-
interface InterfaceDemo
{
void m1( );
void m2();
}
abstract class Parent implements InterfaceDemo
{
public void m1()
{
}
}
class Child extends Parent
{
public void m1()
{
}
}
extends keyword v/s implements keyword
- A class can extends only one class at a time.
- An interface can extends any number of interface simultaneously.
- A class can implements any number of interface simultaneously.
- A class can extend another class and can implement any number of interface simultaneously.
interface method
- Every method present inside interface is always public and abstract whether we are declaring or not.
- Inside interface the following method declarations are equal.
- Every interface method is always public and abstract we can't declare interface method with the following modifiers.
interface variables
- An interface can contain variables also. The main purpose of interface variable is to define requirement level constants.
- Every interface variable is always public, static, final whether we are declaring or not.
- Within the interface the following variable declarations are equal.
Conclusions:-
- For interface variables compulsory we should perform initialization at the time of declaration otherwise, we will get Compile Time error.
- Inside implementation class, we can access interface variables but we cannot modify the values.
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home