Wednesday, January 1, 2020

Interfaces in Java

Java Interfaces 


Classes are the fundamental unit of Java programming, but the fundamental unit of object-oriented programming types. Classes define both implementation and design. The type defines the only design. Interfaces are the only way to define types in Java in abstract form. Interfaces contain no implementation. Interfaces provide complete Abstraction means you can not create the objects of Interfaces. Interfaces are the expression of pure design in Java. Classes can expand their own types by implementing one or more interfaces.

  • In Java, we can not only achieve Abstraction, with the help of Abstract classes but also with the help of Interfaces.
  • Interfaces provide a skeleton of a class.
  • All variables declared in an interface are public, static and final 
/****Declaring an Interface**********/
interface MyInterface{
//all variable(will be public static and final) 
//all methods(only declartions/no implementations)
}


  • All the methods declared in an Interface are public and abstract (a method without implementation).
  • The interface provides a protocol of behavior(methods) or how the behavior is implemented is the responsibility of that Individual class.
  • Syntactically an interface is declared the same as a class with a keyword interface.
  • The keyword “implements” is used to apply any interfaces in any class.
  • Interfaces provide a mechanism to provide multiple inheritance.
  • A single interface can be implemented by several classes. 

interfaces in java
Interfaces in Java



An interface can declare three kinds of members:

  • Constants //all fields are public static and final 
  • methods//all methods are public and abstract
  • nested classes and interfaces

An interface can be preceded by any of these three modifiers


  • public(without public modifier interface is only accessible inside its own package only)
  • abstract(can not be static,final,native or synchronized)
  • strictfp (for strict floating-point evaluation)

Why Interfaces are required?

  • The interface allows us to implement common behavior in two different classes.
  • Interfaces allow us to apply behaviors beyond classes and class hierarchy.
  • Consistent specification among unrelated classes.
  • Reduce coupling between the software components.
  • Applying multiple inheritances (multiple inheritances in Design).
  • Two or more classes may not achieve the same functionality(No duplicity of class).
  • Interfaces a great role in creating rich classes.

Extending Interfaces


Interfaces can also be extended but the fields will be hidden according to the concept of "Fields hiding" in the polymorphism. 

Interfaces can be inherited(Inheritance in Java) like classes,

For example,

/****Interfaces can be Inherited**********/
interface A {
int v1=1;
}
// interfaces can be inherited
interface B extends A
{
int v2=2;
int sum=v2+A.v1;
}

Multiple Inheritance

Several other object-oriented programming languages support a kind of Inheritence known as multiple inheritances. When a subclass extends two or more than two superclasses, it is known as multiple inheritances. This type of inheritance is not supported by Java. This can be provisionally applied in Java with the help of Interfaces.

For example, interface A and B are implemented by the class in the following example.



It means a class can implement two or more than two interfaces together, but can not extend two or more than two classes together.



Video Tutorial