Interface

Interface

  • Java does not support multiple inheritance means that classes in java can not have more than one super class.
  •  Java provides an alternet approach known as interface to support the concept of multiple inheritances.
  • Interface is syntactically similar to classes but they contain instance variable and their methods are declared without anybody.
  • The major difference between class and interface is that interface defines only abstract methods and final fields.
  • Interface is designed to support dynamic method resolution at runtime.
  •  It is the responsibility of the class that implements an interface to define the code for implementation of these methods.
Syntax:

interface interface-name

{

variable declaration;

method declaration;

}

  • Here, interface is the keyword and interface name is any valid java identifier.

  •  Here all variables are declared as constant.
  •  Method declaration will contain only a list of methods without any statements.
  •  Methods in interface have no definition.
  • Variable declared inside the interface are implicitly final and static so it cannot be changed by implementing a class.
Implementing an interface.
  • Interfaces are used as superclass whose properties are inherited by a class.

Syntax

Class classname extends superclass implements interface1,interface2

{

body of class

}

  • We can implement number of interface.
  •  When a class implements more than one interface they are represented by comma.         

No comments:

Post a Comment