Interfaces, examples of
Interface is medium between client and implementation.
client(main class) does not know implementation of functionality, he simply wants to use that functionality.
functionality nothing but declaration of method in interface.
implementation is making functionality to work nothing but class.
if you are creating class and assigning to interface nothing but you want to call only methods which are declared in the interface.
if client does not want to be known implementation of method, then we should assign object to interface.
you will learn more about interface usage in server side programming.
interface PersonInterface {
public abstract void display() ;
public abstract void show();
}
class Person1 implements PersonInterface {
public void display() {
}
public void show() {
}
public void classMethod{
}
}
class Main {
public static void main(String args[]) {
PersonInterface personI = new Person1(); //
personI.display(); // compile
personl.show(); // compile
personI.classMethod(); // compile error
}
}
Person1 class has 3 methods in those 2 methods are overridden from interface, 1 method in class's own implementation method.
but once you are assigning object to reference of interface, scope of interface is only visible to 2 methods which are implemented from interface, remaining methods ( which are implemented own ) are out of scope.
even if you call any other method which is not defined in interface it throws compile error.