Results 1 to 4 of 4
  1. #1
    everurz is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default declare class data member as an inteface type

    hello,

    i want to know can a member variable of a class can be declared as an intrface type......and if so what is its purpose?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yes, it most certainly can, and in fact commonly does. But you must instantiate the object with a class (though this could be an anonymous inner class). For instance we often use the List interface when using ArrayLists like so:

    Java Code:
    List<Fu> fuList = new ArrayList<Fu>();
    Now if later I decide that my code would run much more efficiently using a LinkedList, I'd not have to change anything in my code except for the constructor call.

  3. #3
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Post

    It's called composition or HAS-A relationship.

    Its is heavily used in design patterns.

    If you put Interface in your class as class member,
    you provide encapsulation,
    because your class does not need to know
    how interface is implemented.

    This way you can add more behaviours to your system
    at any time, by new implementations, and code in your main class stays the same!

    What you have to do is provide implementation of interface in some classes
    and then in run-time decide which implementation will you use.

    Simple example (not tested :) )

    Java Code:
    class A{
     private MyInterface job;
    
     public performOperation(){
      job.doJob();
     }
    
    }
    
    intefrace Myinterface {
     public void doJob()
    }
    
    class FisrtJob implements MyInterface {
      public void doJob() {
      System.out.println("doing first job");
    }
    
    class SecondJob implements MyInterface {
      public void doJob() {
      System.out.println("doing second job");
    }
    
    
    /*THIS IS RUNTIME WHERE YOU DECIDE WHICH IMPL WILL YOU USE*/
    class JobDemo {
     public static void main (String[] args){
      
     A a = new A();
     Myinterface job = new FirstJob();  //or new SecondJob
     a.performJob(); //this is always the same call
    
     }
    
    }
    keep on learning this stuff its very useful for advanced coding
    regards :)

  4. #4
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    That's a good way of programming.
    For example, how do you connect to a database using JDBC? You use interface Connection.
    This is useful because if you release a new database product and need to create a driver to it, you can create a class that implements Connection.
    If you have a user and what to change the database he uses, he does not need to throw all the code away, just need to change one place and all the rest of the code may work fine!
    Please don't laugh at my English... I'm trying my best! :)

Similar Threads

  1. Replies: 1
    Last Post: 12-09-2009, 12:57 AM
  2. Creating a Defined Class with Member Functions
    By New2Java in forum New To Java
    Replies: 6
    Last Post: 08-05-2009, 09:05 PM
  3. Replies: 2
    Last Post: 01-09-2009, 07:23 PM
  4. Declare methods in a class
    By Adiel224 in forum New To Java
    Replies: 5
    Last Post: 09-19-2008, 10:38 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07: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
  •