|
Interfaces are used in two ways: 1. Contracts - if you implement the required methods, then you can be one (i.e. your object can be referenced by that kind of data type). Container c = new JPanel(); 2. Markers - no methods to implement. Used to mark objects for a specific purpose. Serializable is an example. It has no methods. Any class that implements it, can be used in readObject() and writeObject() methods. The SingleThreadModel interface tells the web server to run in a one-to-one mode with instances of the servlet and browser requests, essentially negating the biggest advantage of using servlets versus CGI. Joe Teff ------Original Message------ From: "Eyers, Daniel" <daniel.eyers@honeywell.com> To: "'JAVA400-L@midrange.com'" <JAVA400-L@midrange.com> Sent: March 20, 2001 3:51:48 PM GMT Subject: Understanding Implements hmmm... can you explain what the following is doing? public class myClass extends HttpServlet implements SingleThreadModel I know I have a class called myClass that inherits from HttpServlet. I also know that implementing SingleThreadModel changes the way the HttpServlet is handled by WAS. thanks dan -----Original Message----- From: Larry Loen [mailto:lwloen@us.ibm.com] Sent: Tuesday, March 20, 2001 10:06 AM To: JAVA400-L@midrange.com Subject: RE: HTML to XML, vice versa Aaron Bartell asked about the "interface" keyword. "Interface" solves a set of problems that bedevil the O-O world. The most important situation is this: class SwissArmyKnife extends ......what?? A Swiss Army Knife "is a" knife. But, it also "is a" screwdriver and "is a" scissors. How does one deal with this multitude of "is a" situations? In C++, there is a scheme called "mulitple inheritance" and it sounds straightforward, but it isn't. You simply "extend" for all the objects you wish. In Java syntax: class SwissArmyKnife extends knife, scissors, nailfile. . . However, in terms of actual theory and practice, this is very difficult to deal with. I won't even try and repeat the bulging literature on this. Java's solution is to permit exactly one "parent" for each child. This gets rid of multiple inheritance by definition. But, what about Swiss Army Knives? Instead, to handle this, Java defines the notion called an "interface" and it is a kind of special case class. 1. No variables. 2. No default implementation for methods in the interface proper. 3. You can "implement" as many interfaces on your class as make sense. 4. If a regular class "implements" an interface, it must supply a method for every method the interface does. 5. "instance of" for a class that implements a method, will return true if the instance of targets the interface name. So, assuming you understood it was coming at the start, you would not design a "knife" class. Or, if you did, you would add a "knifeinterface" class and have "knife" implement it. Thus: class knife extends sharpobject implements knifeinterface ... class SwissArmyKnife implements knifeinterface, scissorsinterface, nailfileinterface.... And, illustrating 5 above: SwissArmyKnife swiss = Something(); if (swiss instanceof knifeinterface) System.out.println("Will print this"); knifeinterface ki = swiss; // allowed Interfaces, because they formally lack variables, are often used in Java for items whose implementations are expected to vary radically. Whether this is really theoretically justified, I never have tracked down, but it is a fact of life. An interface important to us all is the JDBC definitions, for instance. Any sort of "factory" type interface tends to be defined with a Java interface rather than some sort of base class with a large number of abstract methods. Larry W. Loen - Senior Java and AS/400 Performance Analyst Dept HP4, Rochester MN +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +--- +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +--- +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.