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 interface
DriveCar 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{
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.comUse of interface by third user:Use of interface to satisfy multiple inheritance
Still static method will have body inside interface
#InterfaceasanAPI: How could you say that Interface could be used as API's?
ReplyDeleteSee 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.
DeleteAPI's generally have method and class names.
So if user could call and make use of methods by using ..
#NeedstoImplementallmethods:
ReplyDeleteIs it mandatory that Implementation class should Implement all methods of Interface?
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#Isinterfacesameaschildclass
ReplyDeleteI am confused, how is Implementing class is different from Sub class?
Sub class make use of methods that super class has. So here its not implementation but a child and parent relation.
DeleteBut 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.
Implementing class should have method name but I doubt even if variable has to be without assignment.
DeleteWord 'implements' itself says that method needs to implemented in the class. Sub class just make use of super class methods does not implements.
DeleteIs using Interface a technique of Polymorphism or Inheritance?
ReplyDeleteFrom #SO: Interface is a contract you should comply to or given to, depending if you are implementer or a user.
DeleteNo, 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.
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