Correct usage/method of Java Interfaces
I really don't know this is the right place to post this question,
i hope my question though fundamental, may not be that basic.
My question is: Java Interfaces, what is the proper way of using them? Conceptually, everybody i spoke to, says it supports multiple inheritance. And they just define methods, leaving upon the class that implement it, the functionality(behavior) of the methods. But although every word of this argument is true, I have also noticed that Interfaces are also used to flag. Means, certain funcationality of a particular class is only available, if a specific interfaces(s) is/are implemented. Servlets are good examples. So servlet cannot be servlet unless a particular interface is implemented.
Now the point is that, if I've to put the same behaviour in my own
program, can i do it? Or, this flagging is inbuilt inside JVM?
Can i make,
in three different packages
1) package1: Interface myInterface,
2) package2: A Class class1 with Main method,
3) package3: Another Class class2 whose functionality is available
to class1 only when interface myInterface is implemented by class1.
In such a way that when i try using class2 in class1, without
implementing myInterface, compiler gives me a message.....??
I hope i was able to explain myself. maybe i m just not thinking hard...the solution is just around the corner.
rgds to all
It seems to me that you need to disconnect your tie between inheritance and Java Interfaces.
An Interface tells an outsider that the class which implements the interface will have methods with a particular signature. The Interface itself does not implement any such methods - it is left to the classes which implement the interface to implement these methods. So there are no implementations which are "inherited" - we just know that if you pass an appropriately formatted message to an object which implements a particular interface and the message is a properly structured call to a method exposed by the interface, your object will know what you want to do and will perform the appropriate behavior based on that message.
Any object which implements an Interface can be addressed by that Interface --- similar to the way that an object of a derived class can still be called as an object of the parent class. So, any object implementing the Map Interface can be referred to as a Map object, any Comparable or Iterable or Runnable, etc.
Where an Interface must be implemented, such as your reference to a "servlet", the "contract" between a calling and a called object requires that the called object must implement the methods whose signatures appear in the Servlet Interface. The calling object does not care how you choose to implement those methods - its methods which are making calls to the "servlet" just know that they need to format their calls in a particular way and that they will be receiving some response to the call or that they know that some required behavior will be performed.
Interfaces do not require the extra storage and reference space and overhead needed that class inheritance requires.
With this in mind, could you restate the relationship you are looking for among your interface, class1 and class2? Do you have any particular implementation you are thinking about (more specific)?
Last edited by nspils; 01-01-2006 at 03:10 PM.
Thanx for detailing your reply. Well, i was actually looking for details on Marker Interface and their implementation (if i've to do it on my own!). I only came to know about this MI behavior recently.
Interfaces has been a topic of debate among me and my friends as to what are the possible usages of Interfaces do exists??
Well you are correct in saying that by implementing interfaces one can use dynamic dispatch feature to its fullest.
I am doing more reading on Contractual oriented programming.
Thanx for stopping by.
As you have probably realized, marker interfaces are a different breed. It is more of a 'declared attribute" - metadata - informing the user that this class has a characteristic, rather than a set of declared methods which need to be implemented.
There are threads in the java.sun developer forums which address the writing of one's own marker interface.
i want all inbuilt interface , methods, package
i hope that you ll send that all inbuilt interface , methods, package available in java
By umesh in forum Careers
Last Post: 08-31-2007, 03:19 PM
By Anonymous in forum Java
Last Post: 01-31-2002, 11:08 PM
By Brad O'Hearne in forum Talk to the Editors
Last Post: 11-05-2001, 09:32 AM
By Keith Franklin, MCSD in forum java.announcements
Last Post: 08-18-2000, 06:37 PM
By JJ in forum Enterprise
Last Post: 07-06-2000, 04:50 AM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center