Monday, April 18, 2016

JAVA Interface Implementation

Hi folks,

In this post, I decide to cover little bit confused area in programming, most of the candidates are little bit straggle in when they faced a job interview. On OOP inheritance concept depend on these two methodologies. I explained OOP & Abstract concept, in the previous post. Today I’m figuring out major differentiations between Interfaces & Abstract concepts.


Basically Interfaces & Abstract class are looks like same, when most of the programmers deal with those both methodologies in very first time. But the reality is these two techniques are introduced for the two different things. As well as in a very complex scenario, we must identify which technique implements in to the system. Otherwise, most probably we face difficulties in developing/maintaining stages in SDLC.   


Use Case Diagram

Ok, let’s try to implement above scenario, for the implementation process I use to unique features of the interface. Following list listed down those features. 

1.                  Interface variables are by default Constant.
2.                  The interface allows only method signatures / doesn’t allow method body (Above JAVA 8)
3.                  The interface allows to implement multiple interfaces.
4.                  Interface, Interface implementation can use extends keyword
5.                  Class, Interface implementation can using implements keyword
6.                  An interface can extend multiple interfaces.

Following code segments depict how Mobile Phone class extending using interface wise implementation. For the demonstration I used highlighted segment in above UML diagram. Try to implement whole scenario by yourself.  

package com.interfaces;

public interface Secondary {
    
    public double MEGA_PIXELS = 13;
    public int Resolution = 1080;
   
}
Secondary.java
package com.interfaces;

public interface Primary {
    
    public double MEGA_PIXELS = 13;
    public double FLASH_RANGE = 4;
    public int [] SENSITIVITY = { 100, 200, 400, 800, 1600 };
    public double ZOOM = 3;
    
    public boolean zoomLence(int zoomRange);
    
    public void onFlash();
    
    public void offFlash();
    
    public void autoFocus();
    
    public int changeSensitivity(int Sensitivity);
}
Primary.java

package com.interfaces;

public interface Display {
    
    public String TYPE = "CAPACITIVE TOUCHSCREEN, 16M COLORS";
    public double SIZE = 5.0;
    public int[] RESOLUTION = {720 , 1280};
    
    public boolean multiTouch();
    
    public boolean turnOnDisplay();
    
    public boolean turnOffDisplay();
    
}
Display.java
package com.interfaces;

public interface Camera extends Primary, Secondary{
    
    public String FILE_FORMAT = ".jpeg";
    
    public boolean saveFile(String fileName);
    
    public boolean capture();

}
Camera.java
package com.application;

import com.interfaces.Camera;
import com.interfaces.Display;

public class MobilePhone implements Camera, Display{
    
    private String brandName;
    private int year;
    private String price;
    
    @Override
    public boolean zoomLence(int zoomRange){
        return true;
    }
    
    @Override
    public boolean saveFile(String fileName){
        return true;
    }
    
    @Override
    public void onFlash(){
    }
    
    @Override
    public void offFlash(){
    }
    
    @Override
    public int changeSensitivity(int Sensitivity){
        return 0;
    }
    
    @Override
    public boolean turnOnDisplay(){
        return true;
    }
    
    @Override
    public boolean multiTouch(){
        return true;
    }
    
    @Override
    public boolean turnOffDisplay(){
        return true;
    }
    
    @Override
    public boolean capture(){
        return true;
    }
    
    @Override
    public void autoFocus(){
    }    
}
MobilePhone.java
Additionally, JAVA 8 allow to implement method body inside interface, it looks like the evolution of Interface concept. Following example, I explained how to implement a method body in the interface. Using default & static key word we can implement a method body inside an interface scoop. Also override a default method is not a mandatory, but two interfaces, content same name compiler doesn’t identify which method belongs to which interface. So mitigate demanding issues if there any method to contain the same method signature in two different interfaces, overriding is mandatory. But method which are implements using the static key word can’t override in any concern.
 interface Java8Interface {  
   static void playCaptureSound(){  
     System.out.println("Click !!!");  
   }  
   default void formatMemory(){  
     //Do stuff here;  
   }  
 }  


Happy coding,
Regards,
Denuwan Himanaga

No comments:

Post a Comment