Java Source World A Java quick reference blog
Interface in Java:
- Its a class like structure which can contain combination of all undefined methods and static constants.
Example:
public interface InterfaceTest {
public static String DATA = "data"; //static variable
public abstract String save();//undefined method
}
Interface cannot be instantiated directly. Interface has to be implemented by some class to inherit the features.
The implementing class should provide definition for all inherited methods otherwise the class has to be made abstract.
If a class implements all inherited methods then it can be instantiated.
Example:
public interface InterfaceTest {
public static String DATA = "data"; //static variable
public abstract String save();//undefined method
}
class MyClass implements InterfaceTest{
@Override
public String save() {
return "Saved";
}
}
Interface doesn't have any constructor.
All members in interface are by default public.
By default the data member of an interface are "static" and "final". (static constants).
Interface methods are by default "public" & "abstract".
Interface methods cannot be made "static" & "final".
Interface methods cannot have any definition.
Interface cannot be made "final" as it is "abstract" by default.
If we do not make interface as public then it can be inherited only in the same package.
Hence to make interface available to other packages we need to make it public.
Data members or member methods of an interface cannot be made private.
If a class doesn't override all inherited methods of an interface then it should be made abstract.
Example:
public interface InterfaceTest {
public static String DATA = "data"; //static variable
public abstract String saveDetails();//undefined method
public abstract String getDetails();//undefined method
}
abstract class MyClass implements InterfaceTest{
@Override
public String saveDetails() {
return "Saved";
}
public abstract String getDetails();//undefined method
}
class chileTwo extends MyClass{
@Override
public String getDetails() {
return "Saved";
}
}
A class in java can implement more than one interface.
Example:
interface x{
void m1();
}
interface y{
void m2();
}
class TestInterface implements x, y{
public void m1(){
//do something
}
public void m2(){
//do something
}
}
One interface can extend another interface to inherit the features.
Example:
interface x{
void m1();
}
interface y extends x{
void m2();
}
class TestInterface implements y{
public void m1(){
//do something
}
public void m2(){
//do something
}
}
An interface can extend more than one interface in java
Interface Advantages:
* They can be used to reuse the constants in the entire applications.
* Interface can be used to impose contract on same class to provide definition for all inherited methods.
* Limited polymorphism / Dynamic Method Dispatch can be implemented through interfaces.
* Limited form of multiple inheritance can be implemented through interfaces.
You may like the fallowing posts:
No comments:
Post a Comment