Wednesday, 22 August 2018

Java Interfaces


Interface is a pure abstract class.They are syntactically similar to classes, but you cannot create instance of an Interface and their methods are declared without any body. Interface is used to achieve complete abstraction in Java. When you create an interface it defines what a class can do without saying anything about how the class will do it.
 

Syntax :  interface interface_name { }
Example:
interface Moveable
{
 int AVERAGE-SPEED=40;
 void move();
}

NOTE : Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.

Rules for using Interface
  • Methods inside Interface must not be static, final, native or strictfp.
  • All variables declared inside interface are implicitly public static final variables(constants).
  • All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.
  • Interface can extend one or more other interface.
  • Interface cannot implement a class.
  • Interface can be nested inside another interface.
Example of Interface implementation
interface Moveable
{
 int AVG-SPEED = 40;
 void move();
}

class Vehicle implements Moveable
{
 public void move()
 {
  System .out. print in ("Average speed is"+AVG-SPEED");
 }
 public static void main (String[] arg)
 {
  Vehicle vc = new Vehicle();
  vc.move();
 }
}
Output
Average speed is 40.+

Difference between Class and Interface
Class
Interface
In class, you can instantiate variable and create an object.
In an interface, you can't instantiate variable and create an object.
Class can contain concrete(with implementation) methods
The interface cannot contain concrete(with implementation) methods
The access specifiers used with classes are private, protected and public.
In Interface only one specifier is present i.e., public

Difference between an interface and an abstract class?

Abstract class
Interface
Abstract class is a class which contain one or more abstract methods, which has to be implemented by its sub classes.
Interface is a Java Object containing method declaration but no implementation. The classes which implement the Interfaces must provide the method definition for all the methods.
Abstract class is a Class prefix with an abstract keyword followed by Class definition.
Interface is a pure abstract class which starts with interface keyword.
Abstract class can also contain concrete methods.
Whereas, Interface contains all abstract methods and final variable declarations.
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes.
Interfaces are useful in a situation that all properties should be implemented.

When to use Interface and Abstract Class?
  • Use an abstract class when a template needs to be defined for a group of subclasses
  • Use an interface when a role needs to be defined for other classes, regardless of the inheritance tree of these classes
Must know facts about Interface
  • A Java class can implement multiple Java Interfaces. It is necessary that the class must implement all the methods declared in the interfaces.
  • Class should override all the abstract methods declared in the interface
  • The interface allows sending a message to an object without concerning which classes it belongs.
  • Class needs to provide functionality for the methods declared in the interface.
  • All methods in an interface are implicitly public and abstract
  • An interface cannot be instantiated
  • An interface reference can point to objects of its implementing classes
  • An interface can extend from one or many interfaces. Class can extend only one class but implement any number of interfaces
  • An interface cannot implement another Interface. It has to extend another interface if needed.
  • An interface which is declared inside another interface is referred as nested interface
  • At the time of declaration, interface variable must be initialized. Otherwise, the compiler will throw an error.
  • The class cannot implement two interfaces in java that have methods with same name but different return type.
New features added in interfaces in JDK 9 
From Java 9 onwards, interfaces can contain following also
  1. Static methods
  2. Private methods
  3. Private Static methods
Summary:
  • The class which implements the interface needs to provide functionality for the methods declared in the interface
  • All methods in an interface are implicitly public and abstract
  • An interface cannot be instantiated
  • An interface reference can point to objects of its implementing classes
  • An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces


Previous Post
Next Post

0 comments: