MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Interfaces


Interfaces in Java :

Just like a class in java is a collection of the related methods, an interface in java is a collection of abstract methods.
The interface is one more way to achieve abstraction in Java.
An interface may also contain constants, default methods, and static methods.
All the methods inside an interface must have empty bodies except default methods and static methods.
We use the interface keyword to declare an interface.
There is no need to write abstract keyword before declaring methods in an interface because an interface is implicitly abstract.
An interface cannot contain a constructor (as it cannot be used to create objects)
In order to implement an interface, java requires a class to use the implement keyword.

Example to demonstrate Interface in Java :


interface Bicycle { 
            void apply brake ( int decrement ); 
            void  speed up ( int increment );
            }

class Avon cycle implements Bicycle {
            int speed = 7 ; 
             void apply brake ( int decrement ) {
                     speed = speed - decrement ;
                         }
        void speedup ( int increment ){ 
                 speed = speed + increment ; 
                    }

Example


package com.company;

interface Bicycle{
    int a = 45;
    void applyBrake(int decrement);
    void speedUp(int increment);
}

interface HornBicycle{
    int x = 45;
    void blowHornK3g();
    void blowHornmhn();
}

class AvonCycle implements Bicycle, HornBicycle{
    //public int x = 5;
    void blowHorn(){
        System.out.println("Pee Pee Poo Poo");
    }
    public void applyBrake(int decrement){
        System.out.println("Applying Brake");
    }
    public void speedUp(int increment){
        System.out.println("Applying SpeedUP");
    }
    public void blowHornK3g(){
        System.out.println("Kabhi khushi kabhi gum pee pee pee pee");
    }
    public void blowHornmhn(){
        System.out.println("Main hoon naa po po po po");
    }
}
public class interfaces {
    public static void main(String[] args) {
        AvonCycle cycleHarry = new AvonCycle();
        cycleHarry.applyBrake(1);
        // You can create properties in Interfaces
        System.out.println(cycleHarry.a);
        System.out.println(cycleHarry.x);

        // You cannot modify the properties in Interfaces as they are final
        // cycleHarry.a = 454;
        //System.out.println(cycleHarry.a);

        cycleHarry.blowHornK3g();
        cycleHarry.blowHornmhn();
    }
}

Output

Applying Brake
45
45
Kabhi khushi kabhi gum pee pee pee pee
Main hoon naa po po po po


Conclusion

In this page (written and validated by ) you learned about Java Interfaces . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Abstract Classes Vs Interfaces.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.