Computer Science, asked by Sonynikku, 10 months ago

explain the various forms of interface implementation​

Answers

Answered by HaryanviChora001
4

Answer:

An interface is a way of describing what classes should do, without specifying how they should do it. A class can implement more than one interface. In Java, an interface is not a class but a set of requirements for the class that we want to conform to the interface. All the methods of an interface are by default public. So, it is not required to use the keyword public when declaring a method in an interface. Interfaces can also have more than one method. Interfaces can also define constants but do not implement methods. An interface is defined like a class. Its general form is:

access_specifier interface InterfaceName

(

Return-type Method-l(Parameters);

Return-type Method-2(Parameters);

Type variable-l=value;

Type variable-2=value;

Return-type Method-N(Parameters);

Type variable-N=value;

To make a class implement an interface, we must declare that the class intends to implement the given interface and we need to supply definitions for all methods using the implements keyword.

When an interface has been defined, any of the classes can implement that interface. The general form of the class that includes an implements clause is:

class classname extends superclass implements interface

{

//class body

}

If we say that the Employee class implements the Comparable interface, this can be written as:

class Employee implements Comparable

Interface is a fully abstract class. Using the keyword interface, you can easily define the interface. Interface classes specify what a class must do, instead of how to do it. Interface body contains the variables, but they are not counted as instance variables, and methods without any body, i.e. only the declaration of the methods are there.

Interface has to be implemented using implements keyword. If the interface is implemented in a concrete class then, in concrete class all the abstract method of the interface must be implemented. But if it is implemented in abstract class then only required methods have to be defined, others can be left, but in concrete class this abstract class will be extended, rest all the methods have to be implemented.

Interface works as a contract among the team members of a project. Some time there is a need to define fixed layout or model that has to be followed by everybody, yet each will have its own implementation, own code without bothering about the others code. They follow the same prototype. For example, a display 0 method can be a part of any class, but its definition will vary.

A class can extend one class and implements one or more interfaces simultaneously, the way how multiple inheritances can be possible in Java. An interface can extends any number of interfaces but one interface cannot implements another interface, because if any interface is implemented then its methods must be defined, and interface never has the definition of any method. Interface cannot be instantiated, but it can work as super class of any other class, so that, its reference variable can be created to store or refer to its sub-ordinate classes objects.

Implementation of Interfaces

The interface contains only method declarations. Interface does not allow the implementation of methods, so that method declaration is followed by a semicolon, but no braces. All methods declared in the interface are by default implicitly public, so no need to use public as prefix in declaration. Interface contains the data members as constants. These constant values are implicitly public, static, and final, these modifiers can be omitted.

Interface declaration can be headed by interface modifiers, those are as follows:

public

abstract

strict floating point

Syntax to declare an interface:

modifier interface_name {

return_type methodNamel(parameter-list);

return_type methodName2(parameter-list);

type final-varNamel = value;

type final-varName2 = value;

}

Example:

interface Abc

{

void displayMsg();

}

public class DemoInterface implements Abc

{

public void displayMsg()

{

System.out.println("This is implemented method of Abc interface");

}

}

An interface can extends any

Similar questions