Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Interface

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 284
    Comment on it

    Interface is the way to get the multiple inheritance.
    This means that a class can inherit two or more class.
    As java, we can not extend more than one class but we can extend more than one interface.
    So Interface is used to have inheritance
    But there is one problem. The number of methods declare in the interface are to be inherited in the base class.
    It is like the base class have to marry the Interface and acquire all the properties of that Interface by using method

    interface Bicycle {
    
        //  wheel revolutions per minute
        void changeCadence(int newValue);
    
        void changeGear(int newValue);
    
        void speedUp(int increment);
    
        void applyBrakes(int decrement);
    }
    
    To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:
    
    class ACMEBicycle implements Bicycle {
    
        int cadence = 0;
        int speed = 0;
        int gear = 1;
    
       // The compiler will now require that methods
       // changeCadence, changeGear, speedUp, and applyBrakes
       // all be implemented. Compilation will fail if those
       // methods are missing from this class.
    
        void changeCadence(int newValue) {
             cadence = newValue;
        }
    
        void changeGear(int newValue) {
             gear = newValue;
        }
    
        void speedUp(int increment) {
             speed = speed + increment;   
        }
    
        void applyBrakes(int decrement) {
             speed = speed - decrement;
        }
    
        void printStates() {
             System.out.println("cadence:" +
                 cadence + " speed:" + 
                 speed + " gear:" + gear);
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: