Monday, August 31, 2015

interface


Source: http://www.sitepoint.com/interface-and-inheritance-in-java-interface/

How to use:
An interface defines a contract which an implementing class must adhere to. The above interfaceDriveCar defines a set of operations that must be supported by a Car. So, a class that actually implements the interface should implement all the methods declared in the interface.


interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.


Example:

interface DriveCar{
turnRight();
turnLeft();
moveBack();
accelerate();
}


class Car implements DriveCar{ 
void turnRight(){ 
//implementation code goes here 
} 

void turnLeft(){ 
//implementation code goes here 
}

void moveBack(){ 
//implementation code goes here 
}

void accelerate(){ 
//implementation code goes here 
}

}
Now we can take a reference of type DriveCar and assign an object of Car to it.
Example:
DriveCar carDriver=new Car(); 
carDriver.turnLeft(); 
carDriver.moveBack(); 
//other method invocations 
We can also code in the following way:
Example:
Car car=new Car(); 
car.turnLeft(); 
car.moveBack(); 
//other method invocations 
Javapoint.com
Use of interface by third user:
Use of interface to satisfy multiple inheritance
Extends and implementation across interface:
Output: Hello Welcome One way to have method body inside interface:
output:drawing rectangle default method


Still static method will have body inside interface

Output: drawing rectangle 27

11 comments:

  1. #InterfaceasanAPI: How could you say that Interface could be used as API's?

    ReplyDelete
    Replies
    1. See API's are the methods that are used to by the users to interact with the application. Generally user calls the methods(that has a specific functionality) in API to perform his operations.

      API's generally have method and class names.

      So if user could call and make use of methods by using ..

      Delete
  2. #NeedstoImplementallmethods:

    Is it mandatory that Implementation class should Implement all methods of Interface?

    ReplyDelete
    Replies
    1. Yes, when some class implements interface, it is an agreement between the class and interface that all the methods in interface should be implemented.

      Delete
  3. #Isinterfacesameaschildclass

    I am confused, how is Implementing class is different from Sub class?

    ReplyDelete
    Replies
    1. Sub class make use of methods that super class has. So here its not implementation but a child and parent relation.

      But implementing class is not the direct child of Implemented class. But rather it will have the implementation of the methods that its Implemented class has. Implemented class should be declared with keyword Interface, and it should only have method name and variable name.

      Delete
    2. Implementing class should have method name but I doubt even if variable has to be without assignment.

      Delete
    3. Word 'implements' itself says that method needs to implemented in the class. Sub class just make use of super class methods does not implements.

      Delete
  4. Is using Interface a technique of Polymorphism or Inheritance?

    ReplyDelete
    Replies
    1. From #SO: Interface is a contract you should comply to or given to, depending if you are implementer or a user.

      No, Interface is not Blueprint. A blueprint typically includes the internals. But a interface is purely about what is visible on the outside of a class ... or more accurately, a family of classes that implement the interface.

      When you write an interface, you're basically saying: "I need something that...".

      #Me:
      Still not sure whether OOP concepts include Interface. But for sure it is not Polymorphism or Inheritance.

      Delete
    2. Since a class can implement multiple interfaces and then another class can extend this class, so that is how java is supporting multiple inheritance using interface.

      Delete