Results 1 to 2 of 2
  1. #1
    mikeZet is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Interfaces - What is the advantage in this case ?

    Hi,

    Here I have two types of programs: one with the use of interface, and other without it. Both programs do the same thing.

    Using Interface

    Interface SHAPE
    Java Code:
    public interface Shape {
    	
    	public double getArea();
    
    }

    Class CIRCLE implements the interface SHAPE
    Java Code:
    public class Circle implements Shape {
    
    	private double radius;
    	
    	public Circle(double radius){
    		
    		this.radius = radius;
    }
    		
    	
    	public double getArea(){
    		
    		return 3.14*radius*radius;
    	}

    Class RECTANGLE implements the interface SHAPE
    Java Code:
    public class Rectangle implements Shape{
    	
    	private int width;
    	private int heigth;
    	
    	public Rectangle(int width, int heigth){
    		this.width = width;
    		this.heigth = heigth;
    	}
    	
    	public double getArea(){
    		
    		return width*heigth;
    	}

    Testing the program


    Java Code:
    public class ShapeTester {
    
    	public static void main(String[] args){
    		
    		
    		
    		Shape circle = new Circle(5);
    		Shape rectangle = new Rectangle(4,6);
    	
    		System.out.println("CIRCLE AREA: " + circle.getArea());
    		System.out.println("RECTANGLE AREA: " + rectangle.getArea());
    	}
    	
    	
    }
    OUTPUT

    CIRCLE AREA: 78.5
    RECTANGLE AREA: 24.0



    Program without using interface



    Circle CLASS
    Java Code:
    public class Circle {
    	
    	private int radius;
    	
    	public Circle(int radius){
    		
    		this.radius = radius;
    	}
    	
    	
    	public double getArea(){
    		return 3.14*radius*radius;
    	}
    
    }

    RECTANGLE CLASS

    Java Code:
    public class Rectangle {
    
    	private int width;
    	private int height;
    	
    	public Rectangle(int width, int height){
    		this.width = width;
    		this.height= height;
    	}
    	
    	public double getArea(){
    		
    		return width*height;
    	}
    
    }

    Testing the Program

    Java Code:
    public class ShapeTester {
    
    	public static void main(String[] args){
    		
    		Circle circle = new Circle(5);
    		Rectangle rectangle = new Rectangle(4,6);
    		
    		System.out.println("CIRCLE AREA: " + circle.getArea());
    		System.out.println("RECTANGLE AREA: " +rectangle.getArea());
    			
    		
    	}
    	
    	
    }
    OUTPUT

    CIRCLE AREA: 78.5
    RECTANGLE AREA: 24.0


    So my question is:

    What is the use of the interface in this case if without it the result and the simplicity it is the same ?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Interfaces - What is the advantage in this case ?

    In your later example, not an advantage. But try putting each instance of those classes in an ArrayList or array and calling them in a loop to print out the areas.
    You can't use an Object type because Object doesn't know about getArea(). And you can't put two different class instances in the same array otherwise (unless you subclass something and override or implement the method).

    Now declare an ArrayList or array of type Shape and try it with your first example.

    And here is another use. Suppose you wanted to call a method which needed the interface methods for a variety of implementations.
    In your case you would create the method which accepts a Shape type as an argument. Then you can pass any instance of a class
    which implements that interface.

    Here is an example of the later.

    Java Code:
    public class InterfaceDemo {
    
       public static void main(String[] args) {
    //   Normally, this would be specified as List<Integer> intList = ...
          ArrayList<Integer> intList = new ArrayList<>(); 
          intList.add(10);
          intList.add(20);
          
    // Normally, this would be specified as List<String> stringList = ..
          LinkedList<String> stringList = new LinkedList<>();
          stringList.add("Interfaces");
          stringList.add("are");
          stringList.add("useful!");
          
          doSomethingWithList(intList);
          doSomethingWithList(stringList);
       }
       
       public static <T> void doSomethingWithList(List<T> a) {
          System.out.println(a);
          System.out.println("Size = " + a.size());
       }
    
    }
    If there is ever a possibility of having classes with similar methods, then program to an interface. Also, since implementations of an interface may
    evolve (but named differently), it makes code modification much easier.

    Regards,
    Jim
    Last edited by jim829; 04-27-2017 at 10:01 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 12
    Last Post: 11-15-2015, 10:16 AM
  2. Advantage of HQL over SQL?
    By jmohandos304 in forum Hibernate
    Replies: 11
    Last Post: 09-15-2014, 10:47 AM
  3. advantage of Runnable over Thread
    By viki1719 in forum Threads and Synchronization
    Replies: 4
    Last Post: 05-18-2013, 01:13 AM
  4. advantage of using thread
    By simorgh in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-18-2010, 05:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •