xxxxxxxxxx
Interfaces specify what a class must do.
It is the blueprint of the class.
It is used to achieve total abstraction.
We are using implements keyword for interface.
Basic statement we all know in Selenium is
WebDriver driver = new FirefoxDriver();
WebDriver itself is an Interface.
So we are initializing Firefox browser
using Selenium WebDriver.
It means we are creating a reference variable
of the interface and creating an Object.
So WebDriver is an Interface and
FirefoxDriver is a class.
Interfacing: In simple words, interfacing is communication across two or more components of a computer system by exchanging information.
xxxxxxxxxx
--Interface in java-
-used to specify the behaviour of a class
-blueprint of a class
-contains static constants and abstract methods
-mechanism to achieve 100% abstraction
-used to achieve abstraction and multiple inheritance in java
-interfaces can have abstract methods and variables, cannot have a method body.
-represents IS-A relationship
-cannot be instantiated just like abstract class
-since java 8, can have default and static methods
-to declare, use interface keyword
-all fields public, static and final by default
-class that implements interface must implement all the methods from interface
-to implement interface to a class, use implements keyword
-syntax-
interface InterfaceName
{
//declare constant fields
//declare methods that is abstract by default
}
-we save interface also with .java extension
-uses of interface-
1.to achieve total abstraction
2.to achieve multiple inheritance
3.to achieve loose coupling
-Note- The java compiler adds public and abstract keywords before the interface
method. Moreover, it adds public, static and final keywords before data members.
-eg.
//Shape.java
public interface Shape
{
void draw();
}
//Rectangle.java
class Rectangle implements Shape
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
-class extends another class, interface extends another interface,
but class implements an interface
--multiple inheritance in java by interface
-class implements multiple interfaces, or interface extends multiple interface known as multiple inheritance
-
-Interface inheritance-
-interface extends another interface
-New features added in Interfaces in JDK 8-
-1.default methods inside an interface:
-to add new function in existing interface, old code will not work as classes not implemented those new functions
-so with help of default implementation, we will give a default body for newly added functions,
-then old codes will still works.
-defaults methods need not override inside implementation class, but if you want you can.
-2.Static methods inside interface:
-static methods in interface can be called independently withour an object.
-these methods are not inherited, call static method with help of interface name
//Intr.java
interface Intr
{
//abstract method
void method1();
//default method
default void method2()
{
System.out.println("inside method2");
}
//static method
static void method3()
{
System.out.println("inside method3");
}
}
//IntrImpl1.java
class IntrImpl1 implements Intr
{
public void method1()
{
System.out.println("inside method1 of IntrImpl1");
}
}
//Main.java
class Main
{
public static void main(String[] args)
{
IntrImpl1 i1 = new IntrImpl1();
i1.method1(); //abstract method
i1.method2(); //default method
Intr.method3(); //static method cannot inherited, called with interface name
}
}
---------------------------------------------------------------------
--marker or tagged interface in java-
-interface has no member
-eg. Serializable, Clonable, Remote etc.
-used to provide some essential information to the JVM that JVM may perform some useful operations.
----------------------------------------------------------------------
--Difference between Abstract class and Interface-
Abstract class Interface
1. An Abstract class can have
abstract and non-abstract
methods.
An Interface can have only abstract methods. Since
Java 8, it can have default and static methods also.
2. An Abstract class doesn't
support multiple inheritance. .
An Interface supports multiple inheritance. .
3. An Abstract class can have
final, non-final, static and
non•static variables .
An Interface has only static and final variables .
4. An Abstract class can provide
the implementation of interface.
.
An Interface can't provide the implementation of
abstract class .
5. An abstract class can extend
another Java class and implement
multiple Java interfaces.
An interface can extend another Java interface only.
6. A Java abstract class can have
class members like private,
protected, etc.
Members of a Java interface are public by default.
xxxxxxxxxx
import { Component } from '@angular/core';
//Student interface
interface Student {
id: number;
name: string;
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
//Use of interface
students: Student[] = [
{id: 1, name: "Hardik"},
{id: 2, name: "Paresh"},
{id: 3, name: "Rakesh"},
]
}
xxxxxxxxxx
Interface Define a set of Characteristics and Behaviors
such as
Member Signature Only
No Implementation Details
Cannot be Instantiated
xxxxxxxxxx
Interfaces are Classes that contain "empty" methods that are implemented on-demand
- Implementing such a method is the same as overriding a normal class method.
- "abstract" modifier is used to prevent "instantiation" or "invocation" cause they have/are empty methods
- often used when firing events, that way same event can do multiple tasks
xxxxxxxxxx
class Calculator(AdvancedArithmetic):
def divisorSum(self, n):
temp = []
for i in range(1, n+1):
if n%i == 0:
temp.append(i)
return sum(temp)
xxxxxxxxxx
interface Readable
{
public function read();
}
interface Document extends Readable
{
public function getContents();
}
Code language: PHP (php)
xxxxxxxxxx
[Exposed=(Window,DedicatedWorker,SharedWorker)]
interface XMLHttpRequestEventTarget : EventTarget {
// event handlers
attribute EventHandler onloadstart;
attribute EventHandler onprogress;
attribute EventHandler onabort;
attribute EventHandler onerror;
attribute EventHandler onload;
attribute EventHandler ontimeout;
attribute EventHandler onloadend;
};
[Exposed=(Window,DedicatedWorker,SharedWorker)]
interface XMLHttpRequestUpload : XMLHttpRequestEventTarget {
};
enum XMLHttpRequestResponseType {
"",
"arraybuffer",
"blob",
"document",
"json",
"text"
};
[Exposed=(Window,DedicatedWorker,SharedWorker)]
interface XMLHttpRequest : XMLHttpRequestEventTarget {
constructor();
// event handler
attribute EventHandler onreadystatechange;
// states
const unsigned short UNSENT = 0;
const unsigned short OPENED = 1;
const unsigned short HEADERS_RECEIVED = 2;
const unsigned short LOADING = 3;
const unsigned short DONE = 4;
readonly attribute unsigned short readyState;
// request
undefined open(ByteString method, USVString url);
undefined open(ByteString method, USVString url, boolean async, optional USVString? username = null, optional USVString? password = null);
undefined setRequestHeader(ByteString name, ByteString value);
attribute unsigned long timeout;
attribute boolean withCredentials;
[SameObject] readonly attribute XMLHttpRequestUpload upload;
undefined send(optional (Document or XMLHttpRequestBodyInit)? body = null);
undefined abort();
// response
readonly attribute USVString responseURL;
readonly attribute unsigned short status;
readonly attribute ByteString statusText;
ByteString? getResponseHeader(ByteString name);
ByteString getAllResponseHeaders();
undefined overrideMimeType(DOMString mime);
attribute XMLHttpRequestResponseType responseType;
readonly attribute any response;
readonly attribute USVString responseText;
[Exposed=Window] readonly attribute Document? responseXML;
};