Re: pls clarify important concepts...

From: Frances Del Rio (fdr58_at_yahoo.com)
Date: 02/09/05


Date: Tue, 08 Feb 2005 21:53:59 -0500


Bryce wrote:
> On Tue, 08 Feb 2005 15:42:25 -0500, Frances Del Rio <fdr58@yahoo.com>
> wrote:
>
>
>>I always read if you implement an interface you have to 'implement' all
>>methods in that interface..
>
>
> That is correct.
>
>
>>but take an interface like ServletRequest,
>
>>from servlet API, it has a million methods, do I have to implement all
>
>>methods of this interface if I implement ServletRequest?
>
>
> Yes, but fortunately, you'll rarely, if ever, need to implement
> ServletRequest...
>
>
>>what is it
>>exactly to 'implement' a method? can you just 'declare' method (no
>>stmts inside method, I mean just 'mention' method....;)...)?
>
>
> An empty declaration is still a declaration.
>
> public interface MyInterface {
> public void myMethod();
> }
>
>
> you can just have:
> public class MyClass implements MyInterface {
> public void myMethod() {
> }
> }
>
> Note, that if the method returns something, you have to return
> something.
>
>
>>usu. servlets open like this:
>>
>> public class <ClassName> extends HttpServlet {
>>
>>this is not the same as implementing, right? when would you want to
>>'extend' (create subclass) and when wd you want to 'implement'?
>
>
> That is correct. It is extending. You extend classes, you implement
> interfaces.
>
> Generally, you only subclass if there's some common logic that needs
> to be shared in a class heiarchy. Using interfaces allows you to have
> strong typing, without having to resort to inheritance.
>
> For example, using the examples above, you could have:
>
> MyInterface int = new MyClass();
>
> It really doesn't matter what the class is that you assign to "int",
> as long as it implements MyInterface. This is handy when you want to
> be able to support mutliple strategies.
>
> Here's a concrete example.
>
> Lets say we want to write a database application that adds and deletes
> users.
>
> public interface UserDao {
> public void addUser(String username);
> public void deleteUser(String username);
> }
>
>
> Now, if we want to support simple JDBC, we could implement this class
> like so:
>
> public class UserDaoJDBC implements UserDao {
> public void addUser(String username) {
> // Perform JDBC functions to add user
> }
>
> public void deleteUser(String username) {
> // Perform JDBC functions to delete user
> }
> }
>
> So you write some client code:
>
> public class UserManager {
> public static void main(String[] args) {
> UserDao userDao = new UserDaoJDBC();
> userDao.addUser("John");
> userDao.removeUser("Mary");
> }
> }
>
>
> See how once the userDao is instantiated, all you know about the
> object is that it implements UserDao, you could care less if its JDBC
> (There are ways to abstract that out, using Factories or Dependency
> Injection, but I'm keeping it simple here for educational sake).
>
> Lets now say that you decided you no longer wanted to use straight
> JDBC, but instead want to change your app to use Hibernate.
>
> public class UserDaoHibernate implements UserDao {
> public void addUser(String username) {
> // Perform Hibernate functions to add user
> }
>
> public void deleteUser(String username) {
> // Perform Hibernate functions to delete user.
> }
> }
>
> Now, in our client code, all we need to c hange is the instantiation:
>
> public class UserManager {
> public static void main(String[] args) {
> UserDao userDao = new UserDaoHibernate();
> userDao.addUser("John");
> userDao.removeUser("Mary");
> }
> }
>
> Notice, the way the client uses the object is identical to before.
>
> This is a somewhat contrieved example. In this example, you still have
> to change the client code to reflect that you want to instantiate a
> UserDaoHibernate...
>
> You would probably want to abstract that out using a factory or
> Dependency Injection framework (such as Spring or PicoContainer).
> Don't want to confuse you on this matter, so don't worry about how
> those work. What I want you to get out of this is the fact that the
> interface allowed use to write the client with that interface,
> abstracting the actual implementation.
>

('now w/more cowbell..' the other day I heard a story about this on NPR
and finally I know what this is..... ;) ... had always wondered......:)

Bryce, funny you should mention JDBC, db, etc... because I'm learning
MySQL... haven't delved into connecting w/JDBC yet, but whatever I read
about this you always have to start w/the driver.. I read there are four
types of drivers, but it never says how to make the choice of which
driver to use, does it matter? or is it stricktly up to programmer? it
says, for example, in George Reese (Oreilly) "Managing & Using MySQL":
"As a database programmer, you should select a JDBC implementation that
will provide the greatest stability and performance for your
application." Now what I have is what I dl'd from mysql.com (version
4.1/Win2000, which I run on command line-client.. so what would be the
best driver to use w/that? ( also have MySQL at my webhosting on unix..
may be easier than trying to connect from Tomcat, even though that would
be strictly local..;)..)

ooops... I changed the subject a bit.. Bryce, again thank you for all
your help.... Frances



Relevant Pages

  • Re: pls clarify important concepts...
    ... >methods of this interface if I implement ServletRequest? ... public class MyClass implements MyInterface { ... public void myMethod() { ... public class UserDaoJDBC implements UserDao { ...
    (comp.lang.java.help)
  • Re: Generics
    ... type and implement a interface. ... public void DoIt() ... public class SmallCar: BaseCar, IDoSomething ... //I want to hold a list with cars, ...
    (microsoft.public.dotnet.languages.csharp)
  • Generics
    ... type and implement a interface. ... public void DoIt() ... public class SmallCar: BaseCar, IDoSomething ... //I want to hold a list with cars, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Speed of interfaces vs inheritance
    ... implementation of an interface. ... Another thing to note is the degree of actual polymorphism at the call site. ... But that kind of optimisation makes considering call sites difficult. ... abstract public void abstractCall; ...
    (comp.lang.java.programmer)
  • Re: HashMap get/put
    ... interface IFace ... public void DoitNow() ... DoitNow is generated as a non-virtual method. ... But, change DoitNowto be virtual, add an override in Derived, and then rebuild Assembly1.dll. ...
    (comp.lang.java.programmer)