Interface in Java
Hello friends today we learn one of the most important topic in Java; Interface in Java
The interface keyword is used to declare an interface. Here is a simple example to declare an interface:
/* File name : NameOfInterface.java */
import java.lang.*;
//Any number of import statements
public interface NameOfInterface
{
//Any number of final, static fields
//Any number of abstract method declarations\
}
Interface is a special class which can be implemented by sub-classes. (Got implemented by sub-classes, or substantiated). So, Interface is a special class suitable for generalization of some concreted classes or abstract classes. It allows subclasses to be implemented with different data definition and method implementation.
Some Important points related to Interface
- Interface is a special class in Java.
- Interface has no data.
- Interface has only abstract methods (no implementation)
- Interface is more vague than abstract. Abstract is also vague. Normal class is concrete.
- So, interface has no data and no implementation.
- Then, why we need interface?
- Interface is like header file in C language.
How Interface similar to class
- An interface can contain any number of methods.
- An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
- The byte code of an interface appears in a .class file.
- Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
No comments:
Post a Comment