Interface In Java

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).  
Interfaces specify what a class must do and not how. It is the blueprint of the class.
An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.
A Java library example is, Comparator Interface. If a class implements this interface, then it can be used to sort a collection.
Syntax :
interface <interface_name> {  
// declare constant fields
// declare methods that abstract
// by default.
}
To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static and final by default. A class that implement interface must implement all the methods declared in the interface. To implement interface use implements keyword.

Comments

Popular posts from this blog

Flutter : Introduction

How to release your Flutter app

Firebase Cloud Messaging