
The interface is responsible for producing the sound of the bird while the class shows the production of sound (“Caw Caw” and “Kwi Kwi Kwi”). However, the two birds have different sounds, meaning that the bird class uses the interface method independently. In the above snippet, both Crow and parrot implement the interface bird. To create an interface, you need to use the keyword interface as illustrated below:
#Java interface code#
In a sizeable complex application with lots of classes depending on each other, we can use interfaces to decouple the classes, which will reduce the impact on other classes when we change a single line of code in one class.

To reduce this impact, we put interfaces between the classes such that when we change the code in class Z, it does not affect the code in class X. If we change class Z, X will be affected. Let us assume we have two classes, X and Z, where class X depends on class Z. Interfaces play a significant role when used in classes, for instance, those that depend on each other. Java provides default keyword to create default method.Interfaces in java allow the developer to build reusable, testable, extensible, and coupled components. It means default method is not abstract method, it is used to set some default functionality to the interface. Default method is a method that can have its body. In Java 8 version a new feature is added to the interface, which was default method. Interfaces are useful in a situation that all properties should be implemented. Whereas, Interface contains all abstract methods and final variable declarations.Ībstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interface is a pure abstract class which starts with interface keyword.Ībstract class can also contain concrete methods. The classes which implement the Interfaces must provide the method definition for all the methods.Ībstract class is a Class prefix with an abstract keyword followed by Class definition. Interface is a Java Object containing method declaration but no implementation. Abstract classĪbstract class is a class which contain one or more abstract methods, which has to be implemented by its sub classes. Interface and abstract class both are used to implement abstraction but have some differences as well. } Difference between an interface and an abstract class? Implementation can be provided by a class only. But in this case, interface just inherit, does not provide implementation. Interface can inherit to another interface by using extends keyword. In this example, two interfaces are implemented by a class that show implementation of multiple inheritance. Though classes in Java doesn't support multiple inheritance, but a class can implement more than one interfaces. print in ("Average speed is"+AVG-SPEED") In this example, we created an interface and implemented using a class. NOTE: Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default. Let's take a simple code example and understand what interfaces are: Interface can be nested inside another interface.Interface can extend one or more other interface.

All methods declared inside interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.


Interfaces are syntactically similar to classes, but you cannot create instance of an Interface and their methods are declared without any body.
#Java interface full#
This is the only way by which we can achieve full abstraction. Interface is a concept which is used to achieve abstraction in Java.
#Java interface driver#
