Interface In Java

Interface In Java :

  • Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface.
  • An interface is a blueprint of class. It has constants and abstract methods. We will see in the past abstraction tutorial interface give 100% abstraction, so it is called fully abstraction.
  • Java Interface also represents IS-A relationship.
  • The interface definition states the names of the methods and their return types and argument signatures. There is no executable body for any method that is left to each class that implements the interface.
  • Interface functions should be public and abstract. All the methods in an interface are abstract methods. 
  • Interface fields should be public and final.
  • Use the Keyword interface to define an interface.
  • A class implementing an interface should use the keyword implements.
  • No objects can be created from an interface.
  • We cannot define constructor, instance and static initialization block in an interface.
  • An Interface can extends one or more interfaces.
  • You can define a reference of type interface but you should assign to it an object instance of class type which implements that interface.
  • Interface is similar to an abstract class in that its members are not implemented. Once it defined, any number of classes can implement an interface.
  • Variable declaration is compulsory (public, static and final).
  • You can’t mark an interface as final.
  • Only public and abstract modifiers are allowed for methods in interfaces. Method declaration is not allowed with static, final, private, protected, synchronized, strictfp or native.
  • An interface can extend multiple interfaces. Interfaces can be used as reference type for the object of an implementing class.
  • Interfaces can contain only constants and method signatures, but no implementation.
  • Interfaces cannot be instantiated.
  • An interface can be extended by another interface.

Use interface

  1. It is use to achieve fully abstraction.
  2. By using interface we achieve multiple inheritances.
  3. It can be used to achieve loose coupling.
  4. To hide coding to end user and for security reasons we will use interface and initialization also not required for interface

Syntax

public interface Interface-name{
	//Any number of final, static fields
  	//Any number of abstract method declarations
}

Rules – 1

interface student
{
    static void display() //public or final not allowed 
    { 
    }
}

Rules – 2

interface student
{
    public void display(); 
	abstract void display(); //interface allow abstract method
}

Rules – 3

interface student
{

     int a = 0; //interface variable
    //private, protected
    //public, static, final, final static (only three keyword use)
}
  • Rules

Member variables

  • Can be only public and are by default.
  • By default are static and always static.
  • By default are final and always final.

Methods

  • Can be only public and are by default.
  • Method declaration is compulsory to be public and static only

Interface variable

  • Interface can contain variables the main purpose of these variables is to specify.

public: To make this variable  available for every implementation class.

static: Without existing object also implementation class can access this variable.

final: Implementation class can access this variable but can’t modify.

Constants Valid declaration Not valid declaration
Every interface variable is always public, static and final whether we are declaring or not.   interace variableDeclaration {    int x = 10; //Valid    int y; //Not valid } int x = 10; public int x = 10; public static int x = 10; public static final int x = 10; final int x = 10; public final int x = 10; int x; private int x = 10; public int x = 10; transient int x = 10; volatile int x = 10; public static final int x = 10;

Interface declare value implementation no modify

  • Inside implementation classes we can access interface variables but we can’t modify there values.
interface student 
{
	int x = 10; 
}

class studentvalue implement student
{
	x = 20; //Error
	int x = 20; //Valid
}

Extending interface

  • An interface can extends another interface like the way to the class extends another class using extends keyword. The child interface inheritance the method and member variable of parent interface.

Marker interface (Tagging interface)

  • Marker interface in java is interface with no member variable or methods or in simple word empty interface called marker interface in Java, also known as tagging interface. E.g.  Serializable, Clonnable.
  • Marker interface used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning.

Example:

Interface MarkerInterface{

}

Nested Interface

  • An interface can have another interface i.e. known as nested interface.

Interface contain a set of abstract methods and static data members.

  • Interface also known as a prototype for a class.
  • All methods defined in an interface are only abstract methods means we can declare these methods.
  • Abstract method contains only the declaration for a method without any implementation details.
  • Implementation of abstract method is defined in the class implementing the interface.

Prototype

  • In a normal class definition, each method has a prototype and a body. A prototype is also called a specification because it species the name, parameters, and return type of the method. The body is called the implementation because it implements the specification.
  • Java compiler adds public and abstract keywords before the interface method and public, static and final keyword before data members.
  • A class implements interface but one interface extends another interface.

Can we instantiate an interface?

  • You can’t instantiate an interface directly, but you can instantiate a class that implements an interface.

Can we create an object for an interface?

  • Yes, it is always necessary to create an object implementation for an interface. Interface can’t be instantiate in their own right, so you must write a class that implements the interface and fulfill all the methods defined in it.

Can you declare an interface method static?

  • No, because methods of interface is abstract by default and static and abstract keywords can be used together.

Can an interface be final?

  • No, because its implementation is provided by another class.

Can we define private and protected modifiers for variables in interfaces?

  • No, they are implicitly public.

When can an object reference be cast to an interface reference?

  • An object reference can be cast to an interface reference when the object implements the referenced interface.

Can a class have an interface?

  • Yes, it is known as nested interface.

Can an interface have class?

Can we create an object of an interface?

  • interface is abstract and we can not create an object of abstract class.
  • interface doesn’t any constructor defined in it.
  • Yes, they are static implicitly.

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in