Results 1 to 4 of 4
- 12-21-2009, 07:24 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 1
- Rep Power
- 0
-
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:
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.Java Code:List<Fu> fuList = new ArrayList<Fu>();
- 12-21-2009, 12:40 PM #3
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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 :) )
keep on learning this stuff its very useful for advanced codingJava 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 } }
regards :)
- 12-21-2009, 01:04 PM #4
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
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
-
declare a private instance variable called ran of type random
By tinman in forum New To JavaReplies: 1Last Post: 12-09-2009, 12:57 AM -
Creating a Defined Class with Member Functions
By New2Java in forum New To JavaReplies: 6Last Post: 08-05-2009, 09:05 PM -
local class incompatible, while I do declare serialVersionUID
By Norberhuis in forum NetworkingReplies: 2Last Post: 01-09-2009, 07:23 PM -
Declare methods in a class
By Adiel224 in forum New To JavaReplies: 5Last Post: 09-19-2008, 10:38 AM -
Error: Cannot access protected member long getTimeInMillis() in class Calendar
By cachi in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks