Interface In Java

Interface In Java :

Difference between interface and abstract class :

  • An abstract class when there are some common features shared by all the objects.
  • An interface when all the features have different implementations for different objects.
  • All methods in an interface are implicitly (by default) public and abstract.
  • Interface we can have implement multiple interface concept of a “single class extending more than one class “.
  • It can be used to achieve loose coupling.
Interface In Java

Multiple Inheritance :

  • We cannot create multiple super classes or we can say parents classes or base classes. This is the main reason we can create interface. In interface we can declare function only but not create the body.

Interface :

  • An interface is a blueprint of a class. It has static constants and abstract methods.
  • It is used to achieve fully abstraction. So no need to make an interface as abstract as it is by-default “public abstract “.
  • An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces.
  • Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface.
  • Interfaces just specify the method declaration (implicitly public and abstract) and can only contain fields (which are implicitly public static final). Interface define any abstract methods and final fields (data member or data functions).
  • Interfaces do not specify any code, we can declare methods and data fields contain only constant. Therefore, it is the responsibility of the class that implements an interface to define the code for implementation of these methods.
  • An interface like that of an abstract class cannot be instantiated.Interface is similar to an abstract class in that its members are not implemented.
  • Java interface the methods have no bodies, so it species the methods without implementing them.
  • Interface keyword, java allows you to fully utilize the “One interfaces multiple methods” aspect of polymorphism.
  • Interfaces are designed to support dynamic method resolution at run time. For a method to be called from one class to another, both classes need to be present at compile time so the java compiler can check to ensure that the classes that the method signatures are compatible.
  • An interface is not a class.A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.
  • Variable declaration is compulsory (public, static and final). We cannot use private, protected, transient and volatile keyword with the variable declared in an interface.
  • Method declaration is compulsory to be public and static only, cannot use synchronized and native.
  • We cannot define constructor, instance and static initialization block in an interface.
  • Interfaces doesn’t contain constructors and they cannot be instantiated.
  • Interfaces can contain only constants and method signatures, but no implementation.
  • An interface is not extended by a class; it is implemented by a class.
  • If any one of the method is not implemented, then that implementation class should be declared as abstract.
  • We can create a reference variable to an interface.
  • An interface cannot implement another interface.
  • An interface can extend another interface.
  • A class can implement multiple interfaces.
  • An interface reference can point to objects of its implementing classes
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
  • You can’t mark an interface as final.
  • An Interface cannot extend anything but another interfaces.
  • Only public and abstract modifiers are allowed for methods in interfaces.
  • An interface contains zero or more abstract methods.
  • An interface reference can point to objects of its implementing classes.
  • Once an interface is written any third party vendor can implement it.
  • Interfaces can be used as reference type for the object of an implementing class.

Defining an interface :

Access specifirer interface-name
{
return - type method - name (parameter - list);
type final_varname1 = value;
}

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)
}
  • What is marker interface in java and why required?
  • 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.

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