Results 1 to 4 of 4
  1. #1
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Question Creating objects for class and referenced to interface

    Hi ,
    I want to know the advantages of creating objects for class and referencing to interface.

    For example

    interface test{
    public void display();
    }
    class Testing implements test{
    public void display(){
    System.out.println("Inside Testing ");
    }
    }

    class MainClass{
    public static void main(String a[]){
    Test testingObject = new Testing();
    testingObject.display();
    }
    }

    Here why is it a good practice to hold the testingObject in test interface?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Creating objects for class and referenced to interface

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Creating objects for class and referenced to interface

    Consider this: there is an ArrayList and a LinkedList. Now what if you wanted to make a method which does not care what kind of list it is, but just wants "any" list?

    The answer:

    Java Code:
    public void doSomethingWithAList(List<SomeObject> theList){
    
    }
    To this method you can pass a LinkedList or an ArrayList (or a Vector if you really want to) - because they all implement the List interface. But if you'd have done this:

    Java Code:
    public void doSomethingWithAList(ArrayList<SomeObject> theList){
    
    }
    Then you would only be able to pass an ArrayList.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Creating objects for class and referenced to interface

    I have a method in a class:
    Java Code:
    public ArrayList<String> getMyList() {
       return myList;
    }
    Now, should I discover that it would actually be better to use a LinkedList for myList (for whatever reason) not only do I have to change the method signature, but I also now have to fix all the other places that use this method. If I'd used an interface instead I would have been able to switch out the concrete class represented by myList without any of the hassle.

    It also allows you to stub these things out when testing.

    In essence, you want to be coding against the contract of a class (the interface), not the actual implementation of that contract.
    The using class shouldn't care how the implementing class does its job, so long as it does its job.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  2. Replies: 4
    Last Post: 07-19-2011, 01:30 PM
  3. Replies: 1
    Last Post: 03-11-2011, 08:37 AM
  4. Objects not referenced in the sources
    By krimen_sp in forum NetBeans
    Replies: 0
    Last Post: 12-30-2010, 03:31 PM
  5. Replies: 12
    Last Post: 08-02-2010, 02:39 PM

Posting Permissions

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