Monday 28 April 2014

Interfaces in JAVA




1) Interface does not have any method implementation.

2) All methods in an interface are public and abstract.

3) All the methods in interface should override in subclass. If sub class is not override interface means is called abstract class.

Ex: Public interface mother{
Public abstract void cooking();
}

    public class teacher implements mother
    {
Public void cooking(){
System.out.println("cook well");
}
    }

4)  we cannot use follwing access modifiers in interface methodPrivate,protected,final,static,synchronized,native,strictfp. That means interface method only allowed public and abstract.

5) By defaulr Interface variables are public,final and static

6) No multiple inheritance in java - Cannot extend more than one class at a time. But we can implements the more than one interface into the class.

Ex: The object teacher have many role(type) like teacher,Mother,housewife etc.

So its difficult to use all types in single class. Here we are using interface for implement those types.

Public class teacher extends mother implements housewife,citizen
{ }

So here we cannot extends more than one class at a time for reusability. But we can implements the more than one interface into the class.



7) Interface can extend any number of interfaces

Ex: Public interface car extends vehicle,fourwheeler{
}

Remember if we implements car interface we should override all interface like car,vehicle,fourwheeler


7) Any number of interface can be implemented by concrete class or abstract class.



8) Usage of Interface:

Ex: Selenium suports IE,Firefox,opera.. Here we can use common interface (WebDriver) for implement the function (Get()) for selenium interacts with muliple browser. In this way we can easily use the same variables and functions while developing the script.


Syntax:

Public interface calc{
int number=99;   // By default all fields are public and static
public abstract void add();
public abstract void add();
}



Interface


1) used to define the methods. Those methods are access by the class.
2) The main class able to access the all methods present in class and interface if we defined the object using class
3) But we not able to use methos which is not presented in interface if we defined object using interface.

public interface mobilphone

{
public void call();
public void recharge();
}

public class interfaceclass implements mobilephone

{

public void call()
{ system.out.println("call");

Public void recharge()
{ system.out.println("Rechaege");

public void ratecutter()
{ system.out.println("Rate cutter");

}


public class mainclass1
{
public static void main(String args[])
{
interfaceclass obj1=new interfaceclass();
obj1.call();
obj1.recharge();
obj1.ratecutter();


mobilphone obj2=new interfaceclass();
obj2.call();
obj2.recharge();
obj2.ratecutter();  // we not able to use methos which is not presented in interface if we defined object using interface.

}

No comments:

Post a Comment