interface code example

Example 1: interface in java

/* File name : MammalInt.java */
public class MammalInt implements Animal {

   public void eat() {
      System.out.println("Mammal eats");
   }

   public void travel() {
      System.out.println("Mammal travels");
   } 

   public int noOfLegs() {
      return 0;
   }

   public static void main(String args[]) {
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
}

Example 2: what is interface

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.

Example 3: interface in java

/* File name : Animal.java */
interface Animal {
   public void eat();
   public void travel();
}

Example 4: java interface

public interface Exampleinterface {
	
  public void menthod1();
  
  public int method2();

}

class ExampleInterfaceImpl implements ExampleInterface {


  	public void method1()
    {
        //code here
    }
  
    public int method2()
  	{
    	//code here
  	}

}

Example 5: interface

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.

Example 6: Interface

Interface blueprint of class.The main purpose of an interface is providing 
additional information and behaviors to any class that needs it 
■ The other way to achieve abstraction in java is via interfaceAn interface is not a class, but acts similar, 
can be super type to a classTo create an interface:the keyword interface is used instead of classInheritance allows only one parent class,
but it is possible to implement multiple interfaces to a classIt’s possible to extend interfaces to other interfaces.
■ there is only one access modifier allowed in interface ==> public
*Interface can have: variable:(static & final by default),
methods: (abstract methods, static methods, default method)
*Interface cannot have: constructor, instance variable
instance methods, blocks

extends vs implements: both are used for inheriting         
extends: class extends class, interface extends interface 
(A class can inherit from one class only (extends)
implements: class implements interface1, Interface2(A class can inherit multiple interfaces (implements)
**RemoteWebDriver  implements WebDriver,
           /       |       \  
  chrome   firefox   opera     
 WebDriver driver = new ChromeDriver(); =>Interface
TakeScreenShot,JavaScriptExecuter=>Interface
List and Set also interface. You cannot create object in interface

Tags:

Java Example